Android之實戰篇(三)

來源:互聯網
上載者:User

標籤:android   blog   http   os   io   java   strong   ar   for   

先給出我們用到的工具類

1.發送請求的工具類

本執行個體採用HttpClient與伺服器通訊,用到了一個工具類對Httpclient進行封裝:定義了兩個方法來發送請求

getRequest:發送GET請求

postRequest :發送POST請求

HttpUtil.java(注意IP地址換成自己的IP地址,這個BASE_URL = "http://losthost:8080/auction/,要不然,你就)

[java] view plaincopy
  1. package com.infy.auction.client.util;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import org.apache.http.HttpResponse;  
  9. import org.apache.http.NameValuePair;  
  10. import org.apache.http.client.ClientProtocolException;  
  11. import org.apache.http.client.HttpClient;  
  12. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  13. import org.apache.http.client.methods.HttpGet;  
  14. import org.apache.http.client.methods.HttpPost;  
  15. import org.apache.http.impl.client.DefaultHttpClient;  
  16. import org.apache.http.message.BasicNameValuePair;  
  17. import org.apache.http.util.EntityUtils;  
  18.   
  19. import android.util.Log;  
  20.   
  21. public class HttpUtil {  
  22.   
  23.     private static final String TAG="HttpUtil";  
  24.     //建立HttpCilent對象  
  25.     public static HttpClient httpClient = new DefaultHttpClient();  
  26.     public static final String BASE_URL = "http://losthost:8080/auction/";  
  27.       
  28.     //發送url的請求,伺服器響應字串  
  29.     public static String getRequest(String url) throws  Exception{  
  30.         //建立一個HttpGet對象  
  31.         HttpGet get = new HttpGet(url);  
  32.         //發送GET請求  
  33.         HttpResponse httpResponse = httpClient.execute(get);  
  34.         Log.i(TAG, "getReq ==getStatusCode--->" +httpResponse.getStatusLine().getStatusCode());  
  35.         //如果伺服器成功地返迴響應  
  36.         if(httpResponse.getStatusLine().getStatusCode() == 200){  
  37.             //擷取響應的字串  
  38.             String result = EntityUtils.toString(httpResponse.getEntity());  
  39.             Log.i(TAG, "getReq ==result--->" +result);  
  40.             return result;  
  41.         }  
  42.         return "";  
  43.     }  
  44.     //發送Post請求  
  45.     public static String postRequest(String url,Map<String, String> rawParams) throws Exception{  
  46.       
  47.         Log.i(TAG, "postRequest--->begin");  
  48.           
  49.         //建立HttpPost對象  
  50.         HttpPost post = new HttpPost(url);  
  51.         //如果傳遞的參數個數比較多,可以對傳遞的參數進行封裝  
  52.         List<NameValuePair> params = new ArrayList<NameValuePair>();  
  53.         for(String key:rawParams.keySet()){  
  54.             //封裝請求的參數  
  55.             params.add(new BasicNameValuePair(key, rawParams.get(key)));  
  56.               
  57.         }  
  58.         //佈建要求的參數  
  59.         post.setEntity(new UrlEncodedFormEntity(params,"utf-8"));  
  60.         //發送Post請求  
  61.         HttpResponse httpResponse = httpClient.execute(post);  
  62.         //如果伺服器成功地返迴響應  
  63.         Log.i(TAG, "HttpL---->" +httpResponse.getStatusLine().getStatusCode());  
  64.         if(httpResponse.getStatusLine().getStatusCode() == 200){  
  65.             //擷取響應的字串  
  66.             String result = EntityUtils.toString(httpResponse.getEntity());  
  67.             Log.i(TAG, "response-->" +result);  
  68.             return result;  
  69.         }  
  70.           
  71.     return null;      
  72.     }  
  73.       
  74. } 

 

 

 

 

顯示各種對話方塊的工具類:Dialog.xml

[java] view plaincopy
  1. package com.infy.auction.client.util;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.content.Context;  
  6. import android.content.DialogInterface;  
  7. import android.content.DialogInterface.OnClickListener;  
  8. import android.view.View;  
  9.   
  10. public class DialogUtil {  
  11.   
  12.     //定義一個顯示訊息的對話方塊  
  13.     public static void showDialog(final Context ctx , String msg, boolean closeSelf) {  
  14.         // TODO Auto-generated method stub  
  15.         //建立一個AlertDialog.Builder對象  
  16.         AlertDialog.Builder builder = new AlertDialog.Builder(ctx).setMessage(msg).setCancelable(false);  
  17.           
  18.         if(closeSelf){  
  19.             builder.setPositiveButton("確定", new OnClickListener() {  
  20.                   
  21.                 @Override  
  22.                 public void onClick(DialogInterface dialog, int which) {  
  23.                     // TODO Auto-generated method stub  
  24.                 //結束當前Activity  
  25.                     ((Activity)ctx).finish();  
  26.                 }  
  27.             });  
  28.         }else{  
  29.             builder.setPositiveButton("確定", null);  
  30.         }  
  31.         builder.create().show();  
  32.     }  
  33.   
  34.     //定義一個顯示指定組件的對話方塊  
  35.     public static void showDialog(Context ctx,View view){  
  36.         AlertDialog.Builder  builder = new AlertDialog.Builder(ctx).setView(view).setCancelable(false).setPositiveButton("確定", null);  
  37.         builder.create().show();  
  38.     }  
  39. } 

Android之實戰篇(三)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.