本文介绍了尝试首先执行 Retrofit onResponse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为 AllStores 的活动中的列表仅包含空值.我需要这个列表,因为我想稍后填充我的 ListView.问题是因为我的回调是最后执行的.

My list in my activity called AllStores contains only null. I need this list, because I want to populate my ListView later on. The problem is caused because my callback is getting executed as last.

为了澄清,我在下面做了一个截图:

To clarify I have made a screenshot below:

链接:http://i.imgur.com/bIkWjld.png

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

The screenshot also tells what I actually want. To achieve that I had tried it with a AsyncTask. However it didn't worked out as you can see in the screenshot.

代码如下:

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

EDIT 2.0 I have changed my getSubprise() method to become synchronous and using AsyncTask

AllStores.java:

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:

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;
    }

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

However I'm now still getting the same result see the screenshot below:

链接:http://i.imgur.com/GOGFXMR.png

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

The screenshot looks the same as the screenshot from above.

推荐答案

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

To get the result from the background thread to the main thread I had to use AsyncTask.get(). By using that I could see a value in my stores variable instead of having a null value.

下面你可以看到我的代码给想看的人:

Below you can see my code for those who want to see it:

@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
    }
}

这篇关于尝试首先执行 Retrofit onResponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 05:27