请求网络数据示例。。。。。
String path = "http://api.m.panda.tv/ajax_get_all_subcate?__version=1.0.1.1300&__plat=android";
private List<MyData> list;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
getData(path);
}
private void getData(String url) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(new Callback() {
// 成功
@Override
public void onResponse(Response arg0) throws IOException {
String json = arg0.body().string();
Gson gson = new Gson();
Type type = new TypeToken<Games>() {
}.getType();
Games games = gson.fromJson(json, type);
list = games.getData();
System.out.println(list.toString()+"======");
lv.setAdapter(new MyAdapter(MainActivity.this, list));
}
// 失败
@Override
public void onFailure(Request arg0, IOException arg1) {
}
});