码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-07-02 18:02:16      阅读:244      评论:0      收藏:0      [点我收藏+]

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

用HttpPost 和 HttpClient 发送请求到web 端回调数据,布布扣,bubuko.com

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

标签:style   blog   http   color   数据   os   

原文地址:http://www.cnblogs.com/WangShenCode/p/3819786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!