仅对某些 GSON 方法调用返回 JsonObject Null
Returned JsonObject Null for only some GSON method calls
编辑了我的问题:
我一直在从事我的第一个编程和 Android 项目。
在下面的代码中我收到一个:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.gson.JsonElement.toString()' on a null object reference
试图在我的 result JSONOBJECT 上调用一个方法,它是使用网络库检索的:https://github.com/koush/ion 并且正在使用 google GSON 库。
toast 字符串消息和 log.v 显示 JSON 对象已成功传递,其中包含所有必要的元素。(或者可能未完全传递?Log.v 削减一段时间后关闭。)
我的困惑在于能够在 result 上调用 toString() GSON 方法,这会导致成功的 toast 消息,同时尝试调用任何其他 GSON 方法,例如 result.get(images_count).toString 导致我的错误。我尝试在执行我的代码之前使用 if(result.isJsonNull()) 来确保结果不为空,但即使 result.isJsonNull() returns false 我的方法调用 return是错误。
我的 2 部分问题
是什么导致我的 returned JsonObject result 成功执行某些 GSON 库方法(如 toString()),而 return当我尝试通过 .get(string member)?
访问 json 元素成员时,为其他人设置 NullPointer 异常
将来,我如何 place/recognize 检查点对象可能 return NullPointerException?到目前为止,我只使用 Logging 或 Toasts 来显示当前 object/variable 值。感谢布朗克斯的回答。我必须使用断点。
导致 NullPointer 异常的代码:(我已将我的 CLient-ID 替换为 xxxxxxx,以便对 Internet 保密)。
Future<JsonObject> loading;
private void load() {
// don't attempt to load more if a load is already in progress
if (loading != null && !loading.isDone() && !loading.isCancelled())
return;
// load the tweets from album : gQxwy https://api.imgur.com/3/album/{id}
//https://api.imgur.com/3/album/{id}/images
String url = "https://api.imgur.com/3/album/gQxwy.json";
// This request loads a URL as JsonObject and invokes
// a callback on completion.
loading = Ion.with(this)
.load(url)
.setHeader("Authorization", "Client-ID " + "xxxxxxxxxxxxxxx")
.setLogging("ION_VERBOSE_LOGGING", Log.VERBOSE)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
// this is called back onto the ui thread, no Activity.runOnUiThread or Handler.post necessary.
if (e != null) {
Toast.makeText(Imgur.this, "Error loading tweets", Toast.LENGTH_LONG).show();
return;
}
//Here i test if the object is Null.It goes directly to excute else, showing that the object is not Null.
if(result.isJsonNull()){
Toast.makeText(Imgur.this, result.toString(), Toast.LENGTH_LONG).show();
}else {
Log.v("JSONOBJECT_STRING", result.toString());
Toast.makeText(Imgur.this, result.get("images_count").toString(), Toast.LENGTH_LONG).show();
}
}
});
}
这里是 LogCat,其中包含成功检索到的 JSON 对象(我们可以在日志中看到 JSON 对象。) Json 对象确实包含元素 images_count。
LogCat:
03-21 21:24:26.395 8035-8061/? D/ION_VERBOSE_LOGGING﹕ (353 ms) https://api.imgur.com/3/album/gQxwy.json: Connection successful
03-21 21:24:26.405 8035-8061/? D/ION_VERBOSE_LOGGING﹕ (363 ms) https://api.imgur.com/3/album/gQxwy.json: Recycling keep-alive socket
03-21 21:24:26.407 8035-8035/? V/JSONOBJECT_STRING﹕ {"data":{"id":"gQxwy","title":"playlist","description":null,"datetime":1425872636,"cover":"c7iJOmN","cover_width":160,"cover_height":120,"account_url":"JohnnyJem","account_id":18800283,"privacy":"public","layout":"blog","views":574,"link":"http://imgur.com/a/gQxwy","favorite":false,"nsfw":null,"section":null,"images_count":10,"images":[{"id":"c7iJOmN","title":"Title","description":"Description","datetime":1425872636,"type":"image/gif","animated":true,"width":160,"height":120,"size":65008,"views":1175,"bandwidth":76384400,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/c7iJOmN.gifv","webm":"http://i.imgur.com/c7iJOmN.webm","mp4":"http://i.imgur.com/c7iJOmN.mp4","link":"http://i.imgur.com/c7iJOmN.gif","looping":true},{"id":"SYRfZPy","title":"Title","description":"Description","datetime":1425872638,"type":"image/gif","animated":true,"width":160,"height":120,"size":139574,"views":1054,"bandwidth":147110996,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/SYRfZPy.gifv","webm":"http://i.imgur.com/SYRfZPy.webm","mp4":"http://i.imgur.com/SYRfZPy.mp4","link":"http://i.imgur.com/SYRfZPy.gif","looping":true},{"id":"1aD2tdR","title":"Title","description":"Description","datetime":1425872639,"type":"image/gif","animated":true,"width":160,"height":120,"size":117092,"views":882,"bandwidth":103275144,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/1aD2tdR.gifv","webm":"http://i.imgur.com/1aD2tdR.webm","mp4":"http://i.imgur.com/1aD2tdR.mp4","link":"http://i.imgur.com/1aD2tdR.gif","looping":true},{"id":"7UzUF79","title":"Title","description":"Description","datetime":1425872641,"type":"image/gif","animated":true,"width":160,"height":120,"size":146703,"views":785,"bandwidth":115161855,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/7UzUF79.gifv","webm":"http://i.imgur.com/7UzUF79.webm","mp4":"http://i.imgur.com/7UzUF79.mp4","link":"http://i.imgur.com/7UzUF79.gif","looping":true},{"id":"3PKeSiW","title":"Title","description":"Description","datetime":1425872642,"type":"image/gif","animated":true,"width":160,"height":120,"size":136914,"views":753,"bandwidth":103096242,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/3PKeSiW.gifv","webm":"http://i.imgur.com/3PKeSiW.webm","mp4":"http://i.imgur.com/3PKeSiW.mp4","link":"http://i.imgur.com/3PKeSiW.gif","looping":true},{"id":"o3UBVgR","title":"Title","description":"Description","datetime":1425872644,"type":"image/gif","animated":true,"width":160,"height":120,"size":129046,"views":483,"bandwidth":62329218,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/o3UBVgR.gifv","webm":"http://i.imgur.com/o3UBVgR.webm","mp4":"http://i.imgur.com/o3UBVgR.mp4","link":"http://i.imgur.com/o3UBVgR.gif","looping":true},{"id":"qraDqOQ","title":"Title","description":"Description","datetime":1425872645,"type":"image/gif","animated":true,"width":160,"height":120,"size":93116,"views":466,"bandwidth":43392056,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/qraDqOQ.gifv","webm":"http://i.imgur.com/qraDqOQ.webm","mp4":"http://i.imgur.com/qraDqOQ.mp4","link":"http://i.imgur.com/qraDqOQ.gif","looping":true},{"id":"K9PuB3S","title":"Title","description":"Description","datetime":1425872647,"type":"image/gif","animated":true,"width":160,"height":120,"size":84323,"views":481,"bandwidth":40559363,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/K9PuB3S.gifv","webm":"http://i.imgur.com/K9PuB3S.webm","mp4":"http://i.imgur.com/K9PuB3S.mp4","link":"http://i.imgur.com/K9PuB3S.gif","looping":true},{"id":"p3whCAz","title":"Title","descriptio
03-21 21:24:26.408 8035-8035/? D/AndroidRuntime﹕ Shutting down VM
03-21 21:24:26.408 8035-8035/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.johnnymolina.nextphase, PID: 8035
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.gson.JsonElement.toString()' on a null object reference
at com.johnnymolina.nextphase.activities.Imgur.onCompleted(Imgur.java:116)
at com.johnnymolina.nextphase.activities.Imgur.onCompleted(Imgur.java:98)
at com.koushikdutta.async.future.SimpleFuture.handleCallbackUnlocked(SimpleFuture.java:107)
at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:141)
at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:128)
at com.koushikdutta.ion.IonRequestBuilder.run(IonRequestBuilder.java:246)
at com.koushikdutta.async.AsyncServer$RunnableWrapper.run(AsyncServer.java:57)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-21 21:24:26.411 6534-6864/? W/ActivityManager﹕ Force finishing activity com.johnnymolina.nextphase/.activities.Imgur
03-21 21:24:26.412 6534-6864/? W/ActivityManager﹕ Force finishing activity com.johnnymolina.nextphase/.activities.MainActivity
好的,查看 logCat 似乎没有打印出整个 JSon 对象。这是 Log.v 可以打印的限制吗?还是因为网络调用不是 return 一个完全完整的对象,所以我得到一个 Null 对象引用?
从 Imgur 的 Api 中检索到的 Json 对象的示例:https://api.imgur.com/models/album
{
"data": {
"id": "lDRB2",
"title": "Imgur Office",
"description": null,
"datetime": 1357856292,
"cover": "24nLu",
"account_url": "Alan",
"account_id": 4,
"privacy": "public",
"layout": "blog",
"views": 13780,
"link": "http://alanbox.imgur.com/a/lDRB2",
"images_count": 11,
"images": [
{
"id": "24nLu",
"title": null,
"description": null,
"datetime": 1357856352,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 855658,
"views": 135772,
"bandwidth": 116174397976,
"link": "http://i.imgur.com/24nLu.jpg"
},
{
"id": "Ziz25",
"title": null,
"description": null,
"datetime": 1357856394,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 919391,
"views": 135493,
"bandwidth": 124571044763,
"link": "http://i.imgur.com/Ziz25.jpg"
},
{
"id": "9tzW6",
"title": null,
"description": null,
"datetime": 1357856385,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 655028,
"views": 135063,
"bandwidth": 88470046764,
"link": "http://i.imgur.com/9tzW6.jpg"
},
{
"id": "dFg5u",
"title": null,
"description": null,
"datetime": 1357856378,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 812738,
"views": 134704,
"bandwidth": 109479059552,
"link": "http://i.imgur.com/dFg5u.jpg"
},
{
"id": "oknLx",
"title": null,
"description": null,
"datetime": 1357856338,
"type": "image/jpeg",
"animated": false,
"width": 1749,
"height": 2332,
"size": 717324,
"views": 32938,
"bandwidth": 23627217912,
"link": "http://i.imgur.com/oknLx.jpg"
},
{
"id": "OL6tC",
"title": null,
"description": null,
"datetime": 1357856321,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 1443262,
"views": 32346,
"bandwidth": 46683752652,
"link": "http://i.imgur.com/OL6tC.jpg"
},
{
"id": "cJ9cm",
"title": null,
"description": null,
"datetime": 1357856330,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 544702,
"views": 31829,
"bandwidth": 17337319958,
"link": "http://i.imgur.com/cJ9cm.jpg"
},
{
"id": "7BtPN",
"title": null,
"description": null,
"datetime": 1357856369,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 844863,
"views": 31257,
"bandwidth": 26407882791,
"link": "http://i.imgur.com/7BtPN.jpg"
},
{
"id": "42ib8",
"title": null,
"description": null,
"datetime": 1357856424,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 905073,
"views": 30945,
"bandwidth": 28007483985,
"link": "http://i.imgur.com/42ib8.jpg"
},
{
"id": "BbwIx",
"title": null,
"description": null,
"datetime": 1357856360,
"type": "image/jpeg",
"animated": false,
"width": 1749,
"height": 2332,
"size": 662413,
"views": 30107,
"bandwidth": 19943268191,
"link": "http://i.imgur.com/BbwIx.jpg"
},
{
"id": "x7b91",
"title": null,
"description": null,
"datetime": 1357856406,
"type": "image/jpeg",
"animated": false,
"width": 1944,
"height": 2592,
"size": 618567,
"views": 29259,
"bandwidth": 18098651853,
"link": "http://i.imgur.com/x7b91.jpg"
}
]
},
"success": true,
"status": 200
}
如果您可以 post 一个 json 响应的示例,我们可以更好地帮助您。
但是据我所知,JsonElement "images_count" 不存在,因此您会得到 NullPointerException。
据我所知,如果我尝试从浏览器进行调用,我会收到此 json 响应:
{"data":{"error":"Authentication required","request":"\/3\/album\/gQxwy.json","method":"GET"},"success":false,"status":401}
您可能没有添加来自 WebService 的一些查询参数请求。
P.S.: 要显示当前 object/variable 值,您需要使用断点并且 运行 您的应用程序处于调试模式
编辑了我的问题:
我一直在从事我的第一个编程和 Android 项目。
在下面的代码中我收到一个:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.gson.JsonElement.toString()' on a null object reference
试图在我的 result JSONOBJECT 上调用一个方法,它是使用网络库检索的:https://github.com/koush/ion 并且正在使用 google GSON 库。
toast 字符串消息和 log.v 显示 JSON 对象已成功传递,其中包含所有必要的元素。(或者可能未完全传递?Log.v 削减一段时间后关闭。)
我的困惑在于能够在 result 上调用 toString() GSON 方法,这会导致成功的 toast 消息,同时尝试调用任何其他 GSON 方法,例如 result.get(images_count).toString 导致我的错误。我尝试在执行我的代码之前使用 if(result.isJsonNull()) 来确保结果不为空,但即使 result.isJsonNull() returns false 我的方法调用 return是错误。
我的 2 部分问题
是什么导致我的 returned JsonObject result 成功执行某些 GSON 库方法(如 toString()),而 return当我尝试通过 .get(string member)?
访问 json 元素成员时,为其他人设置 NullPointer 异常将来,我如何 place/recognize 检查点对象可能 return NullPointerException?到目前为止,我只使用 Logging 或 Toasts 来显示当前 object/variable 值。感谢布朗克斯的回答。我必须使用断点。
导致 NullPointer 异常的代码:(我已将我的 CLient-ID 替换为 xxxxxxx,以便对 Internet 保密)。
Future<JsonObject> loading;
private void load() {
// don't attempt to load more if a load is already in progress
if (loading != null && !loading.isDone() && !loading.isCancelled())
return;
// load the tweets from album : gQxwy https://api.imgur.com/3/album/{id}
//https://api.imgur.com/3/album/{id}/images
String url = "https://api.imgur.com/3/album/gQxwy.json";
// This request loads a URL as JsonObject and invokes
// a callback on completion.
loading = Ion.with(this)
.load(url)
.setHeader("Authorization", "Client-ID " + "xxxxxxxxxxxxxxx")
.setLogging("ION_VERBOSE_LOGGING", Log.VERBOSE)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
// this is called back onto the ui thread, no Activity.runOnUiThread or Handler.post necessary.
if (e != null) {
Toast.makeText(Imgur.this, "Error loading tweets", Toast.LENGTH_LONG).show();
return;
}
//Here i test if the object is Null.It goes directly to excute else, showing that the object is not Null.
if(result.isJsonNull()){
Toast.makeText(Imgur.this, result.toString(), Toast.LENGTH_LONG).show();
}else {
Log.v("JSONOBJECT_STRING", result.toString());
Toast.makeText(Imgur.this, result.get("images_count").toString(), Toast.LENGTH_LONG).show();
}
}
});
}
这里是 LogCat,其中包含成功检索到的 JSON 对象(我们可以在日志中看到 JSON 对象。) Json 对象确实包含元素 images_count。 LogCat:
03-21 21:24:26.395 8035-8061/? D/ION_VERBOSE_LOGGING﹕ (353 ms) https://api.imgur.com/3/album/gQxwy.json: Connection successful
03-21 21:24:26.405 8035-8061/? D/ION_VERBOSE_LOGGING﹕ (363 ms) https://api.imgur.com/3/album/gQxwy.json: Recycling keep-alive socket
03-21 21:24:26.407 8035-8035/? V/JSONOBJECT_STRING﹕ {"data":{"id":"gQxwy","title":"playlist","description":null,"datetime":1425872636,"cover":"c7iJOmN","cover_width":160,"cover_height":120,"account_url":"JohnnyJem","account_id":18800283,"privacy":"public","layout":"blog","views":574,"link":"http://imgur.com/a/gQxwy","favorite":false,"nsfw":null,"section":null,"images_count":10,"images":[{"id":"c7iJOmN","title":"Title","description":"Description","datetime":1425872636,"type":"image/gif","animated":true,"width":160,"height":120,"size":65008,"views":1175,"bandwidth":76384400,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/c7iJOmN.gifv","webm":"http://i.imgur.com/c7iJOmN.webm","mp4":"http://i.imgur.com/c7iJOmN.mp4","link":"http://i.imgur.com/c7iJOmN.gif","looping":true},{"id":"SYRfZPy","title":"Title","description":"Description","datetime":1425872638,"type":"image/gif","animated":true,"width":160,"height":120,"size":139574,"views":1054,"bandwidth":147110996,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/SYRfZPy.gifv","webm":"http://i.imgur.com/SYRfZPy.webm","mp4":"http://i.imgur.com/SYRfZPy.mp4","link":"http://i.imgur.com/SYRfZPy.gif","looping":true},{"id":"1aD2tdR","title":"Title","description":"Description","datetime":1425872639,"type":"image/gif","animated":true,"width":160,"height":120,"size":117092,"views":882,"bandwidth":103275144,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/1aD2tdR.gifv","webm":"http://i.imgur.com/1aD2tdR.webm","mp4":"http://i.imgur.com/1aD2tdR.mp4","link":"http://i.imgur.com/1aD2tdR.gif","looping":true},{"id":"7UzUF79","title":"Title","description":"Description","datetime":1425872641,"type":"image/gif","animated":true,"width":160,"height":120,"size":146703,"views":785,"bandwidth":115161855,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/7UzUF79.gifv","webm":"http://i.imgur.com/7UzUF79.webm","mp4":"http://i.imgur.com/7UzUF79.mp4","link":"http://i.imgur.com/7UzUF79.gif","looping":true},{"id":"3PKeSiW","title":"Title","description":"Description","datetime":1425872642,"type":"image/gif","animated":true,"width":160,"height":120,"size":136914,"views":753,"bandwidth":103096242,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/3PKeSiW.gifv","webm":"http://i.imgur.com/3PKeSiW.webm","mp4":"http://i.imgur.com/3PKeSiW.mp4","link":"http://i.imgur.com/3PKeSiW.gif","looping":true},{"id":"o3UBVgR","title":"Title","description":"Description","datetime":1425872644,"type":"image/gif","animated":true,"width":160,"height":120,"size":129046,"views":483,"bandwidth":62329218,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/o3UBVgR.gifv","webm":"http://i.imgur.com/o3UBVgR.webm","mp4":"http://i.imgur.com/o3UBVgR.mp4","link":"http://i.imgur.com/o3UBVgR.gif","looping":true},{"id":"qraDqOQ","title":"Title","description":"Description","datetime":1425872645,"type":"image/gif","animated":true,"width":160,"height":120,"size":93116,"views":466,"bandwidth":43392056,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/qraDqOQ.gifv","webm":"http://i.imgur.com/qraDqOQ.webm","mp4":"http://i.imgur.com/qraDqOQ.mp4","link":"http://i.imgur.com/qraDqOQ.gif","looping":true},{"id":"K9PuB3S","title":"Title","description":"Description","datetime":1425872647,"type":"image/gif","animated":true,"width":160,"height":120,"size":84323,"views":481,"bandwidth":40559363,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/K9PuB3S.gifv","webm":"http://i.imgur.com/K9PuB3S.webm","mp4":"http://i.imgur.com/K9PuB3S.mp4","link":"http://i.imgur.com/K9PuB3S.gif","looping":true},{"id":"p3whCAz","title":"Title","descriptio
03-21 21:24:26.408 8035-8035/? D/AndroidRuntime﹕ Shutting down VM
03-21 21:24:26.408 8035-8035/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.johnnymolina.nextphase, PID: 8035
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.gson.JsonElement.toString()' on a null object reference
at com.johnnymolina.nextphase.activities.Imgur.onCompleted(Imgur.java:116)
at com.johnnymolina.nextphase.activities.Imgur.onCompleted(Imgur.java:98)
at com.koushikdutta.async.future.SimpleFuture.handleCallbackUnlocked(SimpleFuture.java:107)
at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:141)
at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:128)
at com.koushikdutta.ion.IonRequestBuilder.run(IonRequestBuilder.java:246)
at com.koushikdutta.async.AsyncServer$RunnableWrapper.run(AsyncServer.java:57)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-21 21:24:26.411 6534-6864/? W/ActivityManager﹕ Force finishing activity com.johnnymolina.nextphase/.activities.Imgur
03-21 21:24:26.412 6534-6864/? W/ActivityManager﹕ Force finishing activity com.johnnymolina.nextphase/.activities.MainActivity
好的,查看 logCat 似乎没有打印出整个 JSon 对象。这是 Log.v 可以打印的限制吗?还是因为网络调用不是 return 一个完全完整的对象,所以我得到一个 Null 对象引用?
从 Imgur 的 Api 中检索到的 Json 对象的示例:https://api.imgur.com/models/album
{
"data": {
"id": "lDRB2",
"title": "Imgur Office",
"description": null,
"datetime": 1357856292,
"cover": "24nLu",
"account_url": "Alan",
"account_id": 4,
"privacy": "public",
"layout": "blog",
"views": 13780,
"link": "http://alanbox.imgur.com/a/lDRB2",
"images_count": 11,
"images": [
{
"id": "24nLu",
"title": null,
"description": null,
"datetime": 1357856352,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 855658,
"views": 135772,
"bandwidth": 116174397976,
"link": "http://i.imgur.com/24nLu.jpg"
},
{
"id": "Ziz25",
"title": null,
"description": null,
"datetime": 1357856394,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 919391,
"views": 135493,
"bandwidth": 124571044763,
"link": "http://i.imgur.com/Ziz25.jpg"
},
{
"id": "9tzW6",
"title": null,
"description": null,
"datetime": 1357856385,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 655028,
"views": 135063,
"bandwidth": 88470046764,
"link": "http://i.imgur.com/9tzW6.jpg"
},
{
"id": "dFg5u",
"title": null,
"description": null,
"datetime": 1357856378,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 812738,
"views": 134704,
"bandwidth": 109479059552,
"link": "http://i.imgur.com/dFg5u.jpg"
},
{
"id": "oknLx",
"title": null,
"description": null,
"datetime": 1357856338,
"type": "image/jpeg",
"animated": false,
"width": 1749,
"height": 2332,
"size": 717324,
"views": 32938,
"bandwidth": 23627217912,
"link": "http://i.imgur.com/oknLx.jpg"
},
{
"id": "OL6tC",
"title": null,
"description": null,
"datetime": 1357856321,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 1443262,
"views": 32346,
"bandwidth": 46683752652,
"link": "http://i.imgur.com/OL6tC.jpg"
},
{
"id": "cJ9cm",
"title": null,
"description": null,
"datetime": 1357856330,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 544702,
"views": 31829,
"bandwidth": 17337319958,
"link": "http://i.imgur.com/cJ9cm.jpg"
},
{
"id": "7BtPN",
"title": null,
"description": null,
"datetime": 1357856369,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 844863,
"views": 31257,
"bandwidth": 26407882791,
"link": "http://i.imgur.com/7BtPN.jpg"
},
{
"id": "42ib8",
"title": null,
"description": null,
"datetime": 1357856424,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 905073,
"views": 30945,
"bandwidth": 28007483985,
"link": "http://i.imgur.com/42ib8.jpg"
},
{
"id": "BbwIx",
"title": null,
"description": null,
"datetime": 1357856360,
"type": "image/jpeg",
"animated": false,
"width": 1749,
"height": 2332,
"size": 662413,
"views": 30107,
"bandwidth": 19943268191,
"link": "http://i.imgur.com/BbwIx.jpg"
},
{
"id": "x7b91",
"title": null,
"description": null,
"datetime": 1357856406,
"type": "image/jpeg",
"animated": false,
"width": 1944,
"height": 2592,
"size": 618567,
"views": 29259,
"bandwidth": 18098651853,
"link": "http://i.imgur.com/x7b91.jpg"
}
]
},
"success": true,
"status": 200
}
如果您可以 post 一个 json 响应的示例,我们可以更好地帮助您。 但是据我所知,JsonElement "images_count" 不存在,因此您会得到 NullPointerException。
据我所知,如果我尝试从浏览器进行调用,我会收到此 json 响应:
{"data":{"error":"Authentication required","request":"\/3\/album\/gQxwy.json","method":"GET"},"success":false,"status":401}
您可能没有添加来自 WebService 的一些查询参数请求。
P.S.: 要显示当前 object/variable 值,您需要使用断点并且 运行 您的应用程序处于调试模式