1. 程式人生 > >Android 伺服器連線工具類HttpUtil

Android 伺服器連線工具類HttpUtil

/***********************************************
 * 網路工具
 * 
 * @author chen.lin
 * @version 1.0
 ************************************************/
public class HttpUtil extends Activity {

    // 正式更新URL
    public static final String UPDATE_URL = "http://192.168.0.20:8088/android/update/androidapp/";// 120.86.69.225
private static final String TAG = "HttpUtil"; // 獲得Get請求物件request public static HttpGet getHttpGet(String url) { HttpGet request = new HttpGet(url); return request; } // 獲得Post請求物件request public static HttpPost getHttpPost(String url) { HttpPost request = new
HttpPost(url); return request; } // 根據請求獲得響應物件response public static HttpResponse getHttpResponse(HttpGet request) throws ClientProtocolException, IOException { HttpResponse response = new DefaultHttpClient().execute(request); return response; } // 根據請求獲得響應物件response
public static HttpResponse getHttpResponse(HttpPost request) throws ClientProtocolException, IOException { HttpResponse response = new DefaultHttpClient().execute(request); return response; } // 傳送Post請求,獲得響應查詢結果 public static String queryStringForPost(String url) { HttpPost request = HttpUtil.getHttpPost(url); HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 10000); HttpConnectionParams.setSoTimeout(httpParameters, 10000); DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); Logger.i(TAG, url); String result = null; try { // HttpUtil.getHttpResponse(request);//原方法,沒做網路超時處理 HttpResponse response = httpClient.execute(request);// 新方案,做了網路超時設定 // 判斷是否請求成功 if (response.getStatusLine().getStatusCode() == 200) { // 獲得響應 result = EntityUtils.toString(response.getEntity()); return result; } else { Logger.i(TAG, response.getStatusLine().getStatusCode() + ""); } } catch (ClientProtocolException e) { e.printStackTrace(); result = "網路異常!"; return result; } catch (IOException e) { e.printStackTrace(); result = "網路異常!"; return result; } return null; } // 獲得響應查詢結果 public static String queryStringForPost(HttpPost request) { String result = null; try { // 獲得響應物件 HttpResponse response = HttpUtil.getHttpResponse(request); // 判斷是否請求成功 if (response.getStatusLine().getStatusCode() == 200) { // 獲得響應 result = EntityUtils.toString(response.getEntity()); return result; } } catch (ClientProtocolException e) { e.printStackTrace(); result = "網路異常!"; return result; } catch (IOException e) { e.printStackTrace(); result = "網路異常!"; return result; } return null; } // 傳送Get請求,獲得響應查詢結果 public static String queryStringForGet(String url) { // 獲得HttpGet物件 HttpGet request = HttpUtil.getHttpGet(url); String result = null; try { // 獲得響應物件 HttpResponse response = HttpUtil.getHttpResponse(request); // 判斷是否請求成功 if (response.getStatusLine().getStatusCode() == 200) { // 獲得響應 result = EntityUtils.toString(response.getEntity()); return result; } } catch (ClientProtocolException e) { e.printStackTrace(); result = "網路異常!"; return result; } catch (IOException e) { e.printStackTrace(); result = "網路異常!"; return result; } return null; } /** * * @param url * 傳送請求的URL * @return 伺服器響應字串 * @throws Exception */ public static String getRequest(String url) throws Exception { // 建立HttpGet物件。 HttpGet get = new HttpGet(url); HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 3000); HttpConnectionParams.setSoTimeout(httpParameters, 5000); DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); // 傳送GET請求 HttpResponse httpResponse = httpClient.execute(get); // 如果伺服器成功地返回響應 if (httpResponse.getStatusLine().getStatusCode() == 200) { // 獲取伺服器響應字串 String result = EntityUtils.toString(httpResponse.getEntity()); return result; } return null; } /** * 請求遠端伺服器,並封裝引數資訊 * @param url * @param rawParams * @return * @throws Exception */ public static String postRequest(String url, Map<String, String> rawParams) 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))); } //Logger.i(TAG, "params------------------->" + params); // 設定請求引數 post.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 3000); HttpConnectionParams.setSoTimeout(httpParameters, 15000); DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); // 傳送POST請求 HttpResponse httpResponse = httpClient.execute(post); // 如果伺服器成功地返回響應 String result = null; if (httpResponse.getStatusLine().getStatusCode() == 200) { // 獲取伺服器響應字串 result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8"); Logger.i(TAG, "result-------->" + result); } return result; } }