본문 바로가기

Tech develop/Android

[Android]File Upload 시 Progress 보여주기

반응형
package com.flitto.app.core.api;
import android.content.Context;
import android.os.AsyncTask;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.nio.charset.Charset;
import java.util.Map;
public class ProgressUploadAsync extends AsyncTask<Void, Integer, String> {
private static final String TAG = ProgressUploadAsync.class.getSimpleName();
private static final int HTTP_OK = 200;
private static int SOCKET_TIMEOUT_SHORT = 10000;
private static final String FILE_PART_NAME = "Filedata";
private Context context;
private Map<String, String> params;
private File file;
private String url;
private long totalSize;
private HttpClient client;
private boolean isError;
private UploadListener uploadListener;
public ProgressUploadAsync(Context context, String url, File data, Map<String, String> params, UploadListener uploadListener) {
this.context = context;
this.params = params;
this.file = data;
this.url = url;
this.uploadListener = uploadListener;
totalSize = file.length();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
HttpParams httpParams = new BasicHttpParams();
int time = SOCKET_TIMEOUT_SHORT;
HttpConnectionParams.setConnectionTimeout(httpParams, time);
HttpConnectionParams.setSoTimeout(httpParams, time);
client = new DefaultHttpClient(httpParams);
isError = false;
}
@Override
protected void onProgressUpdate(Integer... progress) {
if (uploadListener != null) {
uploadListener.onProgress(progress[0]);
}
}
@Override
protected String doInBackground(Void... params) {
return upload();
}
private String upload() {
String responseString = "";
try {
HttpPost httpPost = new HttpPost(url);
String boundary = "123456asdfg";
httpPost.addHeader("Connection", "Keep-Alive");
httpPost.addHeader("Accept-Charset", "UTF-8");
httpPost.addHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
CustomMultiPartEntity multipart = new CustomMultiPartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, boundary, Charset.forName("UTF-8"), new CustomMultiPartEntity.ProgressListener() {
@Override
public void transferred(long transferred) {
publishProgress((int) ((transferred / (float) totalSize) * 100));
}
});
//Add parameters
for (String strKey : params.keySet()) {
StringBody body = new StringBody(params.get(strKey));
multipart.addPart(strKey, body);
}
multipart.addPart(FILE_PART_NAME, new FileBody(file, file.getName()));
totalSize = multipart.getContentLength();
httpPost.setEntity(multipart);
HttpResponse response = client.execute(httpPost);
final int statusCode = response.getStatusLine().getStatusCode();
responseString = EntityUtils.toString(response.getEntity());
if (statusCode != HTTP_OK) {
isError = true;
}
} catch (Exception e) {
responseString = e.toString();
isError = true;
} finally {
client.getConnectionManager().shutdown();
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (uploadListener != null) {
if (isError) {
uploadListener.onError(result);
} else {
uploadListener.onSuccess(result);
}
}
}
public interface UploadListener {
void onSuccess(String result);
void onError(String message);
void onProgress(int progress);
}
}
반응형