【问题标题】:Apache httpclient android dealing with connection refusedApache httpclient android处理连接被拒绝
【发布时间】:2015-03-23 21:06:29
【问题描述】:

我正在开发一个意图服务,它将对服务器执行 httpget 并检索 json 字符串。

我正在使用以下代码,只要服务器启动,它就可以工作:

 private String getServerContentsJson() throws IOException {
    HttpClient httpclient = new DefaultHttpClient();

   HttpResponse  response = httpclient.execute(new HttpGet(url));


    StatusLine statusline = response.getStatusLine();

    if(statusline.getStatusCode() == HttpStatus.SC_OK){

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        response.getEntity().writeTo(out);
        String responseString = out.toString();
        out.close();
        return responseString;}

    else{
        //Closes the connection.
        response.getEntity().getContent().close();
        throw new IOException(statusline.getReasonPhrase());

    }

问题是当服务器关闭时,我想正确处理它,但我不清楚如何做到这一点。根据文档,httpclient.execute 要么返回正确的响应,要么抛出 IOException 或 ClientProtocolException,但我得到的 HttpHostConnectException: Connection to http://irrelevantURL refused 似乎无法追溯到我的代码。然后应用程序崩溃。有什么建议吗?

这是完整的堆栈跟踪:

W/System.err﹕ org.apache.http.conn.HttpHostConnectException: Connection to http://irrelevantURL refused
W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)

编辑:我猜这与服务器关闭无关,而是由于某种原因它拒绝连接,问题仍然存在,尽管我仍然想正确处理这个问题,以便应用程序不会崩溃

【问题讨论】:

    标签: java android http-get


    【解决方案1】:

    HttpHostConnectExceptionIOException 的一种类型,您没有正确处理它。你应该有类似下面的东西。如果 status_code 不是 200,您可能不想抛出异常,而是返回有意义的错误 responseString

      private String getServerContentsJson()
        throws IOException {
        HttpClient httpclient = new DefaultHttpClient();
    
        String responseString = null;
        HttpResponse response = null;
        try {
          response = httpclient.execute(new HttpGet(url));
    
          StatusLine statusline = response.getStatusLine();
    
          if (statusline.getStatusCode() == HttpStatus.SC_OK) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            responseString = out.toString();
            out.close();
            return responseString;
          } else {
            responseString = statusline.getReasonPhrase();
          }
        } catch (IOException e) {
          throw new IOException(e.getMessage(), e);
          //OR:
          //responseString = "Could not reach server, please try again";
        } finally {
          if (response != null) {
            response.getEntity().consumeContent();
          }
        }
        return responseString;
      }
    

    【讨论】:

    • 您好,感谢您的回答,我想我现在可以正常工作了,但是在这里使用您的示例,我得到了 finally 块中已经使用的内容的异常。我在文档中阅读了一些内容,并替换了 response.getEntity().getContent().close();与 response.getEntity().consumeContent();哪个有效,但也只是评论整行有效,所以我想这里的问题是这是否释放了所有资源?
    • 你是对的,请参阅this answer。我已经更新了代码。此外,正如那里所建议的那样,EntityUtils.consume(response.getEntity()); 可能也可以工作,并且是首选方式。如果您测试它,请删除更新,我会更新代码
    猜你喜欢
    • 2013-01-16
    • 1970-01-01
    • 2016-05-08
    • 2017-09-01
    • 2018-02-11
    • 2011-08-07
    • 2020-10-01
    • 2016-11-11
    • 1970-01-01
    相关资源
    最近更新 更多