String key = "99e383f9a3e57517fb763732ce221be0";
// String key = "d1633613bc9232311b2c663e721f8a66";
String path = "http://japi.juhe.cn/comic/category?";
//图书分类
String book = "http://japi.juhe.cn/comic/book?";
List<String> result =new ArrayList<String>();
List<ChildBean> childBeans =new ArrayList<ChildBean>();
Gson gson = new Gson();
Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
//what=88时,是一级列表的数据
case 88:
String json = (String) msg.obj;
Bean bean = gson.fromJson(json, Bean.class);
result = bean.result;
//得到二级列表数据
getChildData();
break;
//what=99时,是二级列表的数据
case 99:
String childJson = (String) msg.obj;
System.out.println(childJson);
ChildBean childBean = gson.fromJson(childJson, ChildBean.class);
childBeans.add(childBean);
break;
}
};
};
static final String TAG = "MainActivity";
private ExpandableListView elv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
elv = (ExpandableListView) findViewById(R.id.elv);
HttpUtils httpUtils = new HttpUtils();
httpUtils.send(HttpMethod.GET, path + "key=" + key,
new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
String result = arg0.result;
Log.i(TAG, result);
// 如果包含"Success",代表json请求成功
if (result.contains("Success")) {
Message msg = Message.obtain();
msg.obj = result;
msg.what=88;
handler.sendMessage(msg);
} else {
errorData(result);
}
}
});
}
/**
* 得到二级列表数据
*/
protected void getChildData() {
HttpUtils httpUtils =new HttpUtils();
for(int i=0;i<result.size();i++){
httpUtils.send(HttpMethod.GET, book+"key="+key+"&type="+result.get(i), new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
String book = arg0.result;
Message msg=Message.obtain();
msg.what=99;
msg.obj=book;
handler.sendMessage(msg);
}
});
}
elv.setAdapter(new MyAdapter(MainActivity.this,result,childBeans));
}
/**
* 取出json错误信息
*
* @param result
*/
private void errorData(String result) {
// 如果是错误的json,取出错误的原因
try {
JSONObject jsonObject = new JSONObject(result);
String optString = jsonObject.optString("reason");
Toast.makeText(MainActivity.this, optString, 1).show();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return;
}
}
=======================================================================================
//适配器
public class MyAdapter extends BaseExpandableListAdapter {
Context context;
List<String> result;
List<ChildBean> childBeans;
/**
* @param mainActivity
* @param result
* @param childBeans
*/
public MyAdapter(Context context, List<String> result,
List<ChildBean> childBeans) {
this.context=context;
this.result=result;
this.childBeans=childBeans;
}
@Override
public int getGroupCount() {
return result.size();
}
@Override
public int getChildrenCount(int groupPosition) {
int size = childBeans.get(groupPosition).result.bookList.size();
return size;
}
/*
* (non-Javadoc)
*
* @see android.widget.ExpandableListAdapter#getGroup(int)
*/
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return null;
}
/*
* (non-Javadoc)
*
* @see android.widget.ExpandableListAdapter#getChild(int, int)
*/
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
/*
* (non-Javadoc)
*
* @see android.widget.ExpandableListAdapter#getGroupId(int)
*/
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
/*
* (non-Javadoc)
*
* @see android.widget.ExpandableListAdapter#getChildId(int, int)
*/
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}
/*
* (non-Javadoc)
*
* @see android.widget.ExpandableListAdapter#hasStableIds()
*/
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
/*
* (non-Javadoc)
*
* @see android.widget.ExpandableListAdapter#getGroupView(int, boolean,
* android.view.View, android.view.ViewGroup)
*/
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View inflate = View.inflate(context, android.R.layout.simple_expandable_list_item_2, null);
TextView text1 = (TextView) inflate.findViewById(android.R.id.text1);
text1.setText(result.get(groupPosition));
return inflate;
}
/*
* (non-Javadoc)
*
* @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean,
* android.view.View, android.view.ViewGroup)
*/
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View inflate = View.inflate(context, android.R.layout.simple_expandable_list_item_2, null);
TextView text2 = (TextView) inflate.findViewById(android.R.id.text2);
String name = childBeans.get(groupPosition).result.bookList.get(childPosition).name;
text2.setText(name);
return inflate;
}
/*
* (non-Javadoc)
*
* @see android.widget.ExpandableListAdapter#isChildSelectable(int, int)
*/
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}