第三方包
okhttp
gson
数据模型,command+n自动生成getter和setter、toString方法。
public class EnterpriseResultModel implements Serializable {
ResultModel result;//result一定要和返回的参数名一样
List<EnterpriseModel> companies;
}
public class ResultModel implements Serializable {
String message;
String reserve;
String reserve1;
Integer state;
}
public class EnterpriseModel implements Serializable {
String logo;
String companyName;
String address;
}
解析转换
public class JsonUtil {
static Gson mGson;
public static <T> T paraseJson(String json, Class<T> tClass) {
if (mGson == null) {
mGson = new Gson();
}
if (TextUtils.isEmpty(json)) {
return null;
}
return mGson.fromJson(json, tClass);
}
}
网络请求
public void getEnterprises() {
OkHttpClient client = new OkHttpClient();
RequestBody paras = new FormBody.Builder()
.add("MemberType", "0")
.add("Longitude", "112.982")
.add("Latitude", "28.2107")
.add("PayFlag", "-1")
.add("PageSize", "300")
.add("PageIndex", "0")
.build();
Request request = new Request.Builder()
.url("http://www.chinaszma.org:9001/API/Home/GetNearbyCompanies")
.post(paras)
.build();
// Response response = client.newCall(request).execute();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(okhttp3.Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(okhttp3.Call call, Response response) throws IOException {
if (!response.isSuccessful()) throw new IOException("error" + response);
if (!response.isSuccessful()) {
Log.i("a", "shibai");
}
// Log.i("llq", response.body().string());
String date = response.body().string();
EnterpriseResultModel enterpriseResultModel = JsonUtil.paraseJson(date, EnterpriseResultModel.class);
Log.i("林林七", enterpriseResultModel.getResult().getMessage());
}
});
}