android HttpClient访问网络获取数据

使用Apache提供的HttpClient接口同样可以进行HTTP操作。


private static String request(String url, HttpMethod method, List<BasicNameValuePair> params) throws IOException {

              String json = null ;
              BufferedReader reader = null ;

              HttpClient client = null ;
               try {
                     client = new DefaultHttpClient();
                     HttpParams httpParams = client.getParams();
                     httpParams.setParameter(CoreProtocolPNames. USE_EXPECT_CONTINUE ,  false );

                     HttpUriRequest request = getRequest(url, params, method);

                     Log. d( TAG ,  "Sending:" + request.getURI() + " params:" + params.toString());

                     HttpResponse response = client.execute(request);
                      if (response.getStatusLine().getStatusCode() == HttpStatus. SC_OK ) {
                           reader = new BufferedReader( new InputStreamReader(response
                                         .getEntity().getContent()));
                           StringBuilder sb = new StringBuilder();
                            for (String s = reader.readLine(); s != null ; s = reader
                                         .readLine()) {
                                  sb.append(s);
                           }
                           json = sb.toString();

                           Log. d( TAG , json);
                     } else {

                           Log. e( TAG , "Network response error:" + response.getStatusLine().toString());
                            return null ;

                     }
              } catch (ClientProtocolException e) {
                     Log. e( TAG , e.getMessage(), e);
                      return null ;
              } catch (IOException e) {
                     Log. e( TAG , e.getMessage(), e);
                      throw e;
              } finally {
                      try {
                            if (reader != null ) {
                                  reader.close();
                           }

                            if (client != null ) {

                                  client.getConnectionManager().shutdown();
                           }

                     } catch (IOException e) {
                            // ignore me
                            throw e;
                     }
              }
               return json;

       }

        private static HttpUriRequest getRequest(String url,
                     List<BasicNameValuePair> params, HttpMethod method) {
               if (method.equals(HttpMethod. POST )) {

                      try {
                           UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params,
                                          "UTF-8" );
                           HttpPost request = new HttpPost(url);
                           request.setEntity(entity);
                            return request;
                     } catch (UnsupportedEncodingException e) {
                            // Should not come here, ignore me.
                            throw new java.lang.RuntimeException(e.getMessage(), e);
                     }
              } else {

                      // 对参数编码
                     String param = URLEncodedUtils. format(params, "UTF-8" );

                     HttpGet request = new HttpGet(url + "?" + param);
                      return request;
              }
       }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值