我们的应用程序基于Google App Engine。
出于一些奇怪的原因,在Android上调用极少数方法并不适用于Web和iOS。这让我们相信后端没有问题(部署,版本等......)。
通过api explorer调用相同的方法按预期工作。我们也没有在StackDriver中看到任何日志。
所有这些方法都是返回用户拥有的角色。我们不怀疑参数(例如排序,注释等),因为此方法不需要任何参数。
我们是最新的SDK并使用Android Studio 3.2.2。
Android模拟器跟踪:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at com.app.android.LoginActivity$userRoleAsyncTask.doInBackground(LoginActivity.java:413)
at com.app.android.LoginActivity$userRoleAsyncTask.doInBackground(LoginActivity.java:385)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Android代码:
@Override
protected UserRole doInBackground(String... params) {
UserRole response = null;
try {
UserEndpoint.Builder builder = new
UserEndpoint.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), null);
builder.setHttpRequestInitializer(new HttpRequestInitializer(){
@Override
public void initialize(HttpRequest request) throws IOException {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("JSESSIONID", prefs.getCookie());
request.setHeaders(httpHeaders);
}
});
UserEndpoint service = builder.build();
response = service.getRoles().execute();
LogUtil.debug("getRole Response: " + response.toString());
} catch (Exception e) {
Log.d("Could not get UserRole", e.getMessage(), e);
}
return response;
}
}
App Engine代码:
@ApiMethod(name = "getRoles", path = "getRoles", httpMethod = HttpMethod.GET)
public UserRole getRoles(HttpServletRequest request) throws DatabaseException, IOException {
//retrieve session and do not create if it does not exist
HttpSession session = request.getSession(false);
UserRole userRole = null;
if (session != null){
//retrieve user from session
User user = (User) session.getAttribute("user");
userRole = new UserRole(true, user.getIsActive(), user.getHasChild());
}
return userRole;
}
App Engine资源管理器
有什么想法吗?谢谢!
答案 0 :(得分:0)
将root url设置为此方法可解决此问题。
builder.setHttpRequestInitializer(new HttpRequestInitializer(){
@Override
public void initialize(HttpRequest request) throws IOException {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("JSESSIONID", prefs.getCookie());
request.setHeaders(httpHeaders);
}
});
builder.setRootUrl("https://android-app-backend.appspot.com/_ah/api/");
UserEndpoint service = builder.build();
response = service.getRoles().execute();
其中android-app-backend对应于您自己的项目ID。 https://cloud.google.com/endpoints/docs/frameworks/legacy/v1/java/helloendpoints-android-studio