问题
I'm trying to call a custom API on Azure Mobile Services. I've done this successfully from my app so I know I've set it up correctly. However in this case I'm getting a Null Pointer Exception from within the mobile services SDK so I'm having trouble tracing exactly why it's occurring. (using mobile services 1.1.5 for Android)
The logs from Azure indicate that the server script is running successfully. Here's the snippet of script code that's returning data:
} else {
console.log("no match");
response.send(204, results); //No Match
}
Here's the invokeApi code in my Android app:
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;
}
}
}
});
And here's the error stack:
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)
It seems that the error is occurring before onCompleted is called because the Log call is not reflected in the logcat output. I am completely at a loss to why this is the case.
回答1:
Based on the stack trace it looks like a bug in the Android SDK (dealing with 204 responses - which do not have a response content). I'd guess that to workaround this issue you could change the status code from 204 to 200.
You can file a bug at https://github.com/Azure/azure-mobile-services/issues for this issue. It seems, per your comments, that the response is only being parsed correctly for 200 responses, while there are other responses which could have more data. Notice that using 204 means that there will be no response body (by definition - see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.4), but you should be able to access the response with other successful status codes.
回答2:
Having the same issue now with my GET request. Doing a method breakpoint on OnSuccess() which is always null.
The REST tester works and receives valid json back. Using express 4 and tried multiple methods of return, including changing response code to 200.
来源:https://stackoverflow.com/questions/27669302/android-azure-invokeapi-getting-a-null-pointer-exception