標籤: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 請求時候參數在連結中拼接