简体   繁体   中英

parsing Multiple JSON Url

I am trying to parse multiple JSON URl in my app and show them in one list view but only what i can do is showing one URl and i want to show Multiple URl,s in one list view and the URl have the same array names and attributes and here is my code

  @Override
public void onStart() {
    super.onStart();
    if(connectiondetector.isConnected()){
        new jsontask().execute(

                "https://newsapi.org/v1/articles?source=google-news&sortBy=top&apiKey=ade8f00a634b4825a028837ec107afae"

        );

    }else {
        Toast.makeText(page.this, "no internet connection",Toast.LENGTH_SHORT).show();

    }


    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.

}

@Override
public void onStop() {
    super.onStop();


}



public class jsontask extends AsyncTask<String, String, List<moviemodel>> {


    @Override
    protected List<moviemodel> doInBackground(String... params) {
        BufferedReader reader = null;
        HttpURLConnection connection = null;
        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            InputStream stream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line);

            }


            String finaljson = buffer.toString();
            JSONObject parentobject = new JSONObject(finaljson);

           // JSONObject parentobject1 = parentobject.getJSONObject("");
            JSONArray parentarray = parentobject.getJSONArray("articles");
            List<moviemodel>moviemodelList =  new ArrayList<>();
            for (int i = 0; i < parentarray.length(); i++){
                JSONObject finalobject = parentarray.getJSONObject(i);
                moviemodel moviemodel = new moviemodel();
                moviemodel.setAuthor(finalobject.getString("author"));
                moviemodel.setDescription(finalobject.getString("description"));
                moviemodel.setTitle(finalobject.getString("title"));
                moviemodel.setImage(finalobject.getString("urlToImage"));
                moviemodel.setUrl(finalobject.getString("url"));
                moviemodel.setPublishedAt(finalobject.getString("publishedAt"));



                moviemodelList.add(moviemodel);


            }

            return moviemodelList;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


        return null;
    }



    @Override
    protected void onPostExecute(List<moviemodel> result) {


        super.onPostExecute(result);
        newsadapter adapter = new newsadapter(getApplicationContext(),R.layout.row, result);
        lvnews.setAdapter(adapter);
        lvnews.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
                {
                    if(flag_loading == false)
                    {
                        flag_loading = true;
                        new jsontask().execute("https://newsapi.org/v1/articles?source=ars-technica&sortBy=top&apiKey=ade8f00a634b4825a028837ec107afae")

                        ;}
                }
            }
        });
    }

}

you just Declare your List<moviemodel>moviemodelList = new ArrayList<>(); out of the asynctask and used the same list in both asynctask.If you use declare the list inside asynctask it re-initialized and the previous data which store from first asynctask clear from list.

Just declare the list where you declare all variable above the create method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM