[英]multipart request on Android goes to timeout
I Need to upload one file from my Android using multipart and tried with the below code but with no luck. 我需要使用multipart从我的Android设备上载一个文件,并尝试使用以下代码,但没有成功。 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)
08-06 17:49:03.006:W / System.err(24761):原因:libcore.io.ErrnoException:连接失败:ETIMEDOUT(连接超时)
I also tried with those solutions: 我还尝试了以下解决方案:
https://stackoverflow.com/a/19188010/1948785 https://stackoverflow.com/a/19188010/1948785
and with the deprecated class MultipartEntity 以及已弃用的类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;
08-06 17:45:53.461:W / dalvikvm(24761):VFY:无法解析Lorg / apache / http / message / BasicHeaderValueParser中的静态字段3008(INSTANCE);
The libs I am using are those: 我正在使用的库是:
Try this , 尝试这个 ,
DefaultHttpClient httpClient = new DefaultHttpClient();
// yourimagefile is your imagefile
HttpPost httpPostRequest = new HttpPost(URL);
// Try This
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(yourimagefile, "image/jpeg");
mpEntity.addPart("file", cbFile);
httpPostRequest.setEntity(mpEntity);
HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
Try to set timeout duration of HttpClient. 尝试设置HttpClient的超时时间。
int TIMEOUT_MILLISEC = 20000; // = 20 seconds
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);
Check the size of the image. 检查图像的尺寸。 It might be that the image is too large and it is taking a long period of time to upload and the server is issuing a timeout on that connection.
可能是图像太大,并且上载时间很长,并且服务器在该连接上发出了超时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.