import java.util.List;
import java.util.Map;
public class Bean {
public String error_code;
public String reason;
public Result result;
public class Result {
public List<Map<Integer, String> >data;
}
}
package com.example.json_;
public class ListView_Bean {
private int num;
private String values;
public ListView_Bean(int num, String values) {
super();
this.num = num;
this.values = values;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getValues() {
return values;
}
public void setValues(String values) {
this.values = values;
}
public ListView_Bean() {
super();
}
}
package com.example.json_;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private Context context;
private List<ListView_Bean> list;
public MyAdapter(Context context, List<ListView_Bean> list) {
super();
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(context, R.layout.item, null);
TextView textView_num = (TextView) view.findViewById(R.id.num);
TextView textView_values = (TextView) view.findViewById(R.id.values);
textView_num.setText(list.get(position).getNum() + " :");
textView_values.setText(list.get(position).getValues());
return view;
}
}
package com.example.json_;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
public class MainActivity extends Activity {
String url = "http://japi.juhe.cn/funny/type.from?key=%209f69cc0f8cc26e449e273d24c90ea410";
private ArrayList<ListView_Bean> arrayList;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
arrayList = new ArrayList<ListView_Bean>();
HttpUtils httpUtils = new HttpUtils();
httpUtils.send(HttpMethod.POST, url, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
Gson gson = new Gson();
Bean fromJson = gson.fromJson(arg0.result, Bean.class);
List<Map<Integer, String>> data = fromJson.result.data;
for (int i = 0; i < data.size(); i++) {
Iterator<Integer> iterator = data.get(i).keySet()
.iterator();
while (iterator.hasNext()) {
Integer next = iterator.next();
String string = data.get(i).get(next);
arrayList.add(new ListView_Bean(next, string));
}
}
listView.setAdapter(new MyAdapter(MainActivity.this, arrayList));
}
});
}
}