/***
* 请求网络数据
*/
private void httpDatePost(){
progressDialog.setMessage("刷新数据...");
Map <String,String> params = new HashMap<String,String>();
// params.put("userPhone", "110");
// OkHttpUtils.post()
// .url("")
// .params(params)
// .build().execute(new StringCallback() {
//
// @Override
// public void onResponse(String arg0, int arg1) {
// Toast.makeText(context, "UM", Toast.LENGTH_SHORT).show();
// Gson gson = new Gson();
// school = gson.fromJson(arg0, School.class);
// System.out.println(school.getPassword()+"lll");
// Log.e("UUU",arg0);
// initView();
// }
//
// @Override
// public void onError(Call arg0, Exception arg1, int arg2) {
// System.out.println(arg1.toString()+"xxx");
// Toast.makeText(context, "错了", Toast.LENGTH_SHORT).show();
//
// }
// });
OkHttpUtils.post()
.url(“”)
.params(params)
.build().execute(new StringCallback() {
@Override
public void onResponse(String arg0, int arg1) {
Toast.makeText(context, "UM", Toast.LENGTH_SHORT).show();
Gson gson = new Gson();
// school = gson.fromJson(arg0, List<School> school);
List<School> ss = gson.fromJson(arg0, new TypeToken<List<School>>(){}.getType());
lists = new ArrayList<School>() ;
for (School school : ss) {
System.out.println(school.getPassword()+"22");
lists.add(school);
}
System.out.println(lists.get(1).getPassword()+"lll");
Log.e("UUU",arg0);
initView();
}
@Override
public void onError(Call arg0, Exception arg1, int arg2) {
System.out.println(arg1.toString()+"xxx");
Toast.makeText(context, "错了", Toast.LENGTH_SHORT).show();
}
});
* 请求网络数据
*/
private void httpDatePost(){
progressDialog.setMessage("刷新数据...");
Map <String,String> params = new HashMap<String,String>();
// params.put("userPhone", "110");
// OkHttpUtils.post()
// .url("")
// .params(params)
// .build().execute(new StringCallback() {
//
// @Override
// public void onResponse(String arg0, int arg1) {
// Toast.makeText(context, "UM", Toast.LENGTH_SHORT).show();
// Gson gson = new Gson();
// school = gson.fromJson(arg0, School.class);
// System.out.println(school.getPassword()+"lll");
// Log.e("UUU",arg0);
// initView();
// }
//
// @Override
// public void onError(Call arg0, Exception arg1, int arg2) {
// System.out.println(arg1.toString()+"xxx");
// Toast.makeText(context, "错了", Toast.LENGTH_SHORT).show();
//
// }
// });
OkHttpUtils.post()
.url(“”)
.params(params)
.build().execute(new StringCallback() {
@Override
public void onResponse(String arg0, int arg1) {
Toast.makeText(context, "UM", Toast.LENGTH_SHORT).show();
Gson gson = new Gson();
// school = gson.fromJson(arg0, List<School> school);
List<School> ss = gson.fromJson(arg0, new TypeToken<List<School>>(){}.getType());
lists = new ArrayList<School>() ;
for (School school : ss) {
System.out.println(school.getPassword()+"22");
lists.add(school);
}
System.out.println(lists.get(1).getPassword()+"lll");
Log.e("UUU",arg0);
initView();
}
@Override
public void onError(Call arg0, Exception arg1, int arg2) {
System.out.println(arg1.toString()+"xxx");
Toast.makeText(context, "错了", Toast.LENGTH_SHORT).show();
}
});
}
对象和字符串,json的key必须和对象所定义的字段一致;()
:如果JSON最外层数据是单个Bean,直接用class,如果最外层是集合Bean,就要用TypeToken。