Reputation: 156
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?
Upvotes: 0
Views: 870
Reputation: 1241
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
}
}
Upvotes: 3
Reputation: 42824
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::
second async task
from first async task
from onPostExecute
of first Async Task
independent async tasks
. just call one
below another but they will run independently depending on their time
complexitypicasso
, imageLoader
,Volly
for your useAlways better to use a single Async task
Upvotes: 0
Reputation: 13390
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));
Upvotes: 0
Reputation: 608
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();
}
}
Upvotes: 0