您的位置:首页 > 其它

使用Get和Post提交数据的实现和差别

2016-03-07 19:19 543 查看
1、使用get提交数据,这个和Web相似,都是将数据拼接在URL上面,使用? 作通配符,请求头不会多什么东西,服务器返回的数据也是通过流的形式返回的!
String path="http://192.168.13.13/Web2/servelet/LoginServlet?name" + name + "&pass" + pass;
2、如果产生乱码, 则表示服务器和android的编码码表不匹配,android默认的编码是UTF-8 ,web默认的服务器编码是 gbk(windows系统的默认编码),这个需要对服务器
编码进行改造。
3、如果要提交中文,服务器接收的是URL 处理过后的一堆字符串,浏览器会自动做URL编码,而android不会,那么需要做URL编码;
final String path="http://192.168.13.13/Web2/servelet/LoginServlet?name" + URLEncoder.encode(name) + "&pass" + URLEncoder.encode(pass);
使用get提交数据代码展示:
 public Handler handler;    {        handler = new Handler() {            @Override            public void handleMessage(Message msg) {                Toast.makeText(MainActivity.this, (String) msg.obj,Toast.LENGTH_SHORT).show();            }        };    }    EditText et_name=null;            EditText et_password;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        et_name= (EditText) findViewById(R.id.ET_name);        et_password= (EditText) findViewById(R.id.ET_passWord);    }    public void click(View view){        String name=et_name.getText().toString();        String pass=et_password.getText().toString();        //android提交数据,在服务器网址上加通配符?,在服务器网址上拼接字符串,后面连接属性加值        final String path="http://192.168.13.13/Web2/servelet/LoginServlet?name" + URLEncoder.encode(name) + "&pass" + URLEncoder.encode(pass);        Thread thread=new Thread(){            @Override            public void run() {                try {                    URL url=new URL(path);                    HttpURLConnection connection= (HttpURLConnection) url.openConnection();                    connection.setRequestMethod("GET");                    connection.setConnectTimeout(500);                    connection.setReadTimeout(800);                    connection.connect();                    while(connection.getResponseCode()==200){                        InputStream inputStream=connection.getInputStream();                        String text= Util.getTextFromStream(inputStream);                        Message message=Message.obtain();                        message.obj=text;                        handler.sendMessage(message);                    }                } catch (Exception e) {                    e.printStackTrace();                }            }        };    }}
1、使用Post提交数据,请求头浏览器会自动添加如下两个属性,如果是代码android需要添加如下两个属性;:
Content-type;
Content—length;
而且Post需要写入数据,提交网址也只是服务器,不能用拼接的数据;
但是数据需要提交,是以流的方式提交,那么需要将数据以流的形式写入到到服务器;
主要代码:
//android  Post提交数据,path直接是服务器网址final String path="http://192.168.13.13/Web2/servelet/LoginServlet" ;Thread thread=new Thread(){    @Override    public void run() {        try {            URL url=new URL(path);            HttpURLConnection connection= (HttpURLConnection) url.openConnection();
              //拼接字符串            String data="name="+URLEncoder.encode(name)+"&pass=" + URLEncoder.encode(pass);            connection.setRequestMethod("POST");            //设置请求过程中的两个属性            connection.setConnectTimeout(500);            connection.setReadTimeout(800);            connection.connect();            connection.setRequestProperty("Content-Type", "appplication/x-www-form-urlencode");            connection.setRequestProperty("Content-Length", data.length() + "");            connection.setDoOutput(true);            OutputStream outputStream=connection.getOutputStream();            //以输出流的方式写入数据库,一旦下方条件代码一成立建立连接,则会写入            outputStream.write(data.getBytes());            while(connection.getResponseCode()==200){                InputStream inputStream=connection.getInputStream();                String text= Util.getTextFromStream(inputStream);                Message message=Message.obtain();                message.obj=text;                handler.sendMessage(message);            }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: