JSONException运行时错误

时间:2015-10-05 07:30:52

标签: android

我是android编程的新手我在logcat中遇到如下运行时错误。对于我的程序中的jsonparser,请任何人检查并帮助我。我想在项目数组标题,date_taken和媒体“m”中只有我的网址中的3个字段。请帮助我。

 10-05 05:44:16.827: I/Choreographer(779): Skipped 155 frames!  The application may be doing too much work on its main thread.
10-05 05:44:16.827: I/ListViewActivity(779): {"generator":"http:\/\/www.flickr.com\/","title":"Recent Uploads tagged soccer","items":[{"tags":"iris copyright sports soccer be quick epping voetbal 2015 1887 sportclub silvolde hoofdklasse","author":"nobody@flickr.com (SportclubSilvolde)","title":"IMG_9207","description":" <p><a href=\"http:\/\/www.flickr.com\/people\/sportclubsilvolde\/\">SportclubSilvolde<\/a> posted a photo:<\/p> <p><a href=\"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21782884471\/\" title=\"IMG_9207\"><img src=\"http:\/\/farm1.staticflickr.com\/681\/21782884471_6834f2c9d8_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_9207\" \/><\/a><\/p> ","date_taken":"2015-09-27T15:00:50-08:00","link":"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21782884471\/","author_id":"124810748@N04","published":"2015-09-28T02:59:30Z","media":{"m":"http:\/\/farm1.staticflickr.com\/681\/21782884471_6834f2c9d8_m.jpg"}},{"tags":"iris copyright sports soccer be quick epping voetbal 2015 1887 sportclub silvolde hoofdklasse","author":"nobody@flickr.com (SportclubSilvolde)","title":"IMG_9484","description":" <p><a href=\"http:\/\/www.flickr.com\/people\/sportclubsilvolde\/\">SportclubSilvolde<\/a> posted a photo:<\/p> <p><a href=\"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21747301246\/\" title=\"IMG_9484\"><img src=\"http:\/\/farm1.staticflickr.com\/736\/21747301246_f03bc953d6_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_9484\" \/><\/a><\/p> ","date_taken":"2015-09-27T15:58:43-08:00","link":"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21747301246\/","author_id":"124810748@N04","published":"2015-09-28T02:59:38Z","media":{"m":"http:\/\/farm1.staticflickr.com\/736\/21747301246_f03bc953d6_m.jpg"}},{"tags":"iris copyright sports soccer be quick epping voetbal 2015 1887 sportclub silvolde hoofdklasse","author":"nobody@flickr.com (SportclubSilvolde)","title":"IMG_9135","description":" <p><a href=\"http:\/\/www.flickr.com\/people\/sportclubsilvolde\/\">SportclubSilvolde<\/a> posted a photo:<\/p> <p><a href=\"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21585600568\/\" title=\"IMG_9135\"><img src=\"http:\/\/farm1.staticflickr.com\/588\/21585600568_bce2bcd47a_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_9135\" \/><\/a><\/p> ","date_taken":"2015-09-27T14:53:53-08:00","link":"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21585600568\/","author_id":"124810748@N04","published":"2015-09-28T02:59:28Z","media":{"m":"http:\/\/farm1.staticflickr.com\/588\/21585600568_bce2bcd47a_m.jpg"}},{"tags":"iris copyright sports soccer be quick epping voetbal 2015 1887 sportclub silvolde hoofdklasse","author":"nobody@flickr.com (SportclubSilvolde)","title":"IMG_9062","description":" <p><a href=\"http:\/\/www.flickr.com\/people\/sportclubsilvolde\/\">SportclubSilvolde<\/a> posted a photo:<\/p> <p><a href=\"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21761757502\/\" title=\"IMG_9062\"><img src=\"http:\/\/farm6.staticflickr.com\/5648\/21761757502_e18ee1b76f_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_9062\" \/><\/a><\/p> ","date_taken":"2015-09-27T14:48:06-08:00","link":"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21761757502\/","author_id":"124810748@N04","published":"2015-09-28T02:59:27Z","media":{"m":"http:\/\/farm6.staticflickr.com\/5648\/21761757502_e18ee1b76f_m.jpg"}},{"tags":"iris copyright sports soccer be quick epping voetbal 2015 1887 sportclub silvolde hoofdklasse","author":"nobody@flickr.com (SportclubSilvolde)","title":"IMG_9718","description":" <p><a href=\"http:\/\/www.flickr.com\/people\/sportclubsilvolde\/\">SportclubSilvolde<\/a> posted a photo:<\/p> <p><a href=\"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21773428785\/\" title=\"IMG_9718\"><img src=\"http:\/\/farm6.staticflickr.com\/5798\/21773428785_256689c88f_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_9718\" \/><\/a><\/p> ","date_taken":"2015-09-27T16:27:04-08:00","link":"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21773428785\/","author_id":"124810748@N04","published":"2015-09-28T02:59:43Z","media":{"m":"http:\/\/f
10-05 05:44:16.827: W/System.err(779): org.json.JSONException: No value for media
10-05 05:44:16.827: W/System.err(779):  at org.json.JSONObject.get(JSONObject.java:354)
10-05 05:44:16.827: W/System.err(779):  at org.json.JSONObject.getJSONArray(JSONObject.java:544)
10-05 05:44:16.827: W/System.err(779):  at com.suni.sun.ListViewActivity.parseJsonResponse(ListViewActivity.java:60)
10-05 05:44:16.827: W/System.err(779):  at com.suni.sun.JsonParse.onPostExecute(JsonParse.java:70)
10-05 05:44:16.827: W/System.err(779):  at com.suni.sun.JsonParse.onPostExecute(JsonParse.java:1)
10-05 05:44:16.827: W/System.err(779):  at android.os.AsyncTask.finish(AsyncTask.java:631)
10-05 05:44:16.827: W/System.err(779):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-05 05:44:16.827: W/System.err(779):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-05 05:44:16.827: W/System.err(779):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-05 05:44:16.827: W/System.err(779):  at android.os.Looper.loop(Looper.java:137)
10-05 05:44:16.827: W/System.err(779):  at android.app.ActivityThread.main(ActivityThread.java:5041)
10-05 05:44:16.827: W/System.err(779):  at java.lang.reflect.Method.invokeNative(Native Method)
10-05 05:44:16.827: W/System.err(779):  at java.lang.reflect.Method.invoke(Method.java:511)
10-05 05:44:16.827: W/System.err(779):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-05 05:44:16.827: W/System.err(779):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-05 05:44:16.827: W/System.err(779):  at dalvik.system.NativeStart.main(Native Method)
10-05 05:44:16.827: I/JsonParse(779): {"generator":"http:\/\/www.flickr.com\/","title":"Recent Uploads tagged soccer","items":[{"tags":"iris copyright sports soccer be quick epping voetbal 2015 1887 sportclub silvolde hoofdklasse","author":"nobody@flickr.com (SportclubSilvolde)","title":"IMG_9207","description":" <p><a href=\"http:\/\/www.flickr.com\/people\/sportclubsilvolde\/\">SportclubSilvolde<\/a> posted a photo:<\/p> <p><a href=\"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21782884471\/\" title=\"IMG_9207\"><img src=\"http:\/\/farm1.staticflickr.com\/681\/21782884471_6834f2c9d8_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_9207\" \/><\/a><\/p> ","date_taken":"2015-09-27T15:00:50-08:00","link":"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21782884471\/","author_id":"124810748@N04","published":"2015-09-28T02:59:30Z","media":{"m":"http:\/\/farm1.staticflickr.com\/681\/21782884471_6834f2c9d8_m.jpg"}},{"tags":"iris copyright sports soccer be quick epping voetbal 2015 1887 sportclub silvolde hoofdklasse","author":"nobody@flickr.com (SportclubSilvolde)","title":"IMG_9484","description":" <p><a href=\"http:\/\/www.flickr.com\/people\/sportclubsilvolde\/\">SportclubSilvolde<\/a> posted a photo:<\/p> <p><a href=\"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21747301246\/\" title=\"IMG_9484\"><img src=\"http:\/\/farm1.staticflickr.com\/736\/21747301246_f03bc953d6_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_9484\" \/><\/a><\/p> ","date_taken":"2015-09-27T15:58:43-08:00","link":"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21747301246\/","author_id":"124810748@N04","published":"2015-09-28T02:59:38Z","media":{"m":"http:\/\/farm1.staticflickr.com\/736\/21747301246_f03bc953d6_m.jpg"}},{"tags":"iris copyright sports soccer be quick epping voetbal 2015 1887 sportclub silvolde hoofdklasse","author":"nobody@flickr.com (SportclubSilvolde)","title":"IMG_9135","description":" <p><a href=\"http:\/\/www.flickr.com\/people\/sportclubsilvolde\/\">SportclubSilvolde<\/a> posted a photo:<\/p> <p><a href=\"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21585600568\/\" title=\"IMG_9135\"><img src=\"http:\/\/farm1.staticflickr.com\/588\/21585600568_bce2bcd47a_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_9135\" \/><\/a><\/p> ","date_taken":"2015-09-27T14:53:53-08:00","link":"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21585600568\/","author_id":"124810748@N04","published":"2015-09-28T02:59:28Z","media":{"m":"http:\/\/farm1.staticflickr.com\/588\/21585600568_bce2bcd47a_m.jpg"}},{"tags":"iris copyright sports soccer be quick epping voetbal 2015 1887 sportclub silvolde hoofdklasse","author":"nobody@flickr.com (SportclubSilvolde)","title":"IMG_9062","description":" <p><a href=\"http:\/\/www.flickr.com\/people\/sportclubsilvolde\/\">SportclubSilvolde<\/a> posted a photo:<\/p> <p><a href=\"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21761757502\/\" title=\"IMG_9062\"><img src=\"http:\/\/farm6.staticflickr.com\/5648\/21761757502_e18ee1b76f_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_9062\" \/><\/a><\/p> ","date_taken":"2015-09-27T14:48:06-08:00","link":"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21761757502\/","author_id":"124810748@N04","published":"2015-09-28T02:59:27Z","media":{"m":"http:\/\/farm6.staticflickr.com\/5648\/21761757502_e18ee1b76f_m.jpg"}},{"tags":"iris copyright sports soccer be quick epping voetbal 2015 1887 sportclub silvolde hoofdklasse","author":"nobody@flickr.com (SportclubSilvolde)","title":"IMG_9718","description":" <p><a href=\"http:\/\/www.flickr.com\/people\/sportclubsilvolde\/\">SportclubSilvolde<\/a> posted a photo:<\/p> <p><a href=\"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21773428785\/\" title=\"IMG_9718\"><img src=\"http:\/\/farm6.staticflickr.com\/5798\/21773428785_256689c88f_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_9718\" \/><\/a><\/p> ","date_taken":"2015-09-27T16:27:04-08:00","link":"http:\/\/www.flickr.com\/photos\/sportclubsilvolde\/21773428785\/","author_id":"124810748@N04","published":"2015-09-28T02:59:43Z","media":{"m":"http:\/\/farm6.st
10-05 05:44:17.983: I/Choreographer(779): Skipped 132 frames!  The application may be doing too much work on its main thread.
10-05 05:44:21.452: I/Choreographer(779): Skipped 117 frames!  The application may be doing too much work on its main thread.

    my listview activity.

包com.suni.sun;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.suni.sun.JsonParse;
import com.suni.sun.HomeGv;


public class ListViewActivity extends Activity  {

    private ListView lv;
    private ArrayList<HomeGv> img;
    private ArrayAdapter<HomeGv> adap;

    private final static String TAG = ListViewActivity.class.getSimpleName();
    private final static String url = "http://saicloudica.cloudapp.net/json.txt";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        lv = (ListView) findViewById(R.id.listView);
        setListViewAdapter();
        getDataFromInternet();
    }

    private void getDataFromInternet(){
        new JsonParse(this, url).execute();
    }

    private void setListViewAdapter(){
        img = new ArrayList<HomeGv>();
        adap = new CustomListViewAdapter(this, R.layout.home_item, img);
        lv.setAdapter(adap);
    }

    public void parseJsonResponse(String result){
        Log.i(TAG, result);
        try{
            JSONObject json = new JSONObject(result);
            JSONArray jArray = new JSONArray(json.getString("items"));
            for (int i = 0; i < jArray.length(); i++){
                JSONObject jObj = jArray.getJSONObject(i);

                HomeGv imgs = new HomeGv();
                imgs.setTitle(jObj.getString("title"));
                imgs.setDate(jObj.getString("date_taken"));
                JSONArray media = json.getJSONArray("media");
                if(null != media && media.length() > 0) {
                    JSONObject medi = media.getJSONObject(0);
                    if(medi != null){
                        imgs.setMedia(medi.getString("m"));
                    }
                }


                img.add(imgs);
            }
            adap.notifyDataSetChanged();
        } catch (JSONException e){

            e.printStackTrace();
        }
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("EXIT", true);
        startActivity(intent);
    }
}




 my home class
        package com.suni.sun;

    public class HomeGv {

        private String title;
        private String media;
        private String date_taken;

        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        public String getImageUrl() {
            return media;
        }
        public void setMedia(String m) {
            this.media = m;
        }
        public String getDate() {
            return date_taken;
        }
        public void setDate(String date_taken) {
            this.date_taken = date_taken;
        }
    }

2 个答案:

答案 0 :(得分:0)

像这样处理JSON异常

if(json.has("media")) {
    // parse JSON here
}

两件事可能出错了

Your JSON doesnt have the tag media
You are parsing JSON wrong (looking for media inside wrong parent tag)

答案 1 :(得分:0)

media包含在名为JSONArray的{​​{1}}中,它是items,而不是JSONObject。这一行

JSONArray

应该是

JSONArray media = json.getJSONArray("media");

其中JSONObject media = jObj.optJSONObject("media"); if (media != null) { } 是您jObj的{​​{1}}