我使用 GSON 解析 JSON 数据,我的应用程序目前能够解析 Google Geocoder。但是,每当我尝试从另一个网站解析 JSON 时,它都会给出 nullpointerexception
这是我的 EventResponse 类,它连接了 GSON 和网站
public class EventResponse {
public Meta meta;
public Objects[] Objects;
public EventResponse() {}
public class Meta {
public int limit;
public String next;
public int offset;
public String previous;
public int total_count;
}
public class Objects {
public String category;
public String date;
public String description;
public int id;
public String name;
public String resource_uri;
public String venue;
}
}
这是 JSON
{
"meta": {
"limit": 20,
"next": null,
"offset": 0,
"previous": null,
"total_count": 7
},
"objects": [
{
"category": "/api/v1/category/1/",
"date": "2013-05-01",
"description": "Beş yakın arkadaş, ", //Description is longer in real
"id": 10,
"name": "Kötü Ruh / Evil Dead",
"resource_uri": "/api/v1/event/10/",
"venue": "/api/v1/venue/3/"
},
{
"category": "/api/v1/category/1/",
"date": "2013-05-02",
"description": "Filmde, küçümseyen, ",
"id": 9,
"name": "Korkunç Bir Film 5 / Scary Movie 5",
"resource_uri": "/api/v1/event/9/",
"venue": "/api/v1/venue/6/"
}
]
}
这就是我使用 GSON 的方式(我也尝试过 Objects[0],没关系)
Gson gson = new Gson();
EventResponse eResult = new EventResponse();
try {
eResult = gson.fromJson(jsonCoord((catURL + Categories.catID)),
EventResponse.class);
Log.i("ama"+eResult.Objects[1].category, "deneme");
jsonCoord 方法
public String jsonCoord(String address) throws IOException {
URL url = new URL(address);
URLConnection connection = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String inputLine;
String jsonResult = "";
while ((inputLine = in.readLine()) != null) {
jsonResult += inputLine;
}
in.close();
return jsonResult;
}
即使从日志中我也无法获得任何价值。错误堆栈是这样的:(第59行是日志行)
05-02 21:48:32.020: W/System.err(17456): java.lang.NullPointerException
05-02 21:48:32.030: W/System.err(17456): at com.myapp.myapp.Events.getVenue(Events.java:59)
05-02 21:48:32.030: W/System.err(17456): at com.myapp.myapp.Events.onCreate(Events.java:48)
05-02 21:48:32.030: W/System.err(17456): at android.app.Activity.performCreate(Activity.java:5206)
05-02 21:48:32.030: W/System.err(17456): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
05-02 21:48:32.030: W/System.err(17456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
05-02 21:48:32.030: W/System.err(17456): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
05-02 21:48:32.030: W/System.err(17456): at android.app.ActivityThread.access$600(ActivityThread.java:140)
05-02 21:48:32.030: W/System.err(17456): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
05-02 21:48:32.030: W/System.err(17456): at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 21:48:32.030: W/System.err(17456): at android.os.Looper.loop(Looper.java:137)
05-02 21:48:32.030: W/System.err(17456): at android.app.ActivityThread.main(ActivityThread.java:4898)
05-02 21:48:32.030: W/System.err(17456): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 21:48:32.030: W/System.err(17456): at java.lang.reflect.Method.invoke(Method.java:511)
05-02 21:48:32.030: W/System.err(17456): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-02 21:48:32.035: W/System.err(17456): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-02 21:48:32.035: W/System.err(17456): at dalvik.system.NativeStart.main(Native Method)