Android提交数据

               Android后台提交数据到服务器,一种是post,一种是get。两种方式的区别有两点,1.get方法的路径需带参  2.post方法多了请求头部长度与类型

post与get属于原生态方法,还有使用 网络请求框架:AsyncHttpClient方法。今天用一个登录案例来比较一下,代码如下:

                  

public class MainActivity extends AppCompatActivity {


    private EditText et_main_uname;
    private EditText et_main_upass;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_main_uname = (EditText) findViewById(R.id.et_main_uname);
        et_main_upass = (EditText) findViewById(R.id.et_main_upass);
    }


    public void loginGET(View view){
        String uname=et_main_uname.getText().toString();
        String upass=et_main_upass.getText().toString();
        new MyTask().execute(uname,upass,"GET");
    }
    public void loginPOST(View view){
        String uname=et_main_uname.getText().toString();
        String upass=et_main_upass.getText().toString();
        new MyTask().execute(uname,upass,"POST");
    }
    public void loginAsyncHttpClient(View view){
        String uname=et_main_uname.getText().toString();
        String upass=et_main_upass.getText().toString();


        AsyncHttpClient asyncHttpClient=new AsyncHttpClient();
        RequestParams params=new RequestParams();
        params.put("uname",uname);
        params.put("upass",upass);
//        Ctrl+H
//        ResponseHandlerInterface
        asyncHttpClient.post("http://193.168.3.134:8080/G150725_S2SH/loginActionlogin.action",params,new TextHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, org.apache.http.Header[] headers, String responseBody) {
                super.onSuccess(statusCode, headers, responseBody);
                Toast.makeText(MainActivity.this, ""+responseBody, Toast.LENGTH_SHORT).show();
            }


            @Override
            public void onFailure(int statusCode, org.apache.http.Header[] headers, String responseBody, Throwable error) {
                super.onFailure(statusCode, headers, responseBody, error);
            }
        });


    }




    class MyTask extends AsyncTask{


        private URL url;
        private HttpURLConnection connection;


        @Override
        protected Object doInBackground(Object[] objects) {


            String uname=objects[0].toString();
            String upass=objects[1].toString();
            String type=objects[2].toString();
            try {


                if("GET".equals(type)){
                    //用GET方式请求
                    url = new URL("http://193.168.3.134:8080/G150725_S2SH/loginActionlogin.action?uname="+uname+"&upass="+upass);
                    Log.i("test","get方式");
                }else if("POST".equals(type)){
                    Log.i("test","post方式");
                    //用POST方式请求
                    url = new URL("http://193.168.3.134:8080/G150725_S2SH/loginActionlogin.action");
                }
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod(type);
                connection.setConnectTimeout(5000);




                if("POST".equals(type)){
                    //设置可以允许对外输出数据
                    connection.setDoOutput(true);


                    String str="uname="+uname+"&upass="+upass;


                    //添加请求头
                    //Content-Length:24
                    connection.setRequestProperty("Content-Length",""+str.length());
                    //Content-Type:application/x-www-form-urlencoded
                    connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");


                    //将内容提交到服务器
                    connection.getOutputStream().write(str.getBytes());
                }




                if(connection.getResponseCode()==200){
                    InputStream is= connection.getInputStream();
                    BufferedReader br=new BufferedReader(new InputStreamReader(is));
                    String str=br.readLine();
                    return str;
                    //
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }




            return null;
        }


        @Override
        protected void onPreExecute() {
            super.onPreExecute();


        }


        //更新UI
        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);


            String s= (String) o;
            if("success".equals(s.trim())){
                Toast.makeText(MainActivity.this, "跳转到主界面", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(MainActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
            }
            //创建时间  修改时间
        }
    }












}

相比较而言,网络请求框架:AsyncHttpClient比原生态的方式简单了些,只肖几行代码就搞定了,方法都已封装好,只要使用就可以了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值