• android Http 工具类


      1 package com.phone;
      2 
      3 import java.io.ByteArrayOutputStream;
      4 import java.io.IOException;
      5 import java.io.InputStream;
      6 import java.io.UnsupportedEncodingException;
      7 import java.util.ArrayList;
      8 import java.util.List;
      9 import java.util.Map;
     10 
     11 import org.apache.http.HttpResponse;
     12 import org.apache.http.NameValuePair;
     13 import org.apache.http.client.HttpClient;
     14 import org.apache.http.client.entity.UrlEncodedFormEntity;
     15 import org.apache.http.client.methods.HttpGet;
     16 import org.apache.http.client.methods.HttpPost;
     17 import org.apache.http.impl.client.DefaultHttpClient;
     18 import org.apache.http.message.BasicNameValuePair;
     19 import org.apache.http.protocol.HTTP;
     20 import org.apache.http.util.EntityUtils;
     21 import org.json.JSONObject;
     22 import android.util.Log;
     23 
     24 public class HttpUtil {
     25     //创建HttpClient对象
     26     public static HttpClient httpClient =  new DefaultHttpClient();
     27     public static final String  BASE_URL = "http://10.254.31.110/blog_client_pack.php";
     28     
     29     /**
     30      * 
     31      * @param url 发送请求的url
     32      * @return String 服务器响应字符串
     33      * @throws Exception
     34      */
     35     public static String getRequest(String url ) throws Exception{
     36         if(url.equals(null)){
     37             url = BASE_URL;
     38         }
     39         Log.d("http", "-------url--------"+url);
     40         HttpGet get = new HttpGet(url);
     41         HttpResponse httpResponse = httpClient.execute(get);
     42         if(httpResponse.getStatusLine().getStatusCode() == 200 ){
     43             String result = EntityUtils.toString(httpResponse.getEntity());
     44 //            Log.d("http", "-------get--------"+result.length()+";result:"+result);
     45             return result;
     46         }
     47         return null;
     48     }
     49     
     50     /**
     51      * 
     52      * @param url 发送请求的url
     53      * @param rawParams 请求参数
     54      * @return String 服务器响应字符串
     55      * @throws Exception
     56      */
     57     public static String postRequest(String url, Map<String,String> rawParams ) throws Exception{
     58         if(url.equals(null)){
     59             url = BASE_URL;
     60         }
     61         //创建HttpPost对象
     62         HttpPost post = new HttpPost(url);
     63         //如果传递参数个数比较多,可以对传递的参数进行封装
     64         List<NameValuePair> params = new ArrayList<NameValuePair>();
     65         for(String key:rawParams.keySet()){
     66             //封装请求参数
     67             params.add(new BasicNameValuePair(key, rawParams.get(key)));
     68         }
     69 
     70         Log.d("http", params.toString());
     71         //设置请求参数
     72         post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
     73         //发送post请求
     74         HttpResponse httpResponse = httpClient.execute(post);
     75         //如果服务器成功地返回响应
     76         if(httpResponse.getStatusLine().getStatusCode() == 200 ){
     77             //获取服务器响应字符串
     78             String result = EntityUtils.toString(httpResponse.getEntity());
     79             Log.d("http", "-------post--------"+result.length());
     80             return result;
     81         }    
     82         return null;
     83     }
     84     
     85     private String getData( InputStream inputStream ) throws Exception{
     86         String data = "";
     87         //内存缓冲区
     88         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
     89         int len = -1;
     90         byte[] buf = new byte[1024];
     91         try{
     92             while((len = inputStream.read(buf))!= -1 ){
     93                 outputStream.write(buf,0,len);
     94             }
     95             byte[] bytes = outputStream.toByteArray();
     96             data = new String(bytes);
     97         } catch( IOException e){
     98             e.printStackTrace();
     99         }
    100         return data;
    101     }
    102 }
  • 相关阅读:
    oracle递归层级查询 start with connect by prior
    C 常量指针和指针常量
    C 字符串常量和字符串变量定义和区别
    C 指针改变变量值
    UITableViewCell的高度与UILabel自适应
    手动开启ARC
    SWIFT模糊效果
    SWIFT中使用AFNetwroking访问网络数据
    MAC机中安装RUBY环境
    CocoaPods安装和使用教程
  • 原文地址:https://www.cnblogs.com/lsl8966/p/2870755.html
Copyright © 2020-2023  润新知