karweny
2021-03-31 01:25
采纳率: 100%
浏览 271

异步请求回来的数据在onResponse中,无法给外部获取

OkHttpClient client = new OkHttpClient.Builder()
                               .connectTimeout(5000, TimeUnit.MILLISECONDS)
                               .build();
        //创建请求内容
        Request request = new Request.Builder()
                .url(RequestUrl.BasePath + RequestUrl.application_getVideoList)
                .get()
                .build();
        Call task = client.newCall(request);
        //异步请求
        task.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Log.d(TAG, "onFailure--->" + e.toString());
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
 //               int code = response.code();
//                List<ResourceBean> dataList = new ArrayList<>();
                String responseData = response.body().string();
//                Log.d(TAG, "onResponse--->" + code);
//                Log.d(TAG, "onResponse--->" + responseData);
                Gson gson = new Gson();
                picVideoBean = gson.fromJson(responseData, PicVideoBean.class);
                if (picVideoBean != null) {
                    for (int i=0 ; i < picVideoBean.getData().getVideoList().size(); i++ ){
                        url = picVideoBean.getData().getVideoList().get(i).getFileUrl();
 //                      dataList.add(new ResourceBean(url,2));
                        Log.d(TAG, "url--->--->" + url);
                    }

url在外面获取不了!如何把onResponse下面获取的数值传递到外部?

5条回答 默认 最新

相关推荐 更多相似问题