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()会导致刷新时间不准确吗
在写一个网络请求的方法,获取参数1的方法,得到参数0的值后再去调用另外一个请求1的数据,然后再把数据拼接起来,
后台把2次的查询结果拼接,一起传过来
把两次查询结果拼接起来最麻烦,还要转格式,数据合到一起。你直接去找后台让他来把数据整合到一起,整合成你想要的格式。后台操作比前台简单,除非你们后台很烂。