【发布时间】:2015-02-17 12:24:03
【问题描述】:
我有在 android 中上传视频的功能。为此,我将整个代码放入服务中,并在其中使用 AsyncTask 在 doInBackground() 中调用上传视频功能。下面是上传视频的代码:
private String uploadFile(String filePath, String proid, String procat) {
String responseString = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Const_Url.FILE_UPLOAD_URL);
try {
AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
new ProgressListener() {
@Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
File sourceFile = new File(filePath);
// Adding file data to http body
entity.addPart("image", new FileBody(sourceFile));
entity.addPart("project_id", new StringBody(proid));
entity.addPart("user_id", new StringBody(userid));
entity.addPart("video_type", new StringBody("in_store"));
Log.d("background file path ", filePath);
Log.d("background project id", proid);
Log.d("background project cat", procat);
Log.d("background user id", userid);
totalSize = entity.getContentLength();
httppost.setEntity(entity);
// Making server call
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
// Server response
responseString = EntityUtils.toString(r_entity);
} else {
responseString = "Error occurred! Http Status Code: "
+ statusCode;
}
} catch (ClientProtocolException e) {
responseString = e.toString();
} catch (IOException e) {
responseString = e.toString();
}
return responseString;
}
因此 AndroidMultiPartEntity 会上传整个视频。
但问题是,如果在两者之间我试图对服务器进行 HTTP 调用,那么它只会等到视频上传完成()。
理想情况下,它不应该等到时间,而是尽快回复我。我认为视频上传服务会占用整个带宽,因此不会进行其他 HTTP 调用。
请提出一些解决方案。 TIA。
附:我试过了
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
降低该 AsyncTask 的优先级并增加其他 http 调用的优先级(Asynctask)。但徒劳无功。
【问题讨论】:
标签: android multithreading video android-asynctask android-service