当我尝试发布到服务器时出错:
05-05 12:37:05.824: W/System.err(20610): org.apache.http.NoHttpResponseException: The target server failed to respond
05-05 12:37:05.824: W/System.err(20610): at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:85)
05-05 12:37:05.824: W/System.err(20610): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
05-05 12:37:05.824: W/System.err(20610): at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
05-05 12:37:05.824: W/System.err(20610): at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
05-05 12:37:05.824: W/System.err(20610): at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
05-05 12:37:05.824: W/System.err(20610): at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
05-05 12:37:05.824: W/System.err(20610): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
05-05 12:37:05.824: W/System.err(20610): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
05-05 12:37:05.824: W/System.err(20610): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
05-05 12:37:05.834: W/System.err(20610): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
05-05 12:37:05.834: W/System.err(20610): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-05 12:37:05.834: W/System.err(20610): at
05-05 12:37:05.834: W/System.err(20610): at
05-05 12:37:05.834: W/System.err(20610): at
05-05 12:37:05.834: W/System.err(20610): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-05 12:37:05.834: W/System.err(20610): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-05 12:37:05.834: W/System.err(20610): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-05 12:37:05.834: W/System.err(20610): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-05 12:37:05.834: W/System.err(20610): at java.lang.Thread.run(Thread.java:856)
这是我的方法代码:
private HttpResponse connectAndDownload() {
try {
String postBody;
if (requestItem == null) {
postBody = CollectBasicData.generateJson().toString();
} else {
postBody = requestItem;
}
HttpParams httpParams = new BasicHttpParams();
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(httpParams, HTTP.UTF_8);
final DefaultHttpClient client = new DefaultHttpClient(httpParams);
final HttpUriRequest httpUriRequest;
if(requestItem=="METHOD_GET"){
HttpGet httpget= new HttpGet(url);
if(DEBUG_VERSION.IS_DEBUG){
Log.v(" Download API", "url: " + url);
Log.v("DownloadApi", "postBody: " + postBody);
}
httpget.setHeader("Accept", "application/json");
httpget.setHeader("Content-type", "application/json");
httpUriRequest = httpget;
}
else{
HttpPost httppost = new HttpPost(url);
StringEntity se = new StringEntity(postBody, HTTP.UTF_8);
if(DEBUG_VERSION.IS_DEBUG){
Log.v(" Download API", "url: " + url);
Log.v("DownloadApi", "postBody: " + postBody);
}
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-type", "application/json");
httppost.setEntity(se);
httpUriRequest = httppost;
}
HttpConnectionParams.setConnectionTimeout(httpUriRequest.getParams(), TIMEOUT);
HttpConnectionParams.setSoTimeout(httpUriRequest.getParams(), TIMEOUT);
httpUriRequest.addHeader(
"Authorization",
"Basic "
+ Base64.encodeToString((CollectBasicData.getUserName() + ":" + CollectBasicData
.getPassword()).getBytes(), Base64.NO_WRAP));
HttpResponse response = client.execute(httpUriRequest);
return response;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
我尝试以下解决方案: HttpClient on Android : NoHttpResponseException through UMTS/3G 和 http://code.google.com/p/android/issues/detail?id=13117 但没有效果。 当我尝试使用3G发布服务时出现此错误。当我使用WIFI时,一切都还可以。
答案 0 :(得分:0)
我不确定,但这可能会有所帮助。 尝试使用Android的浏览器连接到您的服务器,以确定问题的确切位置(3G连接或您的应用程序),并确保您的服务器可以从公共互联网访问。