HttpClient的get方式:
由於是網絡請求,一定要放在子線程里做。
首先創建一個HttpClient對象:
HttpClient httpClient = new DefaultHttpClient();
然后創建一個HttpGet,將url地址傳進去:
HttpGet httpGet = new HttpGet(path);
然后獲取狀態碼,200的話就是訪問成功了:
int code = response.getStatusLine().getStatusCode();
接下來得到響應:
HttpResponse response = httpClient.execute(httpPost);
再判斷內容是否登入成功就行了。
完整的方法:
1 public static void requestNewForGetLogin(final Handler handler, final String username, final String password) { 2 3 new Thread(new Runnable() { 4 5 @Override 6 public void run() { 7 try { 8 String path = "http://192.168.0.106:8080/Starry/servlet/LoginServlet?username=" + URLEncoder.encode(username,"utf-8") + "&pwd=" + URLEncoder.encode(password,"utf-8"); 9 //創建一個HttpClient對象 10 HttpClient httpClient = new DefaultHttpClient(); 11 //設置請求方式 12 HttpGet httpGet = new HttpGet(path); 13 HttpResponse response = httpClient.execute(httpGet); 14 int code = response.getStatusLine().getStatusCode(); 15 if(code == 200) { 16 HttpEntity entity = response.getEntity(); 17 InputStream inputStream = entity.getContent(); 18 String result = StreamUtils.streamToString(inputStream); 19 boolean issuccess = false; 20 if(result.contains("success")){ 21 issuccess = true; 22 } 23 Message msg = Message.obtain(); 24 msg.obj = issuccess; 25 msg.what = 1; 26 handler.sendMessage(msg); 27 } 28 } catch (Exception e) { 29 e.printStackTrace(); 30 } 31 } 32 }).start(); 33 }
HttpClient的post方式:
post方式與get有很多相似之處。不同的就是需要創建集合封裝數據。
ArrayList<BasicNameValuePair> arrayList = new ArrayList<BasicNameValuePair>(); BasicNameValuePair nameValuePair = new BasicNameValuePair("username", username); arrayList.add(nameValuePair); BasicNameValuePair nameValuePair2 = new BasicNameValuePair("pwd", password); arrayList.add(nameValuePair2);
完整的方法:
1 public static void requestNewForPostLogin(final Handler handler, final String username, final String password) { 2 final String path = "http://192.168.0.106:8080/Starry/servlet/LoginServlet"; // Post 3 new Thread(new Runnable() { 4 5 @Override 6 public void run() { 7 try { 8 //創建一個HttpClient對象 9 HttpClient httpClient = new DefaultHttpClient(); 10 //創建一個請求方式 11 HttpPost httpPost = new HttpPost(path); 12 //創建集合封裝數據 13 ArrayList<BasicNameValuePair> arrayList = new ArrayList<BasicNameValuePair>(); 14 BasicNameValuePair nameValuePair = new BasicNameValuePair("username", username); 15 arrayList.add(nameValuePair); 16 BasicNameValuePair nameValuePair2 = new BasicNameValuePair("pwd", password); 17 arrayList.add(nameValuePair2); 18 //創建一個entity 19 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(arrayList,"utf-8"); 20 httpPost.setEntity(entity); 21 HttpResponse response = httpClient.execute(httpPost); 22 int code = response.getStatusLine().getStatusCode(); 23 if(code == 200) { 24 HttpEntity entity2 = response.getEntity(); 25 InputStream inputStream = entity2.getContent(); 26 String result = StreamUtils.streamToString(inputStream); 27 boolean issuccess = false; 28 if(result.contains("success")){ 29 issuccess = true; 30 } 31 Message msg = Message.obtain(); 32 msg.obj = issuccess; 33 msg.what = 2; 34 handler.sendMessage(msg); 35 } 36 37 } catch (Exception e) { 38 e.printStackTrace(); 39 } 40 } 41 }).start(); 42 }
AsyncHttpClient由於已經封裝好了在子線程里做,就不想自己創建子線程了。
AsyncHttpClient的get方式:
首先創建AsyncHttpClient對象:
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
然后調用asyncHttpClient里的get方法。asyncHttpClient.get(url, responseHandler)
第一個參數數地址,第二個參數可以用匿名類。
responseHandler類里有兩個方法onSuccess和onFailure,分別代表成功和失敗。
1 asyncHttpClient.get(path, new AsyncHttpResponseHandler() { 2 3 @Override 4 public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { 5 if(statusCode == 200) { 6 try { 7 String result = new String(responseBody,"utf-8"); 8 Toast.makeText(context, result, Toast.LENGTH_SHORT).show(); 9 } catch (UnsupportedEncodingException e) { 10 e.printStackTrace(); 11 } 12 13 } 14 } 15 16 @Override 17 public void onFailure(int statusCode, Header[] headers, 18 byte[] responseBody, Throwable error) { 19 System.out.println(".............Failure"); 20 } 21 });
完整的方法:
1 public static void requestNewForGetLogin(final Context context, final Handler handler, final String username, final String password) { 2 3 try { 4 String path = "http://192.168.0.100:8080/Starry/servlet/LoginServlet?username=" + URLEncoder.encode(username,"utf-8") + "&pwd=" + URLEncoder.encode(password,"utf-8"); 5 //創建一個AsyncHttpClient對象 6 System.out.println(path); 7 AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); 8 asyncHttpClient.get(path, new AsyncHttpResponseHandler() { 9 10 @Override 11 public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { 12 if(statusCode == 200) { 13 try { 14 String result = new String(responseBody,"utf-8"); 15 Toast.makeText(context, result, Toast.LENGTH_SHORT).show(); 16 } catch (UnsupportedEncodingException e) { 17 e.printStackTrace(); 18 } 19 20 } 21 } 22 23 @Override 24 public void onFailure(int statusCode, Header[] headers, 25 byte[] responseBody, Throwable error) { 26 System.out.println(".............Failure"); 27 } 28 }); 29 } catch (Exception e) { 30 e.printStackTrace(); 31 } 32 }
AsyncHttpClient的post方式:
與get相似,只需要傳參數進去,用RequestParams類傳參數
RequestParams params = new RequestParams(); params.put("username", username); params.put("pwd", password);
完整的方法:
1 public static void requestNewForPostLogin(final Context context, final Handler handler, final String username, final String password) { 2 final String path = "http://192.168.0.100:8080/Starry/servlet/LoginServlet"; // Post 3 AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); 4 5 RequestParams params = new RequestParams(); 6 params.put("username", username); 7 params.put("pwd", password); 8 asyncHttpClient.post(path, params, new AsyncHttpResponseHandler() { 9 10 @Override 11 public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { 12 if(statusCode == 200) { 13 try { 14 String result = new String(responseBody,"utf-8"); 15 Toast.makeText(context, result, Toast.LENGTH_SHORT).show(); 16 } catch (UnsupportedEncodingException e) { 17 e.printStackTrace(); 18 } 19 20 } 21 } 22 23 @Override 24 public void onFailure(int statusCode, Header[] headers, 25 byte[] responseBody, Throwable error) { 26 System.out.println(".............Failure"); 27 } 28 }); 29 }
