2012-09-06 167 views
1

我有這個奇怪的錯誤:我甚至把無限超時試圖解決它。我說這很奇怪,因爲它曾經工作得很好,但現在,它不起作用,我不知道原因。錯誤:操作超時

09-05 22:40:04.930: W/IInputConnectionWrapper(820): showStatusIcon on inactive InputConnection 
09-05 22:43:26.901: E/ServicioRest(881): Error en listar! 
09-05 22:43:26.901: E/ServicioRest(881): java.net.SocketException: The operation timed out 
09-05 22:43:26.901: E/ServicioRest(881): at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(Native Method) 
09-05 22:43:26.901: E/ServicioRest(881): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:115) 
09-05 22:43:26.901: E/ServicioRest(881): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:244) 
09-05 22:43:26.901: E/ServicioRest(881): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:533) 
09-05 22:43:26.901: E/ServicioRest(881): at java.net.Socket.connect(Socket.java:1055) 
09-05 22:43:26.901: E/ServicioRest(881): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119) 
09-05 22:43:26.901: E/ServicioRest(881): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143) 
09-05 22:43:26.901: E/ServicioRest(881): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 
09-05 22:43:26.901: E/ServicioRest(881): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 
09-05 22:43:26.901: E/ServicioRest(881): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348) 
09-05 22:43:26.901: E/ServicioRest(881): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 
09-05 22:43:26.901: E/ServicioRest(881): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 
09-05 22:43:26.901: E/ServicioRest(881): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 
09-05 22:43:26.901: E/ServicioRest(881): at com.example.interviewhelpers.ClientDetailActivity.onCreateView(ClientDetailActivity.java:72) 
09-05 22:43:26.901: E/ServicioRest(881): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870) 
09-05 22:43:26.901: E/ServicioRest(881): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080) 
09-05 22:43:26.901: E/ServicioRest(881): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622) 
09-05 22:43:26.901: E/ServicioRest(881): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416) 
09-05 22:43:26.901: E/ServicioRest(881): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:505) 
09-05 22:43:26.901: E/ServicioRest(881): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129) 
09-05 22:43:26.901: E/ServicioRest(881): at android.app.Activity.performStart(Activity.java:3781) 
09-05 22:43:26.901: E/ServicioRest(881): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636) 
09-05 22:43:26.901: E/ServicioRest(881): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
09-05 22:43:26.901: E/ServicioRest(881): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
09-05 22:43:26.901: E/ServicioRest(881): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
09-05 22:43:26.901: E/ServicioRest(881): at android.os.Handler.dispatchMessage(Handler.java:99) 
09-05 22:43:26.901: E/ServicioRest(881): at android.os.Looper.loop(Looper.java:123) 
09-05 22:43:26.901: E/ServicioRest(881): at android.app.ActivityThread.main(ActivityThread.java:4627) 
09-05 22:43:26.901: E/ServicioRest(881): at java.lang.reflect.Method.invokeNative(Native Method) 
09-05 22:43:26.901: E/ServicioRest(881): at java.lang.reflect.Method.invoke(Method.java:521) 
09-05 22:43:26.901: E/ServicioRest(881): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
09-05 22:43:26.901: E/ServicioRest(881): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
09-05 22:43:26.901: E/ServicioRest(881): at dalvik.system.NativeStart.main(Native Method) 

這是調用Web服務的方法:最後一行是eclipse告訴我發生問題的地方。

public static final String ruta = "http://10.0.0.2:2731/Api/Usuario"; 

     HttpParams httpParameters = new BasicHttpParams(); 
     HttpConnectionParams.setConnectionTimeout(httpParameters, 0); 
     HttpConnectionParams.setSoTimeout(httpParameters, 0); 
     HttpConnectionParams.setTcpNoDelay(httpParameters, true); 

     HttpClient httpClient = new DefaultHttpClient(httpParameters); 
     HttpGet del = new HttpGet(ruta + "s"); 
     del.setHeader("content-type", "application/json"); 

     try { 
      HttpResponse resp = httpClient.execute(del); 
+1

當您嘗試通過網絡瀏覽器瀏覽網址時,url'http://10.0.0.2:2731/Api/Usuario'是否顯示任何響應?另外,'HttpGet del = new HttpGet(ruta +「s」);'似乎將「s」連接到url字符串的末尾。所以,聲明結果爲'HttpGet del = new HttpGet(「http://10.0.0.2:2731/Api/Usuarios」);'那是你的意思嗎?而且,您確定要將內容類型設置爲'application/json',尤其是當您的請求是'GET'請求時? – sandyiscool

+0

對所有問題都是肯定的。第一個:瀏覽器中的網址不起作用,但這是針對模擬器的。通過localhost和's'連接,我可以看到內容。而且,因爲該服務給我的是一個json,所以我使用該內容類型 –

+1

當你說'瀏覽器中的URL不起作用'時,你的意思是它也超時了嗎?或者它顯示任何錯誤信息?其次,服務器返回給你的數據的內容類型在響應的'content-type'標題中設置。你很可能不會在請求中發送json數據。所以,你通常不會設置。 – sandyiscool

回答

1

您在代碼中將超時值設爲0。使用大於5000(毫秒)的值而不是0。 Ie HttpConnectionParams.setConnectionTimeout(httpParameters,5000); HttpConnectionParams.setSoTimeout(httpParameters,5000);

+1

值爲0表示無限超時。 – sandyiscool

+0

現在它給了我不同的錯誤09-05 23:47:39.550:E/ServicioRest(274):org.apache.http.conn.ConnectTimeoutException:連接到/10.0.0.2:2731超時 –

+1

看看這個問題http://stackoverflow.com/questions/6705170/httpclient-execute-keeps-giving-connecttimeoutexception – SSS