从NYT API获取大嵌套Json

时间:2018-09-23 00:52:59

标签: android json api

我正在尝试使用纽约时报api在Android的列表视图中显示标题和图像。我已经获取标题和日期的数据,并输出到log cat。真正的问题是:每当我输入多余的字段来获取多媒体(图像网址)时,就会显示“ GC_FOR_ALLOC已释放1902K(64),其中22%是免费的11931K / 15156K”

我已经看到api总是发送固定数量的10个nyt文章,但是10个中的每一个都有多个多媒体URL和带有嵌套数组的json,并且该应用程序冻结。我将学习自己将数据放在listview上,我的真正问题是,解决崩溃的最佳方法是什么,每篇文章只需要1个url图片。

//仅具有“多媒体”字段查询的返回的JSON。

{
  "status": "OK",
  "copyright": "Copyright (c) 2018 The New York Times Company. All Rights 
   Reserved.",
  "response": {
    "docs": [
      {
        "multimedia": [
          {
            "rank": 0,
            "subtype": "xlarge",
            "caption": null,
            "credit": null,
            "type": "image",
            "url": "images/2018/09/07/opinion/07Parcak/merlin_143215881_20cecc31-7c8d-4b8f-8541-773170c1822c-articleLarge.jpg",
            "height": 400,
//MORE CODE HERE.. THEN NEXT ARRAY BELOW
  {
    "multimedia": [
      {
        "rank": 0,
        "subtype": "xlarge",
        "caption": null,
        "credit": null,
        "type": "image",
        "url": "images/2018/09/03/world/03xp-brazil-promo/03xp-brazil-promo-articleLarge.jpg",
        "height": 400,
        "width": 600,
        "legacy": {
          "xlarge": "images/2018/09/03/world/03xp-brazil-promo/03xp-brazil-promo-articleLarge.jpg",
          "xlargewidth": 600,
          "xlargeheight": 400
        },
        "subType": "xlarge",
        "crop_name": "articleLarge"
      },
      // Goes on forever. 

1 个答案:

答案 0 :(得分:0)

您使用什么来获取数据,您使用哪种REST客户端?

例如,如果您自己分析响应,则可以添加诸如

的逻辑
package etes.xdda.music;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;

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

import java.util.ArrayList;
import java.util.List;

public class fragmentList extends Fragment {

    View v;
    private RecyclerView myrecyclerview;
    private List<mList> lstContact;

    private String URL_JSON = "https://pastebin.com/raw/fG3zd40U";
    private JsonArrayRequest ArrayRequest;
    private RequestQueue requestQueue ;
    MainActivity activity;

    public fragmentList() {
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.list_fragment,container,false);
        myrecyclerview = (RecyclerView) v.findViewById(R.id.list_recyclerview);
        RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(activity, lstContact);
        myrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        myrecyclerview.setAdapter(recyclerViewAdapter);
        return v;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstaceState) {
        super.onCreate(savedInstaceState);
        _JSONcall();
    }


    public void _JSONcall() {

        lstContact = new ArrayList<>();
        ArrayRequest = new JsonArrayRequest(URL_JSON, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {

                JSONObject jsonObject = null;

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

                        jsonObject = response.getJSONObject(i);

                        lstContact.add(new mList(jsonObject.getString("name"), jsonObject.getString("description"), jsonObject.getString("link"), R.drawable.ic_play_circle_filled_black_24dp));
                    }
                    catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getActivity(), "Error 1!",
                                Toast.LENGTH_LONG).show();
                    }
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getActivity(), "Error 3!",
                        Toast.LENGTH_LONG).show();
            }
        });


        requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
        requestQueue.add(ArrayRequest);
    }
}