Remove all your httpclient, httpmime dependency and add this dependency compile 'commons-httpclient:commons-httpclient:3.1'
. This dependency has built in MultipartRequestEntity so that you can easily upload one or more files to the server
public class FileUploadUrlConnection extends AsyncTask<String, String, String> {
private Context context;
private String url;
private List<File> files;
public FileUploadUrlConnection(Context context, String url, List<File> files) {
this.context = context;
this.url = url;
this.files = files;
}
@Override
protected String doInBackground(String... params) {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(url);
HttpClientParams connectionParams = new HttpClientParams();
post.setRequestHeader(// Your header goes here );
try {
Part[] parts = new Part[files.size()];
for (int i=0; i<files.size(); i++) {
Part part = new FilePart(files.get(i).getName(), files.get(i));
parts[i] = part;
}
MultipartRequestEntity entity = new MultipartRequestEntity(parts, connectionParams);
post.setRequestEntity(entity);
int statusCode = client.executeMethod(post);
String response = post.getResponseBodyAsString();
Log.v("Multipart "," "+response);
if(statusCode == 200) {
return response;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
You can also add the request and response timeout
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);