我正尝试在Azure移动服务上调用自定义API。我已经从我的应用程序成功完成了这个工作,所以我知道我已经正确设置了它。但是,在这种情况下,我在移动服务SDK中收到了空指针异常,所以我无法准确追踪它发生的原因。 (使用适用于Android的移动服务1.1.5)Android Azure invokeApi获取空指针异常
来自Azure的日志表明服务器脚本正在成功运行。下面是脚本代码多数民众赞成在返回数据的片段:
} else {
console.log("no match");
response.send(204, results); //No Match
}
下面是我的Android应用程序的invokeApi代码:
mClient.invokeApi("data/check","GET",params,new ApiJsonOperationCallback() {
@Override
public void onCompleted(JsonElement jsonObject, Exception exception, ServiceFilterResponse response) {
Logg.d("onCompleted called successfully");
if (exception != null) {
if (response.getStatus().getStatusCode() == RESULT_OK) { //Item Already Exists
Logg.d("Match! Item Already Exists.");
} else if (response.getStatus().getStatusCode() == 206) { //Chain Match or partial chain match
Logg.d("Chain Match or partial Chain Match");
} else if (response.getStatus().getStatusCode() == 204) { //No match, request ok
Logg.d("No match found");
} else {
Logg.e("Azure exception: " + exception.getCause().getMessage());
result = RESULT_CANCELED;
}
}
}
});
而这里的错误堆栈:
12-27 08:39:21.497 13234-13234/com.farmsoft.lunchguru.app.debug W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x417b3da0)
12-27 08:39:21.537 13234-13234/com.farmsoft.lunchguru.app.debug E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.farmsoft.lunchguru.app.debug, PID: 13234
java.lang.NullPointerException
at java.io.StringReader.<init>(StringReader.java:47)
at com.google.gson.JsonParser.parse(JsonParser.java:45)
at com.microsoft.windowsazure.mobileservices.MobileServiceClient$5.onResponse(MobileServiceClient.java:708)
at com.microsoft.windowsazure.mobileservices.MobileServiceClient$6.onPostExecute(MobileServiceClient.java:825)
at com.microsoft.windowsazure.mobileservices.MobileServiceClient$6.onPostExecute(MobileServiceClient.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5487)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
看来,错误正在发生之前 onCompleted被调用,因为Log调用未反映在logcat输出中。我完全不知道为什么会出现这种情况。
基于堆栈跟踪,它看起来像是Android SDK中的一个错误(处理204个响应 - 没有响应内容)。我猜想要解决这个问题,你可以将状态码从204改为200. – carlosfigueira 2014-12-28 05:44:45
嗯 - 切换到200让它运行没有错误,尽管我希望使用状态码来向应用程序传达结果类型被发现(或不)。谢谢!你想补充说,作为答案?是否有地方向Azure报告错误?顺便说一句,我已经在Azure上取得了成功,这是因为你的博客,所以我非常感谢你! – Scott 2014-12-28 15:17:01
我发现Azure在使用非标准状态码(如821)时不会返回错误,尽管响应内容不会通过jsonObject参数传递。我可以通过response.getContent()手动访问它,这可能是一种可行的解决方法。 – Scott 2014-12-28 16:19:53