如何从父json中的链接正确解析和显示json

时间:2016-04-20 13:59:50

标签: java android json android-recyclerview

我正在解析这个json:

[
  {
    "title": American Government,
    "type": "Democracy",
    "independent": {
          "from": [
            {
              "Britain"
            }
          ],
          "date": [
            {
              "by": "Britain",
              "day_real": "http://example.com/json/independent"
            }
          ],

        },
    }
  ...]

" day_real"的价值导致另一个json看起来像这样:

{
    "year": 1800,
    "from": "Brtain"
    }

我的目标是解析两个json并在Recyclerview中显示它。

这些是我的课程。

MainActivity

public class MainActivity extends AppCompatActivity {

     private final String TAG = "MainActivity";
     private String govDate;

     //Creating a list of govns
     private List<GovnItems> mGovnItemsList;

     //Creating Views
     private RecyclerView recyclerView;
     private RecyclerView.Adapter adapter;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         Log.d(TAG, "onCreate called");

         //Initializing Views
         recyclerView = (RecyclerView) findViewById(R.id.govn_recycler);
         LinearLayoutManager layoutManager = new LinearLayoutManager(this);
         recyclerView.setLayoutManager(layoutManager);

         adapter = new GovnAdapter(mGovnItemsList, this);

         recyclerView.setAdapter(adapter);
    getData ();

     }



     //This method will get data from the web api

     private void getData(){
         Log.d(TAG, "getData called");

         //Creating a json request
         JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(ConfigGovn.GET_URL,
                 new Response.Listener<JSONArray>() {
                     @Override
                     public void onResponse(JSONArray response) {
                         Log.d(TAG, "onResponse called");

                         //calling method to parse json array
                         parseData(response);

                     }
                 },
                 new Response.ErrorListener() {
                     @Override
                     public void onErrorResponse(VolleyError error) {
                     }
                 });

         //Creating request queue
         RequestQueue requestQueue = Volley.newRequestQueue(this);

         //Adding request to the queue
         requestQueue.add(jsonArrayRequest);

     }


     //This method will parse json data
     private void parseData(JSONArray array){
         Log.d(TAG, "Parsing array");


         for(int i = 0; i<array.length(); i++) {
             GovnItems govnItem = new GovnItems();
             JSONObject jsonObject = null;
             try {
                 jsonObject = array.getJSONObject(i);

                 govnItem.setGovnTitle(jsonObject.getString("title"));
                 govnItem.setGovnType(jsonObject.getString("type"));

                JSONObject govnInde = jsonObject.getJSONObject("independent");
                JSONArray govnDate = govnInde.getJSONArray("date");
                String dayUrl = govnDate.getJSONObject(0).getString("day_real");
                govDate = dayUrl;
             } catch (JSONException w) {
                 w.printStackTrace();
             }

             mGovnItemsList.add(govnItem);
             loadDate();
         }

         adapter.notifyItemRangeChanged(0, adapter.getItemCount());


     }

     private void loadDate() {
         Log.d(TAG, "Loading Date");

         final JsonObjectRequest dateObjectRequest = new JsonObjectRequest(govDate, null,
                 new Response.Listener<JSONObject>() {
                     @Override
                     public void onResponse(JSONObject response) {
                         Log.d("Debug", response.toString());
                         parseDate(response);
                     }
                 },
                 new Response.ErrorListener() {
                     @Override
                     public void onErrorResponse(VolleyError error) {
                         VolleyLog.d("", "Error: " + error.getMessage());
                     }
                 });
         //Creating resquestqueue
         RequestQueue dateRequest = Volley.newRequestQueue(this);

         //Adding request to queue
         dateRequest.add(dateObjectRequest);
     }

     //This method will parse date
     private void parseDate(JSONObject authJsonObject) {
         Log.d(TAG, "Parsing Indep day");
         GovnItems dateItem = new GovnItems();

         try {
             String dateFrom = authJsonObject.getString("from");
             dategovnItem.setGovn_author(dateFrom);
             Log.d(TAG, authorName);
         } catch (JSONException w) {
             w.printStackTrace();
         }
         mGovnItemsList.add(dateItem);

     }
 }

GovnAdapter

public class GovnAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

     private Context mContext;

     //List of govns
     private List<GovnItems> mGovnItems;

     private final int VIEW_ITEM = 0;
     private final int VIEW_PROG = 1;

     private int lastPosition = -1;

    public GovnAdapter(List<GovnItems> govnItems, Context context) {
        super();

        //Getting all govns
        this.mGovnItems = govnItems;
        this.mContext = context;
    }

     @Override
     public int getItemViewType(int position) {
         if (isPositionItem(position))
             return VIEW_ITEM;
         return VIEW_PROG;
     }

     private boolean isPositionItem(int position) {
        return position != getItemCount()-1;
     }



     @Override
     public RecyclerView.ViewHolder  onCreateViewHolder (ViewGroup parent, int viewType) {
         if (viewType == VIEW_ITEM) {
             View v =  LayoutInflater.from(parent.getContext())
                     .inflate(R.layout.govn_summ, parent, false);
             return new TextViewHolder(v);
         } else if (viewType == VIEW_PROG){
             View v = LayoutInflater.from(parent.getContext())
                     .inflate(R.layout.recyclerfooter, parent, false);
             return new ProgressViewHolder(v);
         }

         return null;
     }



     @Override
     public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

         if (holder instanceof TextViewHolder) {

             final GovnItems govnList = mGovnItems.get(position);
             ((TextViewHolder) holder).govnTitle.setText(Html.fromHtml(govnList.getGovn_title()));
             ((TextViewHolder) holder).govnType.setText(Html.fromHtml(govnList.getGovn_type()));
             ((TextViewHolder) holder).govnDate.setText(govnList.getGovn_date());

         } else {
             ((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
             ((ProgressViewHolder) holder).loadButton.setText(R.string.reload);
         }


     }


     @Override
     public int getItemCount(){
         //Return the number of items in the data set
         return mGovnItems.size();
     }


     public static class TextViewHolder extends RecyclerView.ViewHolder{ 
         public TextView govnTitle,govnType, govnDate; 

         public TextViewHolder (final View govnView) {
             super(govnView);
             govnTitle = (TextView) govnView.findViewById(R.id.govn_title);
             govnType = (TextView) govnView.findViewById(R.id.govn_type);
             govnDate = (TextView) govnView.findViewById(R.id.govn_date);

         }


     public static class ProgressViewHolder extends RecyclerView.ViewHolder {
         Button loadButton;
         ProgressBar progressBar;
         public ProgressViewHolder(View footerView){
             super(footerView);
             loadButton = (Button) footerView.findViewById(R.id.reload_button);
             progressBar = (ProgressBar) footerView.findViewById(R.id.progress_load);
         } 
     }


 }

我对此代码所遇到的问题是从&#34;获得&#34;的价值。显示。它只是没有显示。从我的logcat中,我可以看到日期确实已被提取,而且#34;来自&#34;正在成功解析,但它只是不会显示。 我已经调试和调试但没有任何线索。

拜托,您知道我应该如何解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

仅在完全从服务器获取数据后使用以下代码

adapter = new GovnAdapter(mGovnItemsList, this);

recyclerView.setAdapter(adapter);

答案 1 :(得分:0)

我没有编译你的代码,但如果我没有错,这就是问题:

String dayUrl = govnDate.getJSONObject(0).getString("day_real");
govDate = dateUrl;

应为govDate = dayUrl