qq_36702839
qq_36702839
采纳率31.3%
2017-03-23 03:47 阅读 1.0k

请教HttpClient用法!!!请教HttpClient用法!!!

有人可以说说这个是怎么用的么,跟我的记忆中的httpclient有点出入。。。
原本是想设置http超时时间的,可是这个httpclient。。。
用下面的httpclient,该怎么设置超时时间呢???

 HttpClient httpClient = new HttpClient() {

            @Override
            public HttpParams getParams() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public ClientConnectionManager getConnectionManager() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public <T> T execute(HttpHost arg0, HttpRequest arg1, ResponseHandler<? extends T> arg2, HttpContext arg3)
                    throws IOException, ClientProtocolException {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public <T> T execute(HttpHost arg0, HttpRequest arg1, ResponseHandler<? extends T> arg2)
                    throws IOException, ClientProtocolException {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public <T> T execute(HttpUriRequest arg0, ResponseHandler<? extends T> arg1, HttpContext arg2)
                    throws IOException, ClientProtocolException {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public HttpResponse execute(HttpHost arg0, HttpRequest arg1, HttpContext arg2)
                    throws IOException, ClientProtocolException {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public <T> T execute(HttpUriRequest arg0, ResponseHandler<? extends T> arg1)
                    throws IOException, ClientProtocolException {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public HttpResponse execute(HttpHost arg0, HttpRequest arg1) throws IOException, ClientProtocolException {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public HttpResponse execute(HttpUriRequest arg0, HttpContext arg1) throws IOException, ClientProtocolException {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public HttpResponse execute(HttpUriRequest arg0) throws IOException, ClientProtocolException {
                // TODO Auto-generated method stub
                return null;
            }
        };
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • u014804892 失路英雄_17 2017-03-23 06:15

    public String PostObject(final String url, final T object, final Class objectClass) {
    DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
    //设置连接池超时

    defaultHttpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 15000);
    defaultHttpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 15000);
    HttpPost httpPost = new HttpPost(url);
    try {
    StringEntity stringEntity = new StringEntity(new GsonBuilder().create().toJson(object));
    httpPost.setEntity(stringEntity);
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader("Content-type", "application/json");
    httpPost.setHeader("Accept-Encoding", "gzip");

            HttpResponse httpResponse = defaultHttpClient.execute(httpPost);
            if (httpResponse.getStatusLine().getStatusCode() != 200) {
                return "StatusCode = "+httpResponse.getStatusLine().getStatusCode();
            }
    
            String hashStrings=httpResponse.getStatusLine().getReasonPhrase();//获取远程端的MD5hash值
            String hashString=hashStrings.substring(0,hashStrings.indexOf('|'));
            SdCardConstant.setFileName(hashStrings.substring(hashStrings.indexOf('|')+1,hashStrings.length()));//保存文件名
            HttpEntity httpEntity = httpResponse.getEntity();
            if (httpEntity != null) {
                InputStream inputStream = httpEntity.getContent();
                Header contentEncoding = httpResponse.getFirstHeader("Content-Encoding");
                if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                    inputStream = new GZIPInputStream(inputStream);
                }
                if (!SdCardConstant.getFilePath().exists()) {
                    SdCardConstant.getFilePath().mkdir();
                }
                File file = new File(SdCardConstant.getFilePath(),SdCardConstant.getFileName()); 
                if (file.exists()) {
                    FileUtils.deleteFiles(file);
                }
                file.createNewFile();
                saveToFile(file, inputStream);
                inputStream.close();
                FileInputStream in = new FileInputStream(file);
                byte[] data = new byte[in.available()];
                in.read(data);
                String md5HashString =getMd5(data);//下载的文件计算的MD5hash值
                String result=null;
                if (hashString.equalsIgnoreCase(md5HashString)) {
                    result="Download Finished";
                }else {
                    result="Download Failed";
                }
                System.out.println("MD5:"+result);
                in.close();
                return result;
            }
            return null;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return e.getMessage();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            return e.getMessage();
        } catch (IOException e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }
    
    点赞 评论 复制链接分享
  • shayubuhuifei 鲨鱼不会飞 2017-03-23 08:08

    new 不出来楼主的这个HttpClient,话说可以考虑其他的么?HttpURLConnection

    点赞 评论 复制链接分享
  • fcwxin fcwxin 2017-03-23 08:18

    public void post() {
    try {
    String url = "https://reg.163.com/logins.jsp";
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    /*post.addHeader("Content-Type", "application/json");//设定 请求格式 json,也可以设定xml格式
    JSONObject obj = new JSONObject();
    obj.put("password", "1234567");
    obj.put("phone", "13312345678");
    obj.put("token", "");
    */
    List list = new ArrayList();
    list.add(new BasicNameValuePair("id", "helloword"));
    list.add(new BasicNameValuePair("pwd", "android"));
    HttpEntity entity;
    entity = new UrlEncodedFormEntity(list, "utf-8");
    post.setEntity(entity);
    //post.setEntity(new StringEntity(obj.toString()));

            HttpResponse response = client.execute(post);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entitys = response.getEntity();// 获得相应实体
                String msg;
                msg = EntityUtils.toString(entitys).trim();
                postCon = msg;
                Log.i("post成功--", msg);
    
                handler.sendEmptyMessage(1);
            } else {
                Log.i("post失败--", "失败"
                        + response.getStatusLine().getStatusCode());
                handler.sendEmptyMessage(1);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }/* catch (JSONException e) {
            e.printStackTrace();
        }*/
    
    }
    
    点赞 评论 复制链接分享
  • sxchen_csdn 凌岩歌-贰拾肆 2017-03-24 06:30

相关推荐