반응형
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
반응형
'Tech develop > Android' 카테고리의 다른 글
[Android]html형식의 txt 파일 읽어오기 (0) | 2016.03.23 |
---|---|
[android]6.0이상의 폰에서 runtime permission 처리 (0) | 2016.03.10 |
[Android]realm 적용하기 (0) | 2015.12.10 |
[android/안드로이드]java 문자열 함수 (0) | 2014.08.13 |
[안드로이드/android]xml로 그라데이션 적용하기 (0) | 2014.08.10 |