DefaultHttpClient android的java.net.SocketException

时间:2012-11-18 17:28:07

标签: android httpclient socketexception

从我的Android应用程序中,我需要调用一个Web服务来检索一些数据。为了调用该Web服务,我使用HttpPost实体。

问题是我收到了这个错误:

11-18 15:57:42.807: I/ActivityThread(151): finish dispatching broadcast to com.android.server.PowerManagerService$BatteryReceiver@2ffcad08
11-18 15:57:42.807: I/ActivityThread(151): Dispatching broadcast android.intent.action.BATTERY_CHANGED to com.android.internal.policy.impl.KeyguardUpdateMonitor$3@2ff76710
11-18 15:57:42.807: I/ActivityThread(151):   mRegistered=true mCurOrdered=false
11-18 15:57:42.807: I/ActivityThread(151): finish dispatching broadcast to com.android.internal.policy.impl.KeyguardUpdateMonitor$3@2ff76710
11-18 15:57:42.807: I/ActivityThread(151): Dispatching broadcast android.intent.action.BATTERY_CHANGED to com.android.server.connectivity.Tethering$StateReceiver@3002cf58
11-18 15:57:55.207: W/System.err(2327): java.net.SocketException: Connection reset by peer
11-18 15:57:55.217: W/System.err(2327):     at org.apache.harmony.luni.platform.OSNetworkSystem.readSocketImpl(Native Method)
11-18 15:57:55.257: W/System.err(2327):     at org.apache.harmony.luni.platform.OSNetworkSystem.read(OSNetworkSystem.java:358)
11-18 15:57:55.257: W/System.err(2327):     at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:561)
11-18 15:57:55.257: W/System.err(2327):     at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:88)
11-18 15:57:55.257: W/System.err(2327):     at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
11-18 15:57:55.257: W/System.err(2327):     at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
11-18 15:57:55.257: W/System.err(2327):     at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
11-18 15:57:55.267: W/System.err(2327):     at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
11-18 15:57:55.267: W/System.err(2327):     at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
11-18 15:57:55.277: W/System.err(2327):     at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
11-18 15:57:55.277: W/System.err(2327):     at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
11-18 15:57:55.277: W/System.err(2327):     at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
11-18 15:57:55.287: W/System.err(2327):     at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
11-18 15:57:55.287: W/System.err(2327):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:410)
11-18 15:57:55.297: W/System.err(2327):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-18 15:57:55.297: W/System.err(2327):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-18 15:57:55.307: W/System.err(2327):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-18 15:57:55.307: W/System.err(2327):     at SplashScreen.sendJsonauthRegisterSession(SplashScreen.java:241)
11-18 15:57:55.317: W/System.err(2327):     at $SplashScreenTask.doInBackground(SplashScreen.java:587)
11-18 15:57:55.317: W/System.err(2327):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
11-18 15:57:55.337: I/ActivityThread(240): Dispatching broadcast android.intent.action.SERVICE_STATE to com.android.phone.PhoneApp$PhoneAppBroadcastReceiver@2fe4b590
11-18 15:57:55.337: I/ActivityThread(240):   mRegistered=true mCurOrdered=false
11-18 15:57:55.337: I/ActivityThread(240): finish dispatching broadcast to com.android.phone.PhoneApp$PhoneAppBroadcastReceiver@2fe4b590
11-18 15:57:55.347: I/ActivityThread(151): Dispatching broadcast android.intent.action.SERVICE_STATE to android.net.MobileDataStateTracker$MobileDataStateReceiver@3002b098
11-18 15:57:55.347: I/ActivityThread(247): enter process activity msg = 113
11-18 15:57:55.357: I/ActivityThread(151):   mRegistered=true mCurOrdered=false
11-18 15:57:55.357: W/System.err(2327):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-18 15:57:55.357: I/ActivityThread(151): finish dispatching broadcast to android.net.MobileDataStateTracker$MobileDataStateReceiver@3002b098
11-18 15:57:55.357: I/ActivityThread(151): Dispatching broadcast android.intent.action.ANY_DATA_STATE to android.net.MobileDataStateTracker$MobileDataStateReceiver@30023718
11-18 15:57:55.367: I/ActivityThread(151):   mRegistered=true mCurOrdered=false
11-18 15:57:55.367: I/ActivityThread(151): finish dispatching broadcast to android.net.MobileDataStateTracker$MobileDataStateReceiver@30023718
11-18 15:57:55.367: I/ActivityThread(151): Dispatching broadcast android.intent.action.ANY_DATA_STATE to android.net.MobileDataStateTracker$MobileDataStateReceiver@30023f48
11-18 15:57:55.367: I/ActivityThread(151):   mRegistered=true mCurOrdered=false
11-18 15:57:55.367: I/ActivityThread(151): finish dispatching broadcast to android.net.MobileDataStateTracker$MobileDataStateReceiver@30023f48
11-18 15:57:55.367: I/ActivityThread(151): Dispatching broadcast android.intent.action.ANY_DATA_STATE to android.net.MobileDataStateTracker$MobileDataStateReceiver@30024420
11-18 15:57:55.367: I/ActivityThread(151):   mRegistered=true mCurOrdered=false
11-18 15:57:55.367: I/ActivityThread(240): Dispatching broadcast android.intent.action.ANY_DATA_STATE to com.android.phone.PhoneApp$PhoneAppBroadcastReceiver@2fe4b590
11-18 15:57:55.367: I/ActivityThread(240):   mRegistered=true mCurOrdered=false
11-18 15:57:55.367: I/ActivityThread(240): finish dispatching broadcast to com.android.phone.PhoneApp$PhoneAppBroadcastReceiver@2fe4b590
11-18 15:57:55.377: I/ActivityThread(151): finish dispatching broadcast to android.net.MobileDataStateTracker$MobileDataStateReceiver@30024420
11-18 15:57:55.377: I/ActivityThread(151): Dispatching broadcast android.intent.action.ANY_DATA_STATE to android.net.MobileDataStateTracker$MobileDataStateReceiver@300248f8
11-18 15:57:55.377: I/ActivityThread(151):   mRegistered=true mCurOrdered=false
11-18 15:57:55.377: I/ActivityThread(151): finish dispatching broadcast to android.net.MobileDataStateTracker$MobileDataStateReceiver@300248f8
11-18 15:57:55.377: I/ActivityThread(151): Dispatching broadcast android.intent.action.ANY_DATA_STATE to android.net.MobileDataStateTracker$MobileDataStateReceiver@3002b098
11-18 15:57:55.377: I/ActivityThread(151):   mRegistered=true mCurOrdered=false
11-18 15:57:55.387: W/System.err(2327):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-18 15:57:55.387: I/ActivityThread(247): exit process activity msg = 113
11-18 15:57:55.397: W/System.err(2327):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
11-18 15:57:55.397: I/ActivityThread(151): finish dispatching broadcast to android.net.MobileDataStateTracker$MobileDataStateReceiver@3002b098
11-18 15:57:55.427: W/System.err(2327):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
11-18 15:57:55.427: W/System.err(2327):     at java.lang.Thread.run(Thread.java:1096)

此行的错误在以下方法中:

response = client.execute(post);


public boolean sendJsonauthRegisterSession(JSONObject json, String url) {

    HttpParams httpParameters = new BasicHttpParams();
    int timeoutConnection = 150000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 
    int timeoutSocket = 150000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    DefaultHttpClient client = new DefaultHttpClient(httpParameters);

    HttpResponse response;
    try {
        HttpPost post = new HttpPost(url);
        JSONObject js = new JSONObject();
        js.put("request_object", json);

        StringEntity se = new StringEntity(js.toString());
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
                "application/json"));
        post.setEntity(se);
        response = client.execute(post);

        /* Checking response */
        if (response != null) {
                    ...
        }
     } 
      catch(UnknownHostException e5){
          e5.printStackTrace();           
          this.callRegister="register_failed";
     } 
      catch(SocketException ex) {
          ex.printStackTrace();
          this.callRegister="register_failed";
     } 
      catch (Exception e) {
          e.printStackTrace();
          this.callRegister="register_failed";
     }

    client.getConnectionManager().shutdown();
    return registersession;
}

该方法是从AsyncTask调用的。

任何人都知道我做错了什么?谢谢!!!

重要与服务器的连接是在3G,EDGE上完成的...所以它非常慢。

1 个答案:

答案 0 :(得分:1)

只是关闭连接的服务器。

摘自this question

  

这非常致命。远程服务器向您发送了一个RST数据包,表明连接立即丢失,而不是通常的握手。这绕过了正常的半闭状态转换。我喜欢这个描述:

     
    

“通过对等方重置连接”是TCP / IP等同于将电话重新关闭。它更礼貌而不仅仅是不回复,留下一个悬挂。但这并不是真正礼貌的TCP / IP逆转所期望的FIN-ACK。