Back to project page VideoExtand.
The source code is released under:
Apache License
If you think the Android project VideoExtand listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** * /* w ww .ja v a 2s . com*/ */ package com.yuninfo.videoextand.uploader; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.charset.Charset; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.CountDownLatch; import org.apache.http.HttpResponse; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; 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.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.util.EntityUtils; import android.os.Handler; import android.os.Looper; import android.os.Message; import com.google.gson.Gson; import com.google.gson.JsonParseException; import com.google.gson.JsonSyntaxException; import com.yuninfo.videoextand.Config; import com.yuninfo.videoextand.bean.UploadReq; import com.yuninfo.videoextand.bean.UploadResp; import com.yuninfo.videoextand.utils.BeanRefUtil; import com.yuninfo.videoextand.utils.HttpUtil; import com.yuninfo.videoextand.utils.LogUtil; import com.yuninfo.videoextand.utils.StringUtil; /** * Upload thread. * @author zhangyy@yuninfo.com * */ public class UploadThread extends Thread { private static final String TAG = UploadThread.class.getSimpleName(); private static final String REQUEST_URL = Config.YUNINFO_VIDEO_UPLOAD_URL; private final String mCookie; private final UploadReq mParams; private final Handler mHandler; private final CountDownLatch mHandlerInitLatch; private HttpPost mHttpPost = null; public UploadThread(String cookie, UploadReq params, Handler handler) { this.mCookie = cookie; this.mParams = params; this.mHandler = handler; this.mHandlerInitLatch = new CountDownLatch(1); } private ProgressListener<Long> mProgressListener = new ProgressListener<Long>() { @Override public void onProgressUpdate(Long... progress) { Message msg = mHandler.obtainMessage(Config.YUNINFO_ID_TASK_PROGRESS, progress); mHandler.sendMessage(msg); } }; public Handler getHandler() { try { mHandlerInitLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } return this.mHandler; } @Override public void run() { Looper.prepare(); super.run(); Message msg = null; try { HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 1000 * 30); HttpConnectionParams.setSoTimeout(httpParameters, 1000 * 300); HttpConnectionParams.setTcpNoDelay(httpParameters, true); HttpClient httpclient = new DefaultHttpClient(httpParameters); mHttpPost = new HttpPost(REQUEST_URL); ProgressMultipartEntity multiEntity = new ProgressMultipartEntity(); multiEntity.setProgressListener(mProgressListener); Map<String, Object> params = BeanRefUtil.getFieldValueMap(mParams); LogUtil.i(TAG, params); Iterator<Entry<String, Object>> iterator = params.entrySet().iterator(); while(iterator.hasNext()) { Entry<String, Object> entry = iterator.next(); String key = entry.getKey(); Object obj = entry.getValue(); if (obj instanceof File) { File file = (File) params.get(key); if (!file.exists()) { throw new FileNotFoundException("File:" + file.getAbsolutePath() + "not found"); } multiEntity.addPart(key, new FileBody(file, "*/*")); } else if(obj instanceof List) { multiEntity.addPart(key, new StringBody(obj == null ? "" : new Gson().toJson(obj), Charset.forName("utf-8"))); } else { multiEntity.addPart(key, new StringBody(obj == null ? "" : obj.toString(), Charset.forName("utf-8"))); } } mHttpPost.setEntity(multiEntity); LogUtil.i(TAG, mHttpPost.getRequestLine()); if(!StringUtil.isEmpty(mCookie)) { mHttpPost.addHeader("Cookie", mCookie); } mHttpPost.addHeader("Connection", "keep-alive"); mHttpPost.addHeader("Chartset", "UTF-8"); HttpResponse resp = httpclient.execute(mHttpPost); if(resp == null) { msg = mHandler.obtainMessage(Config.YUNINFO_ID_TASK_FAILED, HttpUtil.FAILED, -1, mParams); } else { if(resp.getStatusLine().getStatusCode() == HttpUtil.OK) { String jsonStr = EntityUtils.toString(resp.getEntity()); LogUtil.i(TAG, jsonStr); Gson gson = new Gson(); UploadResp data = gson.fromJson(jsonStr, UploadResp.class); UploadResp.UploadData video = data.getVideo(); if(video == null || video.getRect() != 1 ) { msg = mHandler.obtainMessage(Config.YUNINFO_ID_TASK_FAILED, HttpUtil.FAILED, resp.getStatusLine().getStatusCode(), video == null ? "??????" : video.getMsg()); } else { msg = mHandler.obtainMessage(Config.YUNINFO_ID_TASK_SUCCESSED, HttpUtil.SUCCES, resp.getStatusLine().getStatusCode(), data); } } else { msg = mHandler.obtainMessage(Config.YUNINFO_ID_TASK_FAILED, HttpUtil.FAILED, resp.getStatusLine().getStatusCode(), null); } } } catch (ClientProtocolException e) { e.printStackTrace(); msg = mHandler.obtainMessage(Config.YUNINFO_ID_TASK_FAILED, HttpUtil.NET_ERROR, -1, mParams); } catch (FileNotFoundException e) { e.printStackTrace(); msg = mHandler.obtainMessage(Config.YUNINFO_ID_TASK_FAILED, HttpUtil.FILE_NOT_FOUND, -1, mParams); } catch (IOException e) { e.printStackTrace(); msg = mHandler.obtainMessage(Config.YUNINFO_ID_TASK_FAILED, HttpUtil.IO_ERROR, -1, mParams); } catch (ParseException e) { e.printStackTrace(); msg = mHandler.obtainMessage(Config.YUNINFO_ID_TASK_FAILED, HttpUtil.JSON_ERROR, -1, mParams); } catch (JsonSyntaxException e) { e.printStackTrace(); msg = mHandler.obtainMessage(Config.YUNINFO_ID_TASK_FAILED, HttpUtil.JSON_ERROR, -1, mParams); } catch (JsonParseException e) { e.printStackTrace(); msg = mHandler.obtainMessage(Config.YUNINFO_ID_TASK_FAILED, HttpUtil.JSON_ERROR, -1, mParams); } catch (Exception e) { e.printStackTrace(); msg = mHandler.obtainMessage(Config.YUNINFO_ID_TASK_FAILED, -1, -1, mParams); } if(!isInterrupted()) { mHandler.sendMessage(msg); } mHandlerInitLatch.countDown(); Looper.loop(); } @Override public synchronized void start() { mHandler.sendEmptyMessage(Config.YUNINFO_ID_TASK_STARTED); super.start(); } @Override public void interrupt() { if(mHttpPost != null && !mHttpPost.isAborted()) { mHttpPost.abort(); } mHandler.sendEmptyMessage(Config.YUNINFO_ID_TASK_CANCELED); super.interrupt(); } }