package com.jiuqi.njt.management.task;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
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.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import com.jiuqi.njt.data.OptsharepreInterface;
import com.jiuqi.njt.management.FregmentFileRepair.TaskFinished;
import com.jiuqi.njt.management.task.MyMultipartEntity.ProgressListener;
import com.jiuqi.njt.util.Constants;
import com.jiuqi.njt.util.UIUtil;
import com.jiuqi.njt.util.login.AutoLoginTask;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
/**
* 新增维修报修数据上传(包含图片)
* @author
*
*/
public class UpUserImageTask extends AsyncTask<String, Integer, String> {
private Context context;
private long totalSize;
private String tag = this.getClass().getName();
private Bundle bundle;
private String imageName;
private Map<String, String> postParams = new HashMap<String, String>();
private String actionUrl = Constants.SERVER_URL+"/setsystem/user!saveImage.action";
private TaskFinished taskFinished;
protected OptsharepreInterface sharePre;
public UpUserImageTask(Context context, String imageName,TaskFinished taskFinished) {
super();
this.context = context;
this.imageName = imageName;
this.taskFinished = taskFinished;
sharePre = new OptsharepreInterface(context);
}
public UpUserImageTask(Context context, String imageName) {
super();
this.context = context;
this.imageName = imageName;
sharePre = new OptsharepreInterface(context);
}
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
String serverResponse = "";
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(actionUrl);
postParams.put("guid", sharePre.getPres("guid"));
try {
MyMultipartEntity multipartContent = new MyMultipartEntity(
new ProgressListener() {
@Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
if (params != null && !postParams.isEmpty()) {
for (Map.Entry<String, String> entry : postParams.entrySet()) {
StringBody par = null;
try {
if (entry.getValue() == null) {
continue;
}
par = new StringBody(java.net.URLEncoder.encode(
entry.getValue(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
multipartContent.addPart(entry.getKey(), par);
}
}
Log.e(tag, imageName);
FileBody file = new FileBody(new File(imageName));
multipartContent.addPart("imageFile", file);
// We use FileBody to transfer an image
totalSize = multipartContent.getContentLength();
// Send it
httpPost.setEntity(multipartContent);
HttpResponse response = httpClient.execute(httpPost, httpContext);
int res = response.getStatusLine().getStatusCode();
// new File(imageName).delete();
Log.e("图片上传返回相应码", res + ",");
switch (res) {
case 200:
serverResponse = "数据上传成功";
break;
case 404:
serverResponse = "数据上传失败,请重试";
break;
default:
serverResponse = "数据上传失败,请重试";
}
} catch (Exception e) {
serverResponse = "图片上传失败,请重试";
e.printStackTrace();
}
return serverResponse;
}
@Override
protected void onProgressUpdate(Integer... progress) {
}
@Override
protected void onPostExecute(String result) {
if("数据上传成功".equals(result)){
new AutoLoginTask(context).execute();
if(null!=taskFinished){
taskFinished.whenTaskFinish();
}
}else{
UIUtil.showMsg(context, result);
}
}
@Override
protected void onCancelled() {
System.out.println("cancle");
}
}