赞
踩
new Thread(new Runnable() {
@Override
public void run() {
getServiceList();
}
}).start();
private void getServiceList(){ Message msg = new Message(); try{ OkHttpClient okHttpClient = new OkHttpClient(); FormBody.Builder formBodyBuilder = new FormBody.Builder(); formBodyBuilder.add("token", "xxx") .add("id", "idxxx"); if (constellationArr != null) { for (String constellation : constellationArr) { formBodyBuilder.add("constellationArr", constellation); } } FormBody formBody = formBodyBuilder.build(); /// Request request = new Request.Builder() .url("调接口url") .post(formBody) .build(); // 创建 Call 对象并执行请求 Call call = okHttpClient.newCall(request); Response response = call.execute(); // 处理响应 if (response.isSuccessful()) { // 响应成功 String responseBody = response.body().string(); // 在这里处理响应体 JSONObject jsonObject = new JSONObject(responseBody); boolean success = jsonObject.getBoolean("success"); String message = jsonObject.getString("msg"); String obj = jsonObject.getString("obj"); Log.e(TAG, obj); if (success) { // 使用 TypeToken 获取泛型类型 TypeToken<ArrayList<ServicePersonShortBean>> typeToken = new TypeToken<ArrayList<ServicePersonShortBean>>() {}; ArrayList<ServicePersonShortBean> servicePersonShortBeanList = gson.fromJson(obj, typeToken.getType()); Bundle bundle = new Bundle(); bundle.putSerializable("servicePersonShortBeanList", servicePersonShortBeanList); msg.setData(bundle); msg.what = 1; handler.sendMessage(msg); } else { Bundle bundle = new Bundle(); if(message.equals("token找不到")){ bundle.putString("message", "请您登录后查询"); }else{ bundle.putString("message", message); } msg.setData(bundle); msg.what = 2; handler.sendMessage(msg); } } else { // 响应失败 // 在这里处理失败情况 msg.what = 3; handler.sendMessage(msg); } // 记得关闭响应体 response.close(); } catch (IOException e) { if (e.toString().contains("request failed")) { msg.what = 4; handler.sendMessage(msg); } else { msg.what = 5; handler.sendMessage(msg); } e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); msg.what = 3; handler.sendMessage(msg); } }
// 在主线程中创建 Handler Handler handler = new Handler(Looper.getMainLooper()) { @Override public void handleMessage(Message msg) { super.handleMessage(msg); //根据信息编码及数据做出相对应的处理 switch (msg.what) { case 1: { Bundle bundle = msg.getData(); servicePersonShortBeanList = (ArrayList<ServicePersonShortBean>) bundle.getSerializable("servicePersonShortBeanList"); showServicerList(); new Thread(new Runnable() { @Override public void run() { loadMoreData(); } }).start(); break; } case 2: { Bundle bundle = msg.getData(); String message = bundle.getString("message"); Toast.makeText(ServicePersonListActivity.this, message,Toast.LENGTH_SHORT).show(); break; } case 3: Toast.makeText(ServicePersonListActivity.this, "获取xxx信息失败,请稍后再试!", Toast.LENGTH_SHORT).show(); break; case 4: Toast.makeText(ServicePersonListActivity.this, "网络请求失败,请稍后重试", Toast.LENGTH_SHORT).show(); break; case 5: Toast.makeText(ServicePersonListActivity.this, "网络异常,请检查您的网络\"", Toast.LENGTH_SHORT).show(); break; case 6: { //加载更多 Bundle bundle = msg.getData(); ArrayList<ServicePersonShortBean> moreServicePersonShortBeanList = (ArrayList<ServicePersonShortBean>) bundle.getSerializable("moreServicePersonShortBeanList"); servicePersonShortBeanList.addAll(moreServicePersonShortBeanList); servicePersonAdapter.notifyDataSetChanged(); break; } default: break; } } };
注:链式调用写法举例
FormBody formBody = formBody = new FormBody.Builder()
.add("token", token)
.add("id", id)
.build();
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。