我使用以下代码将Image发布到服务器,但它在
下面给出了错误org.apache.http.NoHttpResponseException:目标服务器无法响应
我不明白问题是来自服务器端还是来自我的代码端。
任何人都可以帮我解决这个问题吗?
代码:
protected Long doInBackground(URL... arg0) {
CustomMultiPartEntity multipartContent = new CustomMultiPartEntity(
new ProgressListener() {
@Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
try {
for (int i = Constants.IMAGE_SELECTION; i < al_image_paths
.size(); i++) {
count = i + 1;
progressDialog.setProgress(0);
// String url=DataUrl.hostId+"/addpost/postImageOrVideo?";
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(image_upload);
Log.e("strImagePath.... before uploading",
al_image_paths.get(i));
multipartContent.addPart("image", new FileBody(new File(
al_image_paths.get(i))));
multipartContent.addPart("sellleadid",
new StringBody(pref.getString("sellerID", "")));
multipartContent.addPart("action", new StringBody(
"Send Used Car Images"));
multipartContent.addPart("app_id", new StringBody(Constants.DEVICE_ID));
totalSize = multipartContent.getContentLength();
httpPost.setEntity(multipartContent);
HttpResponse response = httpClient.execute(httpPost,
localContext);
String serverResponse = EntityUtils.toString(response
.getEntity());
Log.e("serverResponse image", "<> " + serverResponse);
JSONObject object = new JSONObject(serverResponse);
if (object.getString("status").equals("200")) {
selectedImageIds.add(object.getString("imageid"));
selectedUrls.add(object.getString("url"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}`enter code here`
答案 0 :(得分:0)
确保您在清单文件中拥有android.permission.INTERNET
权限。
同时检查 - Apache HttpClient Interim Error: NoHttpResponseException。
修改强>
java.io.IOException
+- org.apache.commons.httpclient.NoHttpResponseException
在某些情况下,通常在负载较重的情况下,Web服务器 可能能够接收请求但无法处理它们。缺乏 像工作线程这样的充足资源是一个很好的例子这可能 导致服务器在不给出的情况下断开与客户端的连接 任何回应。当HttpClient抛出NoHttpResponseException时 遇到这样的情况。在大多数情况下,重试a是安全的 NoHttpResponseException失败的方法。
消息来源: http://hc.apache.org/httpclient-3.x/exception-handling.html