程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-小白 在做Android新聞端 用json獲取網頁數據 出現如下圖的報錯 不知道怎麼改

android-小白 在做Android新聞端 用json獲取網頁數據 出現如下圖的報錯 不知道怎麼改

編輯:編程綜合問答
小白 在做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;
    }
}

最佳回答:


String jsonData = (String) msg.obj;
System.out.println(jsonData);
try {
JSONArray jsonArray = new JSONArray(jsonData);
日志裡面說37行有問題,我覺得也是,給的是JSONObject ,而你將之轉換成JSONArray,會出現這種錯誤

QQ863017854
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved