package com.edu.niit.newsapp_adapter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;
import com.edu.niit.newsapp_adapter.adapter.MyAdapter;
import com.edu.niit.newsapp_adapter.config.SystemConfig;
import com.edu.niit.newsapp_adapter.util.HttpUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private ListView lv_datas;
private MyAdapter myAdapter;
private List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
private Map<String, Object> map = new HashMap<String, Object>();//用于存放获取到的title和time
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case SystemConfig.MSG_TYPE_CHANNEL:
String response = msg.obj.toString();
//创建JSONObject对象,并且取出所需要的object对象
JSONObject object = null;
try {
object = new JSONObject(response);
int showapi_res_code = 0;
showapi_res_code = object.getInt("showapi_res_code");
if (showapi_res_code == 0) {
JSONObject body_object = object.getJSONObject("showapi_res_body");
//创建JSONArray对象
JSONArray channelListArray = body_object.getJSONArray("channelList");
//取出频道列表第一个channelId
String channelId = channelListArray.getJSONObject(0).getString("channelId");
HttpUtil.httpGetData_Get(SystemConfig.GET_NEWS + "?channelId=" + channelId + "&page=1&needContent=0" + "&needHtml=1", handler, SystemConfig.MSG_TYPE_NEWS);
}
} catch (JSONException e) {
e.printStackTrace();
}
break;
case SystemConfig.MSG_TYPE_NEWS:
String newsRes = msg.obj.toString();
Log.i("news_content",newsRes);
try {
JSONObject jsonObject = new JSONObject(newsRes);
int showapi_res_code = jsonObject.getInt("showapi_res_code");
if (showapi_res_code == 0) {
JSONObject showapi_res_body = jsonObject.getJSONObject("showapi_res_body");
int ret_code = showapi_res_body.getInt("ret_code");
if (ret_code == 0) {
JSONObject pagebean = showapi_res_body.getJSONObject("pagebean");
JSONArray contentlist = pagebean.getJSONArray("contentlist");
myAdapter.setData(contentlist);
myAdapter.notifyDataSetChanged();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_datas = (ListView) findViewById(R.id.lv_datas);
myAdapter = new MyAdapter(this);
HttpUtil.httpGetData_Get(SystemConfig.GET_CHANNEL, handler, SystemConfig.MSG_TYPE_CHANNEL);
//绑定Adapter
lv_datas.setAdapter(myAdapter);
}
}
1 代码能够正常运行,实现了预期的功能,逻辑正确。
2 有些代码并不能很容易的去理解,因为有些代码是从网上download的。
3 有些代码并不符合我所遵循的编程规范,但是大部分是符合的。
4 存在一些多余的代码也会有一些重复的但是是很少的。
5 代码的模块化还没有完全实现。