设为首页 加入收藏

TOP

android开发,http工具类
2015-07-24 11:55:06 来源: 作者: 【 】 浏览:4
Tags:android 开发 http 工具

android的HttpClient实现简单的get和post请求

?

[1].[代码] [Java]代码 跳至 [1]

/**

 *
 Http工具类

 */

public

class 
HttpUtil {

    //
 创建HttpClient对象

    public

static 
HttpClient httpClient = new

DefaultHttpClient();

    public

static 
final 
String BASE_URL = "";

 

    /**

     *
 get请求

     *

     *
 @param url

     *           
 发送请求的URL

     *
 @return 服务器响应字符串

     *
 @throws Exception

     */

    public

static 
String doGet(String url) throws

Exception {

        //
 创建HttpGet对象。

        HttpGet
 get = new

HttpGet(url);

        //
 发送GET请求

        HttpResponse
 httpResponse = httpClient.execute(get);

        //
 如果服务器成功地返回响应

        if

(httpResponse.getStatusLine().getStatusCode() == 200)
 {

            //
 获取服务器响应字符串

            HttpEntity
 entity = httpResponse.getEntity();

            InputStream
 content = entity.getContent();

            return

convertStreamToString(content);

        }

        return

null;

    }

 

    /**

     *
 post请求

     *

     *
 @param url

     *           
 发送请求的URL

     *
 @param params

     *           
 请求参数

     *
 @return 服务器响应字符串

     *
 @throws Exception

     */

    public

static 
String doPost(String url, Map rawParams)

            throws

Exception {

        //
 创建HttpPost对象。

        HttpPost
 post = new

HttpPost(url);

        //
 如果传递参数个数比较多的话可以对传递的参数进行封装

        List
 params = new

ArrayList();

        for

(String key : rawParams.keySet()) {

            //
 封装请求参数

            params.add(new

BasicNameva luePair(key, rawParams.get(key)));

        }

        //
 设置请求参数

        post.setEntity(new

UrlEncodedFormEntity(params, "utf-8"));

        //
 发送POST请求

        HttpResponse
 httpResponse = httpClient.execute(post);

        //
 如果服务器成功地返回响应

        if

(httpResponse.getStatusLine().getStatusCode() == 200)
 {

            //
 获取服务器响应字符串

            HttpEntity
 entity = httpResponse.getEntity();

            InputStream
 content = entity.getContent();

            return

convertStreamToString(content);

        }

        return

null;

    }

 

    /**

     *
 获取服务器的响应,转换为字符串

     */

    private

static 
String convertStreamToString(InputStream is) {

        BufferedReader
 reader = new

BufferedReader(new

InputStreamReader(is));

        StringBuilder
 sb = new

StringBuilder();

        String
 line = null;

        try

{

            while

((line = reader.readLine()) != null)
 {

                sb.append(line);

            }

        }
catch

(IOException e) {

            e.printStackTrace();

        }
finally

{

            try

{

                is.close();

            }
catch

(IOException e) {

                e.printStackTrace();

            }

        }

        return

sb.toString();

    }

}

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇10.2.0.1.1gridcontrol的启动和关.. 下一篇hibernate的org.dom4j.DocumentEx..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·如何理解智能指针? (2025-12-24 12:48:26)
·c++是否应避免使用普 (2025-12-24 12:48:23)
·如何通俗易懂学会 C+ (2025-12-24 12:48:21)
·在 C 语言函数中,如 (2025-12-24 12:19:41)
·C盘里面的AppData文 (2025-12-24 12:19:38)