4

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")

                        ;}
                }
            }
        });
    }

}

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.