android新闻内容json,小白 在做Android新闻端 用json获取网页数据 出现如下图的报错 不知道怎么改...

该博客展示了如何在Android应用中从网络获取JSON数据,解析JSON数组,提取关键信息如新闻标题、日期、评论数和图片URL,并将这些数据存储到自定义的News对象列表中。然后使用ListView展示数据,实现列表项点击跳转到详细页面。内容涵盖了HttpUtils类的使用、Handler消息处理、ListView适配器和自定义视图的创建。
摘要由CSDN通过智能技术生成

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

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();

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);

}

}

publi朋不功事做时次功好来多这开制的请一例农在c 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 newsList;

public NewsAdapter(Context context, List 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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值