简体   繁体   English

Apache HTTPClient android处理连接被拒绝

[英]Apache httpclient android dealing with connection refused

I'm working on an intentservice that will do a httpget to a server and retrieve a json string. 我正在开发一个intentservice,它将对服务器执行httpget并检索json字符串。

I'm using the following code, which works aslong as the server is up: 我正在使用以下代码,只要服务器启动,该代码即可工作:

 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());

    }

The problem is when the server is down, I want to deal with that properly, but it is unclear to me how I can do this. 问题是当服务器关闭时,我想妥善处理,但是我不清楚如何处理。 According to the documentation httpclient.execute will either return a proper response, or throw IOException or ClientProtocolException, but instead I'm getting HttpHostConnectException: Connection to http://irrelevantURL refused which doesn't seem to trace back to my code. 根据文档, httpclient.execute将返回正确的响应,或者抛出IOException或ClientProtocolException,但我却收到了HttpHostConnectException: Connection to http://irrelevantURL refused ,这似乎无法追溯到我的代码。 The app then crashes. 该应用程序随后崩溃。 Any suggestions? 有什么建议么?

Here's the full stack trace: 这是完整的堆栈跟踪:

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)

edit: I guess this isn't related to server being down, but instead it refuse connection for some reason, the question remains though I still want to deal with this properly so that the app does not crash 编辑:我想这与服务器关闭无关,但由于某种原因它拒绝连接,问题仍然存在,尽管我仍然想正确处理,以便应用程序不会崩溃

HttpHostConnectException is a type of IOException , you are not handling it properly. HttpHostConnectExceptionIOException ,您没有正确处理它。 You should have something like below. 您应该有类似下面的内容。 You probably do not want to throw a Exception if status_code is not 200, instead return a meaningful error responseString 如果status_code不是200,则可能不希望引发Exception,而是返回有意义的错误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;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM