Android利用HttpClient和Post与Get请求与服务器交互

【版权声明:本文为新价值网原创,未经准许谢绝转载。如需转载,请务必在转载时注明本博客地址。】

代码如下:

  1. import org.apache.http.HttpResponse;   
  2. import org.apache.http.HttpStatus;   
  3. import org.apache.http.NameValuePair;   
  4. import org.apache.http.client.HttpClient;   
  5. import org.apache.http.client.entity.UrlEncodedFormEntity;   
  6. import org.apache.http.client.methods.HttpGet;   
  7. import org.apache.http.client.methods.HttpPost;   
  8. import org.apache.http.impl.client.DefaultHttpClient;   
  9. import org.apache.http.protocol.HTTP;   
  10. import org.apache.http.util.EntityUtils;   
  11. import java.io.BufferedReader;   
  12. import java.io.InputStreamReader;   
  13. import java.util.List;   
  14. public class HttpUtils {   
  15.     /**   
  16.      *   
  17.      * @param url地址   
  18.      * @return 返回网页内容   
  19.      * @throws Exception   
  20.      */   
  21.     public static String get(String url) throws Exception {   
  22.         HttpClient client = new DefaultHttpClient();   
  23.         HttpGet get = new HttpGet(url);   
  24.         HttpResponse response = client.execute(get);   
  25.         BufferedReader reader = new BufferedReader(new InputStreamReader(   
  26.                     response.getEntity().getContent()));   
  27.         String resStr = "";   
  28.         for (String s = reader.readLine(); s != null; s = reader.readLine()) {   
  29.             resStr += s;   
  30.         }   
  31.         return resStr;   
  32.     }   
  33.     /**   
  34.      *   
  35.      * @param url 网页地址   
  36.      * @param params 参数   
  37.      * @return 返回网页内容   
  38.      * @throws Exception   
  39.      */   
  40.     public static String post(String url, List<NameValuePair> params)   
  41.         throws Exception {   
  42.         String response = null;   
  43.         HttpClient httpclient = new DefaultHttpClient();   
  44.         // 创建HttpPost对象   
  45.         HttpPost httppost = new HttpPost(url);   
  46.         try {   
  47.             // 设置httpPost请求参数,设置字符集   
  48.             httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));   
  49.             // 使用execute方法发送HTTP Post请求,并返回HttpResponse对象   
  50.             HttpResponse httpResponse = httpclient.execute(httppost);   
  51.             int statusCode = httpResponse.getStatusLine().getStatusCode();   
  52.             if (statusCode == HttpStatus.SC_OK) {   
  53.                 // 获得返回结果   
  54.                 response = EntityUtils.toString(httpResponse.getEntity());   
  55.             } else {   
  56.                 response = "请求错误,返回码:" + statusCode;   
  57.             }   
  58.         } catch (Exception e) {   
  59.             e.getMessage();   
  60.         }   
  61.         return response;   
  62.     }   
  63. }   

测试:

  1. String data;   
  2.    
  3. Gett方式   
  4. data =   
  5. HttpUtils.get("http://10.0.2.2:8080/Auction/LoginAction.action?username="+   
  6. username + "&password=" + password + "");   
  7.    
  8. Post方式   
  9.    List<NameValuePair> params = new ArrayList<NameValuePair>();   
  10.    params.add(new BasicNameValuePair("username", username));   
  11.    params.add(new BasicNameValuePair("password", password));   
  12.    data = HttpUtils.post(   
  13.    "http://10.0.2.2:8080/Auction/LoginAction.action",   
  14.    params);  

    如有大家有什么问题,可以给我留言,我会抽时间一一解答。

    笔者微博:@LeaveBugsAway欢迎叨扰。

你可能感兴趣的:(httpclient)