1. 程式人生 > >Web後臺和Android前端之間的網路通訊

Web後臺和Android前端之間的網路通訊

一、通過GET方式去提交資料:

public void loginGet(View view){
//例如:
       String uname=et_main_uname.getText().toString();
       String upass=et_main_upass.getText().toString();
       String path="http://193.168.2.141:7788/demo/login.do";
      new MyGetTask().execute(uname,upass,path);
    }

class MyGetTask extends AsyncTask{
        @Override
        protected Object doInBackground(Object[] params) {
            String uname=params[0].toString();
            String upass=params[1].toString();
            String path=params[2].toString();

            try {
                URL url=new URL(path+"?uname="+uname+"&upass="+upass);
                HttpURLConnection connection= (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(5000);
                if(connection.getResponseCode()==200){
                    InputStream is=connection.getInputStream();
                    BufferedReader br=new BufferedReader(new InputStreamReader(is));
                    String s=br.readLine();
                    return s;
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            return null;
        }
        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            String s= (String) o;
            Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
        }
    }

二、通過POST提交資料
public void loginPost(View view){
       String uname=et_main_uname.getText().toString();
       String upass=et_main_upass.getText().toString();
       String path="http://193.168.2.141:7788/demo/login.do";
      new MyPostTask().execute(uname,upass,path);
    }
class MyPostTask extends AsyncTask{

        @Override
        protected Object doInBackground(Object[] params) {
            String uname=params[0].toString();
            String upass=params[1].toString();
            String path=params[2].toString();
            try {
                URL url=new URL(path);
                HttpURLConnection conn= (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");
                conn.setConnectTimeout(5000);

                //admin 123
                //uname=admin&upass=123456

                String s="uname="+uname+"&upass="+upass;
                 //新增請求頭
                conn.setRequestProperty("Content-Length",s.length()+"");
                conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");//固定的寫法

                conn.setDoOutput(true);//允許對外輸出資料

                OutputStream os=conn.getOutputStream();
                os.write(s.getBytes());

                if(conn.getResponseCode()==200){
                    InputStream is=conn.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 onPostExecute(Object o) {
            super.onPostExecute(o);
            String s= (String) o;
            Toast.makeText(MainActivity.this,s, Toast.LENGTH_SHORT).show();

        }
    }

三、使用第三方AsyncHttpClient提交資料
public void loginAsyncHttpClient(View view){
        String uname=et_main_uname.getText().toString();
        String upass=et_main_upass.getText().toString();
        String path="http://193.168.2.141:7788/G160628_Server/login.do";

        AsyncHttpClient ahc=new AsyncHttpClient();
        RequestParams params=new RequestParams();
        params.put("uname",uname);
        params.put("upass",upass);
        ahc.post(this,path,params,new TextHttpResponseHandler(){
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseBody, Throwable error) {
                super.onFailure(statusCode, headers, responseBody, error);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseBody) {
                super.onSuccess(statusCode, headers, responseBody);
                Toast.makeText(MainActivity.this, responseBody, Toast.LENGTH_SHORT).show();
            }
        });


    }