Reputation: 523
I am using the following code to post Image to server,but it is giving error below
org.apache.http.NoHttpResponseException: The target server failed to respond
I did not understand whether the issue is from Server side or from my Code side.
can anyone please help me in solving this issue?
code:
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`
Upvotes: 3
Views: 4596
Reputation: 23344
Make sure that you have android.permission.INTERNET
permission in your manifest file.
Also check - Apache HttpClient Interim Error: NoHttpResponseException.
EDIT:
java.io.IOException
+- org.apache.commons.httpclient.NoHttpResponseException
In some circumstances, usually when under heavy load, the web server may be able to receive requests but unable to process them. A lack of sufficient resources like worker threads is a good example. This may cause the server to drop the connection to the client without giving any response. HttpClient throws NoHttpResponseException when it encounters such a condition. In most cases it is safe to retry a method that failed with NoHttpResponseException.
SOURCE: http://hc.apache.org/httpclient-3.x/exception-handling.html
Upvotes: 1