开发者社区> 问答> 正文

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

screenshot
可以看这个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;
    }
}

展开
收起
杨冬芳 2016-07-06 19:03:46 2853 0
1 条回答
写回答
取消 提交回答
  • IT从业

    String jsonData = (String) msg.obj; System.out.println(jsonData); try { JSONArray jsonArray = new JSONArray(jsonData);日志里面说37行有问题,我觉得也是,给的是JSONObject ,而你将之转换成JSONArray,会出现这种错误

    2019-07-17 19:52:25
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载