用HttpPost 和 HttpClient 發送請求到web 端回調資料

來源:互聯網
上載者:User

標籤:style   blog   http   color   資料   os   

        btnok.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                // 跳轉到新的頁面                String name=usernameTextId.getText().toString();                String pwd=passwordTextId.getText().toString();                String url = "http://112.124.12.46/wxtest/login.soap?method=user.login&UserName="+name+"&Password="+pwd+"";                //String url = "http://112.124.12.46/wxtest/login.soap?method=user.login";                try {                                //【httppost 1】//                    List<NameValuePair> params = new ArrayList<NameValuePair>();//                    HttpPost request = new HttpPost(url);//                    request.setEntity(new UrlEncodedFormEntity(params,//                            HTTP.UTF_8));//                    HttpResponse response = new DefaultHttpClient()//                            .execute(request);//                    String retSrc = EntityUtils.toString(response//                            .getEntity());                    //【httppost 2】                    //【httpclient 1】                    HttpClient httpclient = new DefaultHttpClient();                    HttpGet get = new HttpGet(url);                    HttpResponse response = httpclient.execute(get);                    String retSrc = EntityUtils.toString(response.getEntity(),"utf-8");                    //【httpclient 2】                    //get response and trans to json                    JSONObject resultJson = new JSONObject(retSrc);                    //判斷是否請求成功                    if(response.getStatusLine().getStatusCode()==200)                    {                        String result = resultJson.get("msg").toString();                        int code = Integer.parseInt(resultJson.get("code").toString());                        if (code == 0) {                            //跳轉到菜單頁面-菜單頁面選擇相機-進入驗碼頁面                            Intent intent=new Intent(MainActivity.this,MenuActivity.class);                            startActivity(intent);                            //【cookie】 start                            String cookieFields = response.getHeaders("Set-Cookie")[0] .getValue(); String cookie = cookieFields.split(";\\s*")[0]; HttpGet nextGet = new HttpGet(url); nextGet.setHeader("Cookie", cookie);                            CookieSyncManager.createInstance(MainActivity.this);                            CookieManager cookieManager = CookieManager.getInstance();                            cookieManager.setAcceptCookie(true);                            cookieManager.setCookie("http://112.124.12.46", cookieFields);//cookies是在HttpClient中獲得的cookie                            CookieSyncManager.getInstance().sync();                            //【cookie】 end                        } else {                            // 彈出對話方塊                            dialog(result);                        }                    }                    else{                        dialog("串連異常");                    }                } catch (JSONException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                } catch (ClientProtocolException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                } catch (ParseException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        });

 HttpPost  請求的時候可以用

List<NameValuePair> params = new ArrayList<NameValuePair>(); 來儲存參數傳遞
HttpClient 請求時候參數在連結中拼接

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.