I need to download some images from json urls.I have two urls.In this two urls tags are different .So i am unable to use same method to filer data.Can i use two AsyncTask operation in same class to download this images?
Asked
Active
Viewed 869 times
0
-
1Could you explain a little more. What you mean by tags are different? This url will give an idea what you are looking for from what i understand. http://stackoverflow.com/questions/18357641/is-it-possible-to-run-two-asynctask-in-same-time – codePG Oct 15 '14 at 05:47
-
1yeah you can . Please explain me in detail what do you want exactly . – Harshad07 Oct 15 '14 at 05:49
-
I have to download some images with dates.First url contain the image name and the dates.Secoond one contain the date and the image url.These are giving json response.SO i have to use two thread to download these things.what is the best way to do this? – sandamali perera Oct 15 '14 at 05:54
-
Yes, you have the sequence well described. First use a async to get the URL of the image to be download. Once you have it, start the second async calling the URL you have already retrieved. That would not cause any problems. – codePG Oct 15 '14 at 06:01
-
You can go for Image loading libraries as well here in this case, get the image URL's and pass those URL's to Picasso and it will handle the rest. – Ashish Tamrakar Oct 15 '14 at 06:08
4 Answers
3
Check this one it will help you.
Example Activity.
public class AsyncTaskActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String url1= "";
String url2= "";
new ExampleAsynTask().execute(url1,url2);
}
}
Asyntask :
private class ExampleAsynTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
// Do the Operations like updating the UI in android before the background operation is finished
}
@Override
protected String doInBackground(String... params) {
// Perform background operations that take longer time to run
String url1= params[0];
backgroundTask(params[0])
backgroundTask(params[1])
return "Done";
}
@Override
protected void onProgressUpdate(Void... values) {
// Do the Operations like updating the UI in android during the background operation is running
}
@Override
protected void onPostExecute(String result) {
// Do the Operations like updating the UI in android after the background operation is finished
}
public void backgroundTask(String url){
//To do coding
}
}

samsad
- 1,241
- 1
- 10
- 15
0
Use the async task as following ,
class asyncFirst extends AsyncTask<String, Integer, String>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog = new ProgressDialog(this);
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
HttpClient client = new DefaultHttpClient();
URI website = new URI("http:// your first link here ");
HttpPost request = new HttpPost();
// Add your code
// get your first url response here
}
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
new asyncSecond ().execute();
}
}
class asyncSecond extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
HttpClient client = new DefaultHttpClient();
URI website = new URI("http:// your second link here ");
HttpPost request = new HttpPost();
// Add your code
// get your second url response here
}
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
// dismiss your progress dialog here
progressDialog.dismiss();
}
}

Harshad07
- 608
- 7
- 23
0
Just use a single Async task, and perform two server requests in them
Sample:: This is best suitable if you need a result from first http-request
to be passed to second http-request
AsyncTaskActivity.java
public class AsyncTaskActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new LongOperation().execute("");
}
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
// Do the Operations like updating the UI in android before the background operation is finished
}
@Override
protected String doInBackground(String... params) {
// Perform background operations that take longer time to run
backgroundOperNoOne(){
//Write a Http request for First url contain the image name and the dates
//Store the value in a local variable if you want to use the data obtained from this network request to next network request
}
backgroundOperNoTwo(){
//Write a Http request for Second url contain the date and the image url
}
return "Executed";
}
@Override
protected void onProgressUpdate(Void... values) {
// Do the Operations like updating the UI in android during the background operation is running
}
@Override
protected void onPostExecute(String result) {
// Do the Operations like updating the UI in android after the background operation is finished
}
}
}
Note::
- You can also call the
second async task
fromfirst async task
fromonPostExecute
offirst Async Task
- If you want two different
independent async tasks
. just call one below another but they will run independently depending on their time complexity - If you want to handle images look at
picasso
,imageLoader
,Volly
for your use
Always better to use a single Async task

Devrath
- 42,072
- 54
- 195
- 297
0
Yes you can, but I would advise you to use ExecutorService for this. As if you need to download more images simultaneously like 10 or 15 then using that much AsyncTask would be an issue.
So
Just make a Runnable, e.g:
class ImageDownloader implements Runnable{
public String picID = "";
public ImageDownloader(String id){
picID = id;
}
@Override
public void run(){
// image downloading code
}
}
and use ExecutorService like:
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.execute(new ImageDownloader(somePicID));

Farhan
- 13,290
- 2
- 33
- 59