1. HttpUtils 該工具類應用於Android客戶端+Web服務器
/** * */ package com.nubb.auction.client.util; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; /** * Description:利用Url請求Json格式的數據 * * @author maoyun0903@163.com * @version 1.0 */ public class HttpUtil { // 創建HttpClient對象 public static HttpClient httpClient = new DefaultHttpClient(); public static final String BASE_URL = "http://192.168.1.11:8080/auction/android/"; /** * * @param url 發送請求的URL * @return 服務器響應字符串 * @throws Exception */ public static String getRequest(final String url) throws Exception { FutureTask<String> task = new FutureTask<String>( new Callable<String>() { @Override public String call() throws Exception { // 創建HttpGet對象。 HttpGet get = new HttpGet(url); // 發送GET請求 HttpResponse httpResponse = httpClient.execute(get); // 如果服務器成功地返回響應 if (httpResponse.getStatusLine() .getStatusCode() == 200) { // 獲取服務器響應字符串 String result = EntityUtils .toString(httpResponse.getEntity()); return result; } return null; } }); new Thread(task).start(); return task.get(); } /** * @param url 發送請求的URL * @param params 請求參數 * @return 服務器響應字符串 * @throws Exception */ public static String postRequest(final String url , final Map<String ,String> rawParams)throws Exception { FutureTask<String> task = new FutureTask<String>( new Callable<String>() { @Override public String call() throws Exception { // 創建HttpPost對象。 HttpPost post = new HttpPost(url); // 如果傳遞參數個數比較多的話可以對傳遞的參數進行封裝 List<NameValuePair> params = new ArrayList<NameValuePair>(); for(String key : rawParams.keySet()) { //封裝請求參數 params.add(new BasicNameValuePair(key , rawParams.get(key))); } // 設置請求參數 post.setEntity(new UrlEncodedFormEntity( params, "gbk")); // 發送POST請求 HttpResponse httpResponse = httpClient.execute(post); // 如果服務器成功地返回響應 if (httpResponse.getStatusLine() .getStatusCode() == 200) { // 獲取服務器響應字符串 String result = EntityUtils .toString(httpResponse.getEntity()); return result; } return null; } }); new Thread(task).start(); return task.get(); } }
2. DiaLogUtils 顯示指定組件的對話框
/** * */ package com.nubb.auction.client.util; import com.nubb.auction.client.AuctionClientActivity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.DialogInterface.OnClickListener; import android.view.View; /** * Description:顯示指定組件的對話框,並跳轉至指定的Activity * @author maoyun0903@163.com * @version 1.0 */ public class DialogUtil { // 定義一個顯示消息的對話框 public static void showDialog(final Context ctx , String msg , boolean goHome) { // 創建一個AlertDialog.Builder對象 AlertDialog.Builder builder = new AlertDialog.Builder(ctx) .setMessage(msg).setCancelable(false); if(goHome) { builder.setPositiveButton("確定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent i = new Intent(ctx , AuctionClientActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); ctx.startActivity(i); } }); } else { builder.setPositiveButton("確定", null); } builder.create().show(); } // 定義一個顯示指定組件的對話框 public static void showDialog(Context ctx , View view) { new AlertDialog.Builder(ctx) .setView(view).setCancelable(false) .setPositiveButton("確定", null) .create() .show(); } }