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

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();
    }
}
qq_36702839
qq_36702839 谢谢,非常的详细~~可是,还是看不太明白,有没有简略点的,本人菜鸟一个,智商有点不够用~~~
2 年多之前 回复

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

qq_36702839
qq_36702839 我也不知道为什么会new出这么强大的东西,555555555~~。HttpURLConnection这个也试过,还是new出跟上面那个差不多的~~还是不会~~智商不够啊~~
2 年多之前 回复

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();
    }*/

}
qq_36702839
qq_36702839 这个也是太高大上了,重要问题是我new不出你们这些的~~~~~~~~~~~~~~~~~
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!