gpt4 book ai didi

java - 尝试首先执行 Retrofit onResponse

转载 作者:行者123 更新时间:2023-12-01 10:43:04 27 4
gpt4 key购买 nike

我的 Activity 中名为 AllStores 的列表仅包含 null。我需要这个列表,因为我想稍后填充我的 ListView。导致问题的原因是我的回调最后执行。

为了澄清,我制作了下面的屏幕截图:

链接:/image/CFDHN.png

截图也说明了我真正想要的。为了实现这一点,我尝试使用 AsyncTask。然而,正如您在屏幕截图中看到的那样,它并没有成功。

这是代码:

<小时/>

编辑 2.0 我已将我的 getSubprise() 方法更改为同步并使用 AsyncTask

AllStores.java:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Response<List<Store>> subprises = new StoreService().getSubprises();
System.out.println("AllStores (Activity) - subprise " + subprises);
}

StoreService.java:

       public Response<List<Store>> getSubprises() {
new LongOperation().execute("");
System.out.println("getStores (StoreService.java) value "+ getStores());
return getStores();
}

private class LongOperation extends AsyncTask<String, Void, Response<List<Store>>> {

@Override
protected Response<List<Store>> doInBackground(String... params) {
System.out.println("doInBackground executed second");
try {
Call<List<Store>> call = subpriseAPI.listStores();
stores=call.execute();
Iterator it=stores.body().iterator();
// while(it.hasNext())
// System.out.println(((Store)it.next()).getSTREET());
} catch (IOException e) {
e.printStackTrace();
}
return stores;
}

@Override
protected void onPreExecute() {
//Can't put the call here because this is the main thread
System.out.println("onPreExecute first");
}

@Override
protected void onPostExecute(Response<List<Store>> result) {
//Can't put the call here because this is the main thread
setStores(stores);
}
}

public Response<List<Store>> getStores() {
return stores;
}

public void setStores(Response<List<Store>> stores) {
this.stores = stores;
}

但是我现在仍然得到相同的结果,请参见下面的屏幕截图:

链接:/image/LI52q.png

屏幕截图看起来与上面的屏幕截图相同。

最佳答案

为了将结果从后台线程获取到主线程,我必须使用 AsyncTask.get() 。通过使用它,我可以在我的 stores 变量中看到一个值,而不是一个空值。

下面你可以看到我的代码,供那些想查看的人使用:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public Response<List<Store>> getSubprises() throws IOException, ExecutionException, InterruptedException {
LongOperation longOperation = new LongOperation();
longOperation.execute("");
stores = longOperation.get();
return stores;
}

private class LongOperation extends AsyncTask<String, Void, Response<List<Store>>> {
@Override
protected Response<List<Store>> doInBackground(String... params) {
//System.out.println("doInBackground executed second");
try {
Call<List<Store>> call = subpriseAPI.listStores();
stores=call.execute();
} catch (IOException e) {
e.printStackTrace();
}
return stores;
}

@Override
protected void onPreExecute() {
//Can't put the call here because this is the main thread
//System.out.println("onPreExecute first");
}

@Override
protected void onPostExecute(Response<List<Store>> result) {
//Can't put the call here because this is the main thread
}
}

关于java - 尝试首先执行 Retrofit onResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34346224/

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