QQ863017854 2016-01-14 07:44 采纳率: 100%
浏览 1662
已采纳

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

图片说明!求告知 怎么改!
可以看这个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;
    }
}
  • 写回答

4条回答

  • 小星星1314 2016-01-14 08:53
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决