I am using MultiPartRequester class for multipart image upload to server, but i found that some part is deprecated in this. for example HttpConnectionParams , getConnectionManager() etc. so anyone have new solution that is not deprecated with new API level for file upload?
I am using this code.
public class MultiPartRequester {
private Map<String, String> map;
private AsyncTaskCompleteListener mAsynclistener;
private int serviceCode;
private HttpClient httpclient;
private Activity activity;
private AsyncHttpRequest request;
private static final String TAG = "MultiPartRequester";
public MultiPartRequester(Activity activity, Map<String, String> map,
int serviceCode, AsyncTaskCompleteListener asyncTaskCompleteListener) {
this.map = map;
this.serviceCode = serviceCode;
this.activity = activity;
}
class AsyncHttpRequest extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
map.remove("url");
try {
HttpPost httppost = new HttpPost(urls[0]);
httpclient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(
httpclient.getParams(), 600000);
MultipartEntityBuilder builder = MultipartEntityBuilder
.create();
for (String key : map.keySet()) {
if (key.equalsIgnoreCase(AndyConstants.Params.PICTURE)) {
File f = new File(map.get(key));
builder.addBinaryBody(key, f,
ContentType.MULTIPART_FORM_DATA, f.getName());
} else {
builder.addTextBody(key, map.get(key), ContentType
.create("text/plain", MIME.UTF8_CHARSET));
}
AppLog.Log(TAG, key + "---->" + map.get(key));
}
httppost.setEntity(builder.build());
ActivityManager manager = (ActivityManager) activity
.getSystemService(Context.ACTIVITY_SERVICE);
if (manager.getMemoryClass() < 25) {
System.gc();
}
HttpResponse response = httpclient.execute(httppost);
String responseBody = EntityUtils.toString(
response.getEntity(), "UTF-8");
reurn responseBody;
} catch (Exception e) {
e.printStackTrace();
} catch (OutOfMemoryError oume) {
System.gc();
} finally {
if (httpclient != null)
httpclient.getConnectionManager().shutdown();
}
return null;
}
@Override
protected void onPostExecute(String response) {
if (mAsynclistener != null) {
mAsynclistener.onTaskCompleted(response, serviceCode);
}
}
}
public void cancelTask() {
request.cancel(true);
AppLog.Log(TAG, "task is canelled");
}
}
this is FileUploadMultipartRequest class:
this is BaseRequest class:
and this is GsonRequest class:
sample cod:
you can add more request to my request and call that anywhere. like this:
server side cod (.net):
public class ImageRouteHandler :
and change your RouteConfige like this:
sample code for server side:
IMO, you should use one of the following:
OkHttp
Retrofit
Google's Volley
With Volley and Retrofit, yon can read my answers at Working POST Multipart Request with Volley and without HttpEntity and Retrofit - Multipart request: Required MultipartFile parameter 'file' is not present
Regaring OkHttp, please try My GitHub sample project with basic sample code as the following:
Hope it helps!
Yeah as the Apache HTTPClient is deprecated now you can use HTTPURLConnection I used this code to send multiple files over gmail via SMTP, You modify the code and use it!