可以看这个http://ask.csdn.net/questions/232048
private Handler getNewsHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
String jsonData = (String) msg.obj;
System.out.println(jsonData);
try {
JSONArray jsonArray = new JSONArray(jsonData);
for (int i=0;i<jsonArray.length();i++){
JSONObject object = jsonArray.getJSONObject(i);
String id = object.getString("id");
String title = object.getString("title");
String excerpt = object.getString("excerpt");
String date = object.getString("date");
String comment_count = object.getString("comment_count");
// String custom_fields = object.getString("custom_fields");
String thumb_value_URL = object.getString("thumb_value_URL");
System.out.println("title = " +title);
/*System.out.println("thumb_value = " +thumb_value);*/
newsList.add(new News(title, date, comment_count, thumb_value_URL));
}
adapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvNews = (ListView) findViewById(R.id.lvNews);
newsList = new ArrayList<News>();
adapter = new NewsAdapter(this, newsList);
lvNews.setAdapter(adapter);
lvNews.setOnItemClickListener(this);
HttpUtils.getNewsJSON(GET_NEWS_URL, getNewsHandler);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
News news = newsList.get(position);
Intent intent = new Intent(this, BrowseNewsActivity.class);
intent.putExtra("Excerpt", 11);
startActivity(intent);
}
}
public class News {
private String title;
// private String excerpt;
private String date;
// private String id;
private String comment_count;
private String thumb_value;
// private String custom_fields;
public News(String title, String date, String comment_count, String thumb_value ){
setTitle(title);
// setExcerpt(excerpt);
setDate(date);
// setId(id);
setComment_count(comment_count);
// setCustom_fields(custom_fields);
setThumb_value(thumb_value);}
public String getComment_count() {
return comment_count;
}
public void setComment_count(String comment_count) {
this.comment_count = comment_count;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getThumb_value() {
return thumb_value;
}
public void setThumb_value(String thumb_value) {
this.thumb_value = thumb_value;
}
}
public class NewsAdapter extends BaseAdapter {
private Context context;
private List<News> newsList;
public NewsAdapter(Context context, List<News> newsList){
this.context = context;
this.newsList = newsList;
}
@Override
public int getCount() {
return newsList.size();
}
@Override
public News getItem(int position) {
return newsList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.news_item, null);
}
TextView Title = (TextView) convertView.findViewById(R.id.Title);
// TextView excerpt = (TextView) convertView.findViewById(R.id.excerpt);
TextView date = (TextView) convertView.findViewById(R.id.date);
TextView comment_count = (TextView)convertView.findViewById(R.id.comment_count);
ImageView thumb_value_URL = (ImageView) convertView.findViewById(R.id.thumb_value);
News news = newsList.get(position);
Title.setText(news.getTitle());
// excerpt.setText(news.getExcerpt());
date.setText(news.getDate());
comment_count.setText(news.getComment_count());
// String custom_fields = news.getCustom_fields();
/* HttpUtils.setPicBitmap(thumb_value,thumb_value_URL);*/
return convertView;
}
}
String jsonData = (String) msg.obj; System.out.println(jsonData); try { JSONArray jsonArray = new JSONArray(jsonData);日志里面说37行有问题,我觉得也是,给的是JSONObject ,而你将之转换成JSONArray,会出现这种错误
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。