用HttpPost 和 HttpClient 发送请求到web 端回调数据

        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 请求时候参数在链接中拼接

用HttpPost 和 HttpClient 发送请求到web 端回调数据,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。