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】// Listparams = new ArrayList ();// 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 请求的时候可以用
Listparams = new ArrayList (); 来存储参数传递 HttpClient 请求时候参数在链接中拼接