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 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题