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下面获取的数值传递到外部?
异步操作 将结果返回给其他地方 有不少方案,比如 使用Handler ,广播 。
我建议使用Lisenter ,在启动下载模块的时候将lisenter传入,通过回调通知其他模块更新结果
可以发送handler出去,也可以写个接口方法回调使用
创建一个全局变量接收试试
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
全局有问题。