2016-04-22 11 views
0

Hallo Ich arbeite mit Android Volley, um JSON vom Server zu bekommen, dann parse es zum Objekt. Ich versuche, eine Klasse mit einer Methode zum Analysieren der JSON-Antwort zu erstellen. Meine URL sind zum Beispiel: https://hacker-news.firebaseio.com/v0/item/11544988.json?print=prettyFehler beim Parsen von JSON Android bei Verwendung der Klassenmethode

hier ist meine Klassenmethode (in TopStory.java)

public static TopStory fromJson(JSONObject jsonObject) { 
     TopStory topStory = new TopStory(); 

     // Deserialize json into object fields 
     try { 
      topStory.id = jsonObject.getInt(Constant.TAG_ID); 
      topStory.title = jsonObject.getString(Constant.TAG_TITLE); 
      topStory.author = jsonObject.getString(Constant.TAG_AUTHOR); 
      topStory.timestamp = jsonObject.getInt(Constant.TAG_TIMESTAMP); 
      topStory.url=jsonObject.getString(Constant.TAG_URL); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
      return null; 
     } 
     // Return new object 
     return topStory; 
    } 

im MainActivity. Ich benutze Volley, um die Antwort zu erhalten, und benutze die Klassenmethode, um sie zum Objekt zu analysieren. Ich kann die Antwort bekommen (kann einen Toast zur Anzeige bringen), aber erhielt Fehler beim Parsen: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.vic.hackernew.Model.TopStory.getAuthor()' on a null object reference. Jede Hilfe ist sehr zu schätzen. Vielen Dank.

private void getTopStoryDetail(RequestQueue requestQueue, String topStoryUrl) { 

     progressBar.setVisibility(View.VISIBLE); 
     CustomJsonObjectRequest jsonObjectRequest = new CustomJsonObjectRequest(Request.Method.GET,topStoryUrl,null, 
       new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject respond) { 
         progressBar.setVisibility(View.GONE); 

         Toast.makeText(getApplicationContext(),respond.toString(), Toast.LENGTH_LONG).show(); 

         TopStory topStory = TopStory.fromJson(respond); 

         topStories.add(topStory); 

         Toast.makeText(getApplicationContext(), topStory.getAuthor(), Toast.LENGTH_LONG).show(); 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show(); 
         progressBar.setVisibility(View.GONE); 
        } 
       }); 
     //Adding request to the queue 
     requestQueue.add(jsonObjectRequest); 
    } 

meine ganze TopStory.java

package com.vic.hackernew.Model; 

import android.widget.Toast; 

import com.vic.hackernew.Utils.Constant; 

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

import java.net.URL; 
import java.sql.Array; 
import java.sql.Timestamp; 
import java.util.ArrayList; 
import java.util.Arrays; 

/** 
* Created by vic on 21-Apr-16. 
*/ 
public class TopStory { 
    private int id; 
    private String title; 
    private String author; 
    private int score; 
    private int[] kids; 
    private int timestamp; 
    private String url; 

    public TopStory() { 
    } 

    public TopStory(int id, String title, String author, int point, int timestamp,String url) { 
     this.id = id; 
     this.title = title; 
     this.author = author; 
     this.score = point; 
     this.timestamp = timestamp; 
     this.url = url; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public String getAuthor() { 
     return author; 
    } 

    public void setAuthor(String author) { 
     this.author = author; 
    } 

    public int getScore() { 
     return score; 
    } 

    public void setScore(int score) { 
     this.score = score; 
    } 

    public int getTimestamp() { 
     return timestamp; 
    } 

    public void setTimestamp(int timestamp) { 
     this.timestamp = timestamp; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public int[] getKids() { 
     return kids; 
    } 

    public void setKids(int[] kids) { 
     this.kids = kids; 
    } 

    public String getUrl() { 
     return url; 
    } 

    public void setUrl(String url) { 
     this.url = url; 
    } 

    // Decodes business json into business model object 
    public static TopStory fromJson(JSONObject jsonObject) { 
     TopStory topStory = new TopStory(); 

     // Deserialize json into object fields 
     try { 
      topStory.id = jsonObject.getInt(Constant.TAG_ID); 
      topStory.title = jsonObject.getString(Constant.TAG_TITLE); 
      topStory.author = jsonObject.getString(Constant.TAG_AUTHOR); 
      topStory.timestamp = jsonObject.getInt(Constant.TAG_TIMESTAMP); 
      topStory.url=jsonObject.getString(Constant.TAG_URL); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
      return null; 
     } 
     // Return new object 
     return topStory; 
    } 

Mein Constanst.java

public class Constant { 
    public static String TAG_ID = "id"; 
    public static String TAG_TITLE = "title"; 
    public static String TAG_AUTHOR = "by"; 
    public static String TAG_KIDS = "kids"; 
    public static String TAG_SCORE = "point"; 
    public static String TAG_TIMESTAMP = "timestamp"; 
    public static String TAG_URL = "url"; 

    public static String TAG_CONTENT = "content"; 
    public static String TAG_BASE_URL = "https://hacker-news.firebaseio.com/v0/"; 
    public static String TAG_TOPSTORIES_URL = "topstories.json"; 
} 
+0

Gründe, warum Sie [Gson] nicht verwenden möchten (https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/Gson.html)? Es ist viel einfacher zu arbeiten mit – aProperFox

+0

post Ihre Modellklasse – mdDroid

+0

Hallo, ich habe meine gesamte TopStory.java –

Antwort

0

nur diese Zeile einfügen in das Parsen

topStory.author = jsonObject.optString(Constant.TAG_AUTHOR); 

statt

topStory.author = jsonObject.getString(Constant.TAG_AUTHOR); 

Wenn dies Ihr Problem löst, war das Problem in Ihrer Antwort mit der URL eines Elements.

So sah auf die json Sie receieving es

{ 

    "by": "maibaum", 
    "descendants": ​156, 
    "id": ​11544988, 
    "kids": 

    [ 
     ​11545261, 
     ​11546393, 
     ​11545187, 
     ​11546084, 
     ​11545168, 
     ​11546078, 
     ​11545846, 
     ​11546650, 
     ​11545285, 
     ​11545262, 
     ​11545178, 
     ​11546996, 
     ​11545394, 
     ​11546690, 
     ​11546275, 
     ​11545336, 
     ​11545515, 
     ​11545127, 
     ​11546000, 
     ​11546043, 
     ​11546842, 
     ​11546025, 
     ​11546221, 
     ​11545281, 
     ​11545142, 
     ​11546502, 
     ​11546215, 
     ​11546070, 
     ​11546257, 
     ​11546258, 
     ​11545195 
    ], 
    "score": ​238, 
    "time": ​1461269223, 
    "title": "FBI Paid More Than $1M to Hack San Bernardino iPhone", 
    "type": "story", 
    "url": "http://www.wsj.com/articles/comey-fbi-paid-more-than-1-million-to-hack-san-bernardino-iphone-1461266641" 

} 

, wenn Sie feststellen werden keine Autorenfeld hineinschauen ist. Deshalb bekommst du null. Auch ich sehe "durch" in dieser Antwort, wenn es der Autor ist, dann sollten Sie Ihren Constant.Author zu "durch" anstelle von "Autor" ändern, das Ihr Problem lösen kann. Ich hoffe, es wäre hilfreich.

+0

Hallo, ich denke nicht das Problem. mein Constant.TAG_AUTHOR = "von". Ich kann Autor durch Versuch erhalten { Toast.makeText (getApplicationContext(), reply.getString (Constant.TAG_AUTHOR), Toast.LENGTH_LONG) .show(); } catch (JSONException e) { e.printStackTrace(); } –

+0

fügen Sie die Konstanten Klasse –

+0

Hi ive fügen Sie die constanst.java auf die Frage –

Verwandte Themen