2012-05-21 73 views
2

我的應用程序與Facebook協同工作,並使用JSON參數發佈請求。 請求被髮送〜7-8分鐘,然後我發現一個SocketTimeOutException。 10分鐘後,我可以再次發送請求。發送HttpPost請求時發生SocketTimeOutException

下面的代碼:

public static String httpTPRequest(String url, String params) { 
    String response = Static.EMPTY; 
    HttpPost httpPost = new HttpPost(url); 
    httpPost.addHeader("Content-Type", "application/json"); 
    HttpParams httpParams = new BasicHttpParams(); 
    DefaultHttpClient httpClient = new DefaultHttpClient(httpParams); 
    HttpConnectionParams.setConnectionTimeout(httpParams, HTTP_TIMEOUT); 
    HttpConnectionParams.setSoTimeout(httpParams, HTTP_TIMEOUT); 

    try { 
     httpPost.setEntity(new StringEntity(params, "UTF-8")); 
     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity entity = httpResponse.getEntity(); 
     if (entity != null) { 
      response = EntityUtils.toString(entity); 
     } 
    } catch (Exception e) { 
     Debug.log(TAG, "tp server request:" + e.getMessage()); 
    } 

    return response; 
} 

爲什麼會發生?

堆棧跟蹤

SocketTimeoutException:Http::Socket stack:org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(Native Method) 
Http::Socket stack: org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocket(OSNetworkSystem.java:131) 
Http::Socket stack: org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:247) 
Http::Socket stack: org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:535) 
Http::Socket stack: java.net.Socket.connect(Socket.java:1054) 
Http::Socket stack: org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:117) 
Http::Socket stack: org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:129) 
Http::Socket stack: org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 
Http::Socket stack: org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 
Http::Socket stack: org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348) 
Http::Socket stack: org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 
Http::Socket stack: org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 
Http::Socket stack: org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 
Http::Socket stack: com.saymeow.saymeow.utils.Http.httpTPRequest(Http.java:194) 
Http::Socket stack: java.lang.Thread.run(Thread.java:1096) 
Http::Socket stack: com.saymeow.saymeow.requests.ApiRequest$1.run(ApiRequest.java:83) 
Http::SocketTimeoutException: Socket is not connected 
+0

請加上SocketTimeOutException –

+0

什麼的堆棧跟蹤你的網址是? params是什麼樣的? 也許Facebook沒有回答。 你可以嘗試用curl手動創建相同的請求嗎? –

+0

我不僅使用此代碼的Facebook。我的網址是http://api.verumnets.ru/?v=1,params是{「data」:「limit」:40},「service」:「feedInbox」} –

回答

-1

如果params爲一個JSON字符串,你可能需要更換:

httpPost.setEntity(new StringEntity(params,"UTF-8")); 

httpPost.setEntity(new ByteArrayEntity(params.getBytes("UTF8"))); 
+0

謝謝你的回答,我會盡力去做 –

+0

不幸的是我發現了異常 –

+0

你抓住了哪些異常? –