我已经在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();
}
错误说:找到了必需的java.util.List appbackend_path .... StudentCollection
appbackend_path只是指我的后端api的包名。所以我的问题是为什么这会返回一个StudentCollection而不是List?
我试图让'stud'变量成为StudentCollection并且它摆脱了编译错误并将应用程序构建到我的手机上但是当我尝试填充listview时我得到了这个例外:
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设置,请告诉我们。 api适用于我使用localhost / _ah / api / explorer构建,我可以添加学生列表等等,一切都按预期显示。
编辑: 经过更多阅读和测试不同的东西,我已经让它工作,并弄清楚为什么我得到错误,如果其他人有相同的错误,这是我的修复:
我不应该在我的后端使用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数据。
答案 0 :(得分:1)
API方法返回的 App Engine 自动生成的ObjectCollection
类有一个名为getItems()
的方法
这将返回List<yourObject>
(在您的情况下为List<Student>
),
只需在.getItems()
之后添加.execute()
即可获得结果。