I Need to upload one file from my Android using multipart and tried with the below code but with no luck. I got a connection timeout exception and tried with different code having the same result.
try
{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entityBuilder.addTextBody(USER_ID, "DFD");
entityBuilder.addTextBody(NAME, "DFD");
String filepath = Environment.getExternalStorageDirectory() + "/Download/myImage.jpg";
Log.d(MULTIPART_TAG, filepath);
File file = new File(filepath);
if(file != null)
{
entityBuilder.addBinaryBody("IMAGE", file);
}
HttpEntity entity = entityBuilder.build();
post.setEntity(entity);
HttpResponse response = client.execute(post);
HttpEntity httpEntity = response.getEntity();
String result = EntityUtils.toString(httpEntity);
Log.v("result", result);
}
catch(Exception e)
{
e.printStackTrace();
}
Here is the exception that I get:
08-06 17:49:03.006: W/System.err(24761): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
I also tried with those solutions:
https://stackoverflow.com/a/19188010/1948785
and with the deprecated class MultipartEntity
My second question is what is the meaning of this warning (I dont know if it is related with my problem but I get it when performing the request):
08-06 17:45:53.461: W/dalvikvm(24761): VFY: unable to resolve static field 3008 (INSTANCE) in Lorg/apache/http/message/BasicHeaderValueParser;
The libs I am using are those:
- httpclient-4.3.4.jar
- httpcore-4.3.2.jar
- httpmime-4.3.4.jar