q9722088 2018-03-28 08:31 采纳率: 54.5%
浏览 785
已采纳

Android如何读取两次网络数据然后一起传出来

public void postEqueue(int id){
    OkHttpClient client = new OkHttpClient();
    RequestBody requestBody = new FormBody.Builder()
            .add("busStation", id + "")
            .build();
    Request request = new Request.Builder()
            .url("http://10.0.2.2:8080/DaSaiFuWuQi/Gongjiaochaxun")
            .post(requestBody)
            .build();
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            JSONArray array = null;
            ArrayList<ChildInfo> temp = new ArrayList<>();
            try {
                array = new JSONArray(response.body().string());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < array.length(); i++) {
                int this_long = 0;
                try {
                    JSONObject jsonObject = array.getJSONObject(i);
                    this_long = jsonObject.getInt("Distance");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                ChildInfo info = new ChildInfo();
                info.setBusImg(images[i]);
                info.setBusName(names[i]);
                info.setBusLong(this_long);
                info.setBusTime(this_long / 20);
                temp.add(info);
            }
            Collections.sort(temp, new SortByDistance());
            Message msg = new Message();
            msg.obj = temp.subList(0,2);
            handler1.sendMessage(msg);
        }
    });
}
Handler handler1 = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        childList.get(0) = (ArrayList<>)msg.obj
    }

};



    需要查两次一次传0一次传1,在外面需要得到两次的集合再拼到一个集合里(ps:List<List<GhildInfo>>)

    顺便问下大佬们 题目要求是三秒跟新一次数据,我这么获取数据之后再用handler.postDelayed()会导致刷新时间不准确吗
  • 写回答

3条回答 默认 最新

  • 哈士奇会咬人 2018-03-28 09:36
    关注

    在写一个网络请求的方法,获取参数1的方法,得到参数0的值后再去调用另外一个请求1的数据,然后再把数据拼接起来,

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64