【发布时间】:2023-04-01 10:15:01
【问题描述】:
我正在开发一个使用谷歌端点作为后端的安卓应用。出于某种原因,其中一个端点方法只会在从 android 客户端调用时导致 java.lang.NullPointerException,但在从 api explorer 调用时不会。
这里是端点方法:
@ApiMethod(name = "getFriends", path = "friends", httpMethod = ApiMethod.HttpMethod.GET)
public List<Profile> getFriends(@Named("userId") String userId) {
Profile profile = ofy().load().key(Key.create(Profile.class, userId)).now();
List<String> friendIds = profile.getFriendIds(); // NullPointerException on this line
List<Profile> friends = new ArrayList<>();
if (friendIds != null) {
for (String friendId : friendIds) {
Profile friend = ofy().load().key(Key.create(Profile.class, friendId)).now();
friends.add(friend);
}
}
return friends;
}
这是配置文件实体的 getFriendIds() 方法:
public List<String> getFriendIds() {
return friendIds != null ? ImmutableList.copyOf(friendIds) : null;
}
在调试模式和 API 资源管理器中,一切正常。在这个例子中,我创建了 2 个用户,一个是 id = 4567,另一个是 id = 9876。以下是我运行获取好友的方法时发生的情况:
它按预期工作,并且在被 api explorer 调用时不会导致错误。
然后我在用户没有朋友的时候试了一下:
它按预期工作,并且在被 api explorer 调用时不会导致错误。
在我的 android 客户端中,我调用了这个确切的方法,但该方法总是导致 java.lang.NullPointerException。
我从这样的异步任务中调用它:
return mApi.getFriends(mUserId).execute().getItems();
这是客户端的 stacktrace:
07-12 14:09:57.934 9777-9830/com.hb.birthpay W/System.err: com.google.api.client.googleapis.json.GoogleJsonResponseException: 503 Service Unavailable
07-12 14:09:57.934 9777-9830/com.hb.birthpay W/System.err: {
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: "code": 503,
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: "errors": [
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: {
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: "domain": "global",
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: "message": "java.lang.NullPointerException",
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: "reason": "backendError"
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: }
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: ],
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: "message": "java.lang.NullPointerException"
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: }
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056)
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
07-12 14:09:57.936 9777-9830/com.hb.birthpay W/System.err: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
07-12 14:09:57.936 9777-9830/com.hb.birthpay W/System.err: at com.hb.birthpay.utils.EndpointTask$GetFriends$override.doInBackground(EndpointTask.java:101)
07-12 14:09:57.936 9777-9830/com.hb.birthpay W/System.err: at com.hb.birthpay.utils.EndpointTask$GetFriends$override.access$dispatch(EndpointTask.java)
07-12 14:09:57.936 9777-9830/com.hb.birthpay W/System.err: at com.hb.birthpay.utils.EndpointTask$GetFriends.doInBackground(EndpointTask.java:0)
07-12 14:09:57.936 9777-9830/com.hb.birthpay W/System.err: at com.hb.birthpay.utils.EndpointTask$GetFriends.doInBackground(EndpointTask.java:68)
这是后端的 stacktrace:
java.lang.NullPointerException
at com.hb.backend.spi.BirthpayEndpoint.getFriends(BirthpayEndpoint.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
为什么会发生这种情况,我该如何解决?
【问题讨论】:
标签: java android google-app-engine null google-cloud-endpoints