1、建议修改的代码:
1 switch (msg.what) { 2 case SystemConfig.MSG_TYPE_CHANNEL: 3 String response = msg.obj.toString(); 4 //创建JSONObject对象 5 try { 6 ... 7 } 8 ... 9 }
意见:创建JSONObject对象后应该取出所需要的object对象。
修改后的代码:
1 switch (msg.what) { 2 case SystemConfig.MSG_TYPE_CHANNEL: 3 String response = msg.obj.toString(); 4 //创建JSONObject对象,并且取出所需要的object对象 5 JSONObject object = null; 6 try { 7 ... 8 } 9 ... 10 }
2、建议修改的代码:
1 if (showapi_res_code == 0) { 2 JSONObject body_object = object.getJSONObject("showapi_res_body"); 3 String channelId = channelListArray.getJSONObject(0).getString("channelId"); 4 HttpUtil.httpGetData_Get(SystemConfig.GET_NEWS + "?channelId=" + channelId + "&page=1&needContent=0" + "&needHtml=1", handler, SystemConfig.MSG_TYPE_NEWS); 5 }
意见:没有注释,最后一行代码过长不便于观看,还应该创建JSONArray对象
修改后的代码:
1 if (showapi_res_code == 0) { 2 JSONObject body_object = object.getJSONObject("showapi_res_body"); 3 //创建JSONArray对象 4 JSONArray channelListArray = body_object.getJSONArray("channelList"); 5 //取出频道列表第一个channelId 6 String channelId = channelListArray.getJSONObject(0).getString("channelId"); 7 HttpUtil.httpGetData_Get(SystemConfig.GET_NEWS + "?channelId 8 =" + channelId + "&page 9 =1&needContent=0" + "&needHtml 10 =1", handler, SystemConfig.MSG_TYPE_NEWS); 11 }
3、建议修改的代码:
1 @Override 2 protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.activity_main); 5 6 lv_datas = (ListView) findViewById(R.id.lv_datas); 7 myAdapter = new MyAdapter(this); 8 HttpUtil.httpGetData_Get(SystemConfig.GET_CHANNEL, handler, SystemConfig.MSG_TYPE_CHANNEL); 9 }
意见:最后应该绑定Adapter
修改后的代码:
1 @Override 2 protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.activity_main); 5 6 lv_datas = (ListView) findViewById(R.id.lv_datas); 7 myAdapter = new MyAdapter(this); 8 HttpUtil.httpGetData_Get(SystemConfig.GET_CHANNEL, handler, SystemConfig.MSG_TYPE_CHANNEL); 9 //绑定Adapter 10 lv_datas.setAdapter(myAdapter); 11 }
总的来讲,整体代码没有大的错误,只是大部分代码没有注释,而且过于冗长,不利于观看、检查,建议下次将部分重要代码注释,细心编程。