2016-03-01 83 views
0
對象後,將其存儲

我試圖解析Json類似如下:如何解析的Android的JSONObject與JsonArray存儲裏面RecyclerView

 { 
     "status": "ok", 
     "feed": { 
     "title": "title1", 
     "link": "http://www.link/", 
     "author": "", 
     "description": "desc", 
     "image": "" 
     }, 
     "items": [ 
     { 
      "title": "something", 
      "link": "something", 
      "guid": "something", 
      "pubDate": "date", 
      "categories": [], 
      "author": "", 
      "thumbnail": "", 
      "description": null, 
      "content": null 
     }, 
... 
... 

而且我從response得到預期的結果,但是,這是不是point.Here是我做過什麼:

try { 

      JSONObject response = new JSONObject(result); 

      JSONObject feed_object = response.getJSONObject("feed"); 
      PostItems item = new PostItems(); 
      item.setAuthor(feed_object.getString("title")); 

      JSONArray items_arr = response.getJSONArray("items"); 
      for (int i = 0; i < items_arr.length(); i++) { 
       JSONObject post = items_arr.getJSONObject(i); 

       item.setTitle(post.getString("title")); 
       item.setDate(post.getString("pubDate")); // As expected results 

       feedItem.add(item); 
      } 

     } 

的一點是,如果我存儲(第一object像我這樣做如上面的代碼RecyclerView顯示的項目,但沒有在項目內設置正確的titledate

如果我將其存儲在陣列內部和在此之後,設置一個陣列(對於array)內部和array外,它只是表示這樣的錯誤:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference 
       at java.io.StringReader.<init>(StringReader.java:47) 
       at android.text.HtmlToSpannedConverter.convert(Html.java:449) 
       at android.text.Html.fromHtml(Html.java:136) 
       at android.text.Html.fromHtml(Html.java:99) 
       at client.com.news.RecyclerViews.MainSanjeshRecyclerView.MyRecyclerAdapter.onBindViewHolder(MyRecyclerAdapter.java:34) 
       at client.com.news.RecyclerViews.MainSanjeshRecyclerView.MyRecyclerAdapter.onBindViewHolder(MyRecyclerAdapter.java:14) 
       at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5465) 
       at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5498) 
       at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4735) 
       at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4611) 
       at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1988) 
       at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1384) 
       at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1347) 
       at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574) 
       at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3026) 
       at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2903) 
       at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3277) 
       at android.view.View.layout(View.java:16630) 
       at android.view.ViewGroup.layout(ViewGroup.java:5437) 
       at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:596) 
       at android.view.View.layout(View.java:16630) 
       at android.view.ViewGroup.layout(ViewGroup.java:5437) 
       at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1684) 
       at android.view.View.layout(View.java:16630) 
       at android.view.ViewGroup.layout(ViewGroup.java:5437) 
       at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:122) 
       at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42) 
       at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1139) 
       at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:810) 
       at android.view.View.layout(View.java:16630) 
       at android.view.ViewGroup.layout(ViewGroup.java:5437) 
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) 
       at android.widget.FrameLayout.onLayout(FrameLayout.java:273) 
       at android.view.View.layout(View.java:16630) 
       at android.view.ViewGroup.layout(ViewGroup.java:5437) 
       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) 
       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) 
       at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) 
       at android.view.View.layout(View.java:16630) 
       at android.view.ViewGroup.layout(ViewGroup.java:5437) 
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) 
       at android.widget.FrameLayout.onLayout(FrameLayout.java:273) 
       at android.view.View.layout(View.java:16630) 
       at android.view.ViewGroup.layout(ViewGroup.java:5437) 
       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) 
       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) 
       at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) 
       at android.view.View.layout(View.java:16630) 
       at android.view.ViewGroup.layout(ViewGroup.java:5437) 
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) 
       at android.widget.FrameLayout.onLayout(FrameLayout.java:273) 
       at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678) 
       at android.view.View.layout(View.java:16630) 
       at android.view.ViewGroup.layout(ViewGroup.java:5437) 
       at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171) 
      at android.vi 

第二嘗試:

try { 

       JSONObject response = new JSONObject(result); 

       JSONObject feed_object = response.getJSONObject("feed"); 
       PostItems item = new PostItems(); 
       item.setAuthor(feed_object.getString("title")); 

       JSONArray items_arr = response.getJSONArray("items"); 
       for (int i = 0; i < items_arr.length(); i++) { 
        JSONObject post = items_arr.getJSONObject(i); 
        PostItems item2 = new PostItems(); 
        item2.setTitle(post.getString("title")); 
        item2.setDate(post.getString("pubDate")); 

        feedItem.add(item2); 
       } 
       feedItem.add(item); 
      } 

其實,我已經嘗試過什麼,你甚至可以把它想象,看到計算器上的任何問題,我不能沒有找到像這樣的問題,但有一次,只顯示一個項目沒有來自對象的正確數據,一次沒有顯示來自Array的正確數據。

任何幫助將是偉大的。

+0

什麼是行'MyRecyclerAdapter.java:34'? – Blackbelt

+0

這就是它,'feedListRowHolder.author.setText(Html.fromHtml(feedItem.getAuthor()));'但我認爲這不是重點,這只是當我試圖設置這條線兩次llike第二代碼:'feedItem.add(item2); feedItem.add(item);' – Mohsen

+0

'getAuthor'返回null。請注意:\t 'PostItems item = new PostItems();'應該在每個for循環的實例化處--blackbelt剛剛編輯 – Blackbelt

回答

1

您正在嘗試在每個項目中使用getAuthor(),當它清晰地設置爲ArrayList中的一個(第一個)項目時。當它沒有任何價值時,它返回null,產生NullPointerException。每次迭代都應該創建一個新的對象。

只是移動

PostItems item = new PostItems(); 
item.setAuthor(feed_object.getString("title")); 

for循環。

編輯

try { 
    JSONObject response = new JSONObject(result); 

    JSONObject feed_object = response.getJSONObject("feed"); 

    JSONArray items_arr = response.getJSONArray("items"); 
    for (int i = 0; i < items_arr.length(); i++) { 
     JSONObject post = items_arr.getJSONObject(i); 
     PostItems item = new PostItems(); 
     item.setAuthor(feed_object.getString("title")); // being fetched from top node 
     item.setTitle(post.getString("title")); // being fetched from array 
     item.setDate(post.getString("pubDate")); // being fetched from array 

     feedItem.add(item); 
    } 

} 
+0

沒有工作,這不是我在談論實際,請閱讀評論。 – Mohsen

+0

查看我的更新。錯過了一條線。 – Rohit5k2

+0

請將您的代碼與我的相匹配。試試這個代碼。 – Rohit5k2