mehrere URLs in einem AsyncTask abarbeiten

MARJAVA

Aktives Mitglied
Java:
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?
 

MARJAVA

Aktives Mitglied
In der Deklarationszeile hatte ich kein StringArray eingetragen, jetzt funktioniert es wunderbar, eben mit einem String Array
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
ruutaiokwu `build.gradle` so konfigurieren dass es mehrere .jar's z.b. mit org.junit erlaubt, oder... Android & Cross-Platform Mobile Apps 11
A Mehrere EditText-Komponenten sollen zusammenarbeiten Android & Cross-Platform Mobile Apps 2
L Musikplayer mehrere Aktivities Android & Cross-Platform Mobile Apps 6
D Android "mehrere" Fragmente erstellen + richtige Zuordnung. Aber wie? Android & Cross-Platform Mobile Apps 1
A Mehrere Fehler beim Compilieren Android & Cross-Platform Mobile Apps 4
C Android Code in mehrere .java verteilen Android & Cross-Platform Mobile Apps 6
P EditText mehrere Zeilen - verschiedene Farbe Android & Cross-Platform Mobile Apps 3
R Android Mehrere TabHosts im gleichen Projekt Android & Cross-Platform Mobile Apps 4
H Mehrere Activities (Android) Android & Cross-Platform Mobile Apps 4
J Mehrere Plattformen Android & Cross-Platform Mobile Apps 2
D Java ME Mehrere Random´s geben immer die gleiche Zahl zurück Android & Cross-Platform Mobile Apps 7
I Java Handy - mehrere Programme gleichzeitig Android & Cross-Platform Mobile Apps 8
D Mehrere Kopien von einer Form Android & Cross-Platform Mobile Apps 7
AllBlack Auf der Suche nach einem App-Entwickler Android & Cross-Platform Mobile Apps 1
I Android Auf ImageView aus einem anderen Thread zugreifen liefert Fehlermeldung (App stürzt ab) Android & Cross-Platform Mobile Apps 5
ruutaiokwu Android In einem Android-“Spinner”-Element GLEICHZEITIG Bild (links) UND Text (rechts) anzeigen Android & Cross-Platform Mobile Apps 0
ruutaiokwu Wie fügt man bei Android Studio .jar-Libraries zu einem Android-Java-Projekt hinzu? Android & Cross-Platform Mobile Apps 33
L Eingaben in der MainActivity einem Service übergeben Android & Cross-Platform Mobile Apps 0
M Android App → Problem mit dem Speichern von einem Bitmap–Objekt. Android & Cross-Platform Mobile Apps 1
Alex/89 ImageView mit einem Bild von der SD Karte füllen Android & Cross-Platform Mobile Apps 5
G GPS in einem Service abfragen Android & Cross-Platform Mobile Apps 2
J wie sicher ist der Quellcode in einem apk? bzw wie schützt man ihn? Android & Cross-Platform Mobile Apps 6
B Android In einem View der ersten Activity zweite anzeigen Android & Cross-Platform Mobile Apps 2
A Android Von einem Thread auf anderen zugreifen Android & Cross-Platform Mobile Apps 3
W Android Bestimmen von welchem Typ die Objekte in einem ArrayAdapter sind Android & Cross-Platform Mobile Apps 3
X Android Befehle an einem PC-Programm schicken Android & Cross-Platform Mobile Apps 4
N Textview macht immer nach einem Beistrich einen Abstand Android & Cross-Platform Mobile Apps 6
S Android binäre Daten zwischen Android und einem Java-Server Android & Cross-Platform Mobile Apps 5
B Programm aus einem Buch funzt nicht! Android & Cross-Platform Mobile Apps 16
B Zeilen zählen in einem Textfile Android & Cross-Platform Mobile Apps 1
G Schriftgröße in einem TextField ändern Android & Cross-Platform Mobile Apps 2
A Problem beim Subtrahieren eines Double von einem Double Android & Cross-Platform Mobile Apps 5
U ein texteingabefeld auf einem Canvas . Android & Cross-Platform Mobile Apps 2
M Java Midlet -> in einem ChoiceGroup ein TextField zur Wah Android & Cross-Platform Mobile Apps 2
G Java Programme auf einem PDA Android & Cross-Platform Mobile Apps 1

Ähnliche Java Themen

Neue Themen


Oben