gpt4 book ai didi

java - Appengine API 方法返回模型集合

转载 作者:行者123 更新时间:2023-11-30 11:16:12 25 4
gpt4 key购买 nike

我已经在 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);

螺柱被声明为 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 而不是一个列表?

我试图将“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 设置,请告诉我。当我使用 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 客户端中创建和使用应用引擎数据。

最佳答案

App Engine 自动生成 ObjectCollection API 方法返回的类有一个名为 getItems() 的方法
这将返回 List<yourObject> (在你的情况下 List<Student> ),
只需添加 .getItems().execute() 之后你应该得到你的结果。

关于java - Appengine API 方法返回模型集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25009293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com