简体   繁体   English

解析多个JSON URL

[英]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 我试图在我的应用程序中解析多个JSON URl并在一个列表视图中显示它们,但只有我能做的是显示一个URl,我想在一个列表视图中显示多个URl,并且URl具有相同的数组名称和属性,这是我的代码

  @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<>(); 你只需声明你的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. 在asynctask之外并在asynctask中使用相同的列表。如果你使用声明在asynctask里面的列表它重新初始化,并且从列表中清除从第一个asynctask存储的先前数据。

Just declare the list where you declare all variable above the create method. 只需声明在create方法上声明所有变量的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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