用到的包:
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
主要代码:
try { //创建OkHttpClient对象 OkHttpClient client = new OkHttpClient(); //创建Request 对象 Request request = new Request.Builder() .url("HttpUr了")//请求接口。如果需要传参拼接到接口后面。 .build(); Response response = null; //得到Response 对象 response = client.newCall(request).execute(); if (response.isSuccessful()) { String result = response.body().string(); if(!"".equals(result)){ Map<String, Object> map = (Map<String, Object>)JSON.parse(result); Map<String, Object> dataMap = (Map<String, Object>)map.get("data"); String id = StringUtils.nvlString(dataMap.get("ID")); resultMap.put("statusCode",1000); resultMap.put("id",id); } }else{ resultMap.put("statusCode",2001); resultMap.put("message","XXXXX"); } return CommonResult.buildSuccess(version, resultMap); } catch (Exception e) { e.printStackTrace(); return CommonResult.error(Status.ERROR_5001); }