こんにちは私はアンドロイドVolley
を使ってサーバからJSON
を取得し、それをオブジェクトに解析しています。 JSONレスポンスを解析するメソッドを持つクラスを作成しようとしています。私のURLは、例えば、次のとおりです。https://hacker-news.firebaseio.com/v0/item/11544988.json?print=pretty
クラスメソッドを使用するときにJSON androidを解析する際にエラーが発生しました
はこちら(TopStory.java
で)私のクラスメソッドMainActivityで
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;
}
です。私はVolleyを使ってレスポンスを取得し、クラスメソッドを使用してオブジェクトを解析します。私は応答を得ることができます(表示するトーストを作ることができます)が、解析時にエラーが発生しました:java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.vic.hackernew.Model.TopStory.getAuthor()' on a null object reference
。どんな助けでも大変感謝しています。ありがとう。
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);
}
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;
}
マイ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";
}
[Gson](https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/Gson.html)を使用しない理由は何ですか? – aProperFox
あなたのモデルクラスを投稿する – mdDroid
こんにちは、私はTopStory.java全体を追加しました –