【发布时间】:2014-07-29 06:21:35
【问题描述】:
我已在 android studio 中成功创建了一个 appengine 项目,并尝试在 Android 客户端中使用它。当我从后端调用List<Student> api 方法说我应该返回一个模型集合时,我遇到了错误。这是我的代码,用于显示我正在谈论的示例:
StudentEndpoint service = AppConstants.getApiHandle();
stud = service.getStudents().execute(); //this is the error
list = new ArrayList<Map<String, String>>();
_list = stud.getItems();
for (Student student : _list) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("email", student.getEmail());
hm.put("school", student.getSchool());
list.add(hm);
}
String from[] = {"email", "school"};
int to[] = {R.id.email, R.id.school};
adapter = new SimpleAdapter(this, list, R.layout.listlay, from, to);
lv.setAdapter(adapter);
stud 被声明为List<Student>
这里是getAllStudents()的api方法:
@ApiMethod(name="getStudents")
public List<Student> getStudents() {
return ofy().load().type(Student.class).list();
}
错误是说:required java.util.List found appbackend_path....StudentCollection
appbackend_path 只是指我的后端 api 的包名称。所以我的问题真的是为什么这会返回一个 StudentCollection 而不是一个 List?
我尝试将“stud”变量设置为 StudentCollection,它消除了编译错误并将应用程序构建到我的手机上,但是当我尝试填充列表视图时,我得到了这个异常:
07-28 21:21:43.566 31789-31789/com.nicky.communitybeta W/System.err﹕ android.os.NetworkOnMainThreadException
07-28 21:21:43.566 31789-31789/com.nicky.communitybeta W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
07-28 21:21:43.566 31789-31789/com.nicky.communitybeta W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:215)
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:29)
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:245)
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:189)
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:369)
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:25)
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:93)
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:965)
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at com.nicky.communitybeta.MyActivity.onCreate(MyActivity.java:50) //same line as error i am referring to above
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5720)
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
07-28 21:21:43.567 31789-31789/com.nicky.communitybeta W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2317)
07-28 21:21:43.568 31789-31789/com.nicky.communitybeta W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:143)
07-28 21:21:43.568 31789-31789/com.nicky.communitybeta W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
07-28 21:21:43.568 31789-31789/com.nicky.communitybeta W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
07-28 21:21:43.568 31789-31789/com.nicky.communitybeta W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
07-28 21:21:43.568 31789-31789/com.nicky.communitybeta W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5070)
07-28 21:21:43.568 31789-31789/com.nicky.communitybeta W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
07-28 21:21:43.568 31789-31789/com.nicky.communitybeta W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
07-28 21:21:43.568 31789-31789/com.nicky.communitybeta W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
07-28 21:21:43.568 31789-31789/com.nicky.communitybeta W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
非常感谢任何帮助,如果您需要查看更多文件或 gradle 设置,请告诉我。当我使用 localhost/_ah/api/explorer 构建时,api 工作,我可以添加学生列表等,一切都按预期显示。
编辑: 经过更多的阅读和测试不同的事情后,我得到了它的工作,并弄清楚了为什么我会出错,如果其他人有同样的错误,这是我的修复:
我不应该在我的后端使用 List<Student> 作为 api 方法,而是像我在这里一样使用 CollectionResponse:
@ApiMethod(name = "getAllStudents")
public CollectionResponse<Student> getAllStudents() {
List<Student> stud = ofy().load().type(Student.class).list();
return CollectionResponse.<Student>builder().setItems(stud).build();
}
然后在客户端代码中我收到第一个错误,因为我需要将调用设置为 service.getAllStudents().execute() 作为 CollectionResponseStudent,如下所示:
StudentEndpoint service = AppConstants.getApiHandle();
CollectionResponseStudent stud = service.getAllStudents().execute();
在弄清楚这一点后,我需要将它全部包装在一个 AsyncTask 中。如上所示,主线程上的网络流量过多导致了该异常。它现在正在一个 android 客户端中创建和使用 appengine 数据。
【问题讨论】:
-
List
不起作用其实是一个bug。它应该自动包装成一个类似于 CollectionResponse 的模式。如果您不介意提交错误,那就太好了:code.google.com/p/googleappengine/issues/entry 请务必添加一个 Component-Endpoints 标记。
标签: java android google-app-engine google-cloud-endpoints android-gradle-plugin