# mehrere URLs in einem AsyncTask abarbeiten



## MARJAVA (15. Nov 2018)

```
package de.markusc.movieplanet;

import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ProgressBar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.regex.Pattern;

public class DownloadTask extends AsyncTask<String,Void, String> {

    private Activity activity;
    private ArrayList<Integer> filmIds=new ArrayList<Integer>();
    private ArrayList<String> filmFotos=new ArrayList<String>();
    public ProgressBar progressBar;
    public GridView gridView;
    public int counter;
    public int numberOfUrls;

    public DownloadTask(Activity activity,GridView gridView, ProgressBar progressBar){
        this.activity=activity;
        this.progressBar=progressBar;
        this.gridView=gridView;

        filmIds.clear();
        filmFotos.clear();
    }
    @Override
    protected String doInBackground(String... urls) {
        String result="";
        URL url;
        HttpURLConnection urlConnection=null;
        try{
            numberOfUrls=urls.length;
            for (int i=0;i<urls.length;i++) {
                url = new URL(urls[i]);

                urlConnection = (HttpURLConnection) url.openConnection();
                InputStream inputStream = urlConnection.getInputStream();
                InputStreamReader reader = new InputStreamReader(inputStream);
                int data = reader.read();

                while (data != -1) {
                    char current = (char) data;
                    result += current;
                    data = reader.read();
                }
                return result;
            }
            return null;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }


    protected void onPostExecute(String s){
        super.onPostExecute(s);


        String[] resultString=s.split("page:");
        Log.i("log anzahl", String.valueOf(resultString.length));
        Log.i("log string s",s);
        counter++;
        Log.i("log counter",String.valueOf(counter));
        try {
            JSONObject jsonObject=new JSONObject(s);
            String page1Result=jsonObject.getString("results");
            JSONArray jsonArray=new JSONArray(page1Result);


            for (int i=0; i<jsonArray.length();i++){

                JSONObject filme=jsonArray.getJSONObject(i);
                int filmID=filme.getInt("id");
                String filmName=filme.getString("title");
                String filmFoto=filme.getString("poster_path");
                filmIds.add(filmID);
                filmFotos.add(filmFoto);



                Log.i("log", String.valueOf(filmIds.get(i)));

                Log.i("log", String.valueOf(filmIds.get(0)));
            }




        } catch (JSONException e) {
            e.printStackTrace();
        }

        gridView.setAdapter(new GridViewAdapter(activity,filmIds,filmFotos));
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                     /*   Intent intent2 = new Intent(getActivity(), ThirdActivity.class);
                        intent2.putExtra("id",filmIds.get(i));
                        Log.i("log filmID", String.valueOf(filmIds.get(i)));
                        startActivity(intent2);*/
            }
        });
        gridView.setVisibility(View.VISIBLE);
        progressBar.setVisibility(View.INVISIBLE);

    }
}
```
Hallo zusammen,

ich möchte mehrere URLs in einem AsyncTask laufen lassen und in der Methode onPostExecute schlussendlich gemeinsam auswerten. Leider wertet die onPostExecute-Methode nur die erste URL aus.
Jemand eine Idee, wie ich mehrere URLs gleichzeitig auswerten kann, wenn sie nacheinander durchgelaufen sind?


----------



## mihe7 (16. Nov 2018)

Indem Du die Ergebnisse z. B. in einer Liste ablegst und die Liste dann zurückgibst?


----------



## MARJAVA (18. Nov 2018)

In der Deklarationszeile hatte ich kein StringArray eingetragen, jetzt funktioniert es wunderbar, eben mit einem String Array


----------

