qq_36702839 2017-03-23 03:47 采纳率: 31.3%
浏览 1070

请教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条回答

  • 失路英雄_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();
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图