Issue
I am facing a problem which I don't know how to solve.
GetDataTask works fine. But when I am uploading a file, the request is sent but the response comes only after the file upload is finished! GetDataTask and UploadTask run asynchronously using AsyncTask.
Any idea why it doesn't work and why the response doesn't come back when the file is uploading.
Is there another way to realize this?
The problem is, it is supposed to run in a parallel but unfortunately they are running synchronously.
I have posted my actual source below.
Thanks in advance.
GetData Task:
private class GetDataTask extends AsyncTask<String, Void, String>{
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
return NetConnection.getRecordData(mUserId, mUserPassword);
}
@Override
protected void onPostExecute(String result) {
parseJson(result);
}
}
Upload Task:
private class HttpMultipartPost extends AsyncTask<String, Integer, String>
{
TextProgressBar pb;
long totalSize;
String filePath;
@Override
protected void onPreExecute()
{
pb = (TextProgressBar) findViewById(R.id.idPBUploadProgress);
pb.setMax(100);
}
@Override
protected String doInBackground(String... arg)
{
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost( Utils.UPLOAD_URL );
try
{
CustomMultiPartEntity multipartContent = new CustomMultiPartEntity(new ProgressListener()
{
@Override
public void transferred(long num)
{
publishProgress((int) ((num / (float) totalSize) * 100));
//Log.v(TAG, "Progress =" + num);
}
});
// We use FileBody to transfer an file
filePath = arg[0];
multipartContent.addPart("upfile", new FileBody(new File( filePath )));
totalSize = multipartContent.getContentLength();
Log.e(TAG, "Upload file size = " + totalSize/1048576 + "MB") ;
// Send it
httpPost.setEntity(multipartContent);
HttpResponse response = httpClient.execute(httpPost, httpContext);
String serverResponse = EntityUtils.toString(response.getEntity());
return serverResponse;
}
catch (Exception e)
{
System.out.println(e);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... progress)
{
pb.setProgress((int) (progress[0]));
pb.setText(progress[0] + "%");
}
@Override
protected void onPostExecute(String result)
{
Log.e(TAG, "Response =" + result);
parseResult( result, filePath );
}
}
Solution
Ok following are the notes from the official java doc...
Order of execution
When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.
If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.
SO if you invoke two AsyncTask together.. they would not be executed in parallel (exception is donut, ECLAIR and gingerbread)... You can use executeOnExecutor
to execute them in parallel...
Answered By - Praful Bhatnagar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.