我一直在测试一个小型嵌入式 Web 服务器Web server(它正在运行 mike G. 的multisocket server)。我正在尝试使用 Eclipse 中的 android 虚拟设备从服务器获取响应,但由于某种原因,服务器有一半时间无法响应。
这是错误的logcat。“on”是服务器成功时响应的字符串。
09-03 23:12:54.672: I/System.out(996): on
09-03 23:12:55.989: I/System.out(996): on
09-03 23:12:57.113: D/dalvikvm(996): GC_FOR_MALLOC freed 882K, 53% free 3142K/6663K, external 1625K/2137K, paused 37ms
09-03 23:12:57.113: W/System.err(996): org.apache.http.NoHttpResponseException: The target server failed to respond
09-03 23:12:57.122: W/System.err(996): at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:85)
09-03 23:12:57.122: W/System.err(996): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
09-03 23:12:57.122: W/System.err(996): at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
09-03 23:12:57.122: W/System.err(996): at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
09-03 23:12:57.122: W/System.err(996): at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
09-03 23:12:57.132: W/System.err(996): at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
09-03 23:12:57.132: W/System.err(996): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
09-03 23:12:57.132: W/System.err(996): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:421)
09-03 23:12:57.132: W/System.err(996): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-03 23:12:57.132: W/System.err(996): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-03 23:12:57.132: W/System.err(996): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-03 23:12:57.132: W/System.err(996): at com.SS.Main.TestHttpGet.executeHttpGet(TestHttpGet.java:40)
09-03 23:12:57.132: W/System.err(996): at com.SS.Main.SmartHomeSystemActivity$1.onClick(SmartHomeSystemActivity.java:32)
09-03 23:12:57.132: W/System.err(996): at android.view.View.performClick(View.java:2485)
09-03 23:12:57.132: W/System.err(996): at android.view.View$PerformClick.run(View.java:9080)
09-03 23:12:57.142: W/System.err(996): at android.os.Handler.handleCallback(Handler.java:587)
09-03 23:12:57.142: W/System.err(996): at android.os.Handler.dispatchMessage(Handler.java:92)
09-03 23:12:57.142: W/System.err(996): at android.os.Looper.loop(Looper.java:123)
09-03 23:12:57.142: W/System.err(996): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-03 23:12:57.142: W/System.err(996): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 23:12:57.142: W/System.err(996): at java.lang.reflect.Method.invoke(Method.java:507)
09-03 23:12:57.142: W/System.err(996): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-03 23:12:57.142: W/System.err(996): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-03 23:12:57.152: W/System.err(996): at dalvik.system.NativeStart.main(Native Method)
我试图从这个线程做一个重试处理程序
但这似乎并没有解决它(或者我使用不正确)所以我决定尝试继续重新发送请求。这是正在运行的代码,但我觉得这不是一个好方法。当来自PC的浏览器发送请求时,服务器没有问题,但有时会无法响应android浏览器和应用程序仿真。有任何想法吗?
package com.SS.Main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
public class TestHttpGet {
public void executeHttpGet() throws Exception {
BufferedReader in = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
HttpProtocolParams.setUseExpectContinue(client.getParams(), false);
request.setURI(new URI("http://142.197.135.16:5000/aled.htm?led=on"));
//HttpResponse response = client.execute(request);
HttpResponse response = null;
boolean RetryConnection = true;
int retrycount = 0;
//retry request 5 times
while(RetryConnection == true && retrycount < 5)
{
try{
response = client.execute(request);
RetryConnection = false;
}catch(IOException e)
{
System.out.println("caught");
retrycount += 1;
}
}
in = new BufferedReader
(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String page = sb.toString();
System.out.println(page);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}