应用程序在程序运行后崩溃

时间:2016-04-29 12:56:03

标签: android

我有一个listview,显示来自url的名称,在json响应中还有news_id。我使用pojo作为getter和setter的名字和news_id。我想过滤所选列表视图的名称及其user_id,并将news_id保存在数组列表中。

private static final String TAG_HELLOGALLI =" hellogalli&#34 ;;     private static final String TAG_STATUS =" status&#34 ;;

JSONArray contacts = null;

ArrayList<HashMap<String,String>> contactList;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    contactList = new ArrayList<HashMap<String, String>>();
    ListView lv = getListView();


      new GetContacts().execute();
}

private class GetContacts extends AsyncTask<String, Void, ArrayList<HashMap<String, String>>>
{
    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("Please wait...");
        progressDialog.setCancelable(false);
        progressDialog.show();

    }

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
        ServiceHandler serviceHandler = new ServiceHandler();
        String jsonstr = serviceHandler.makeServiceCall(url, ServiceHandler.POST);
        Log.d("Response:", ">" + jsonstr);


            try {
                JSONObject jsonObject = new JSONObject(jsonstr);

                String status = jsonObject.getString(TAG_STATUS);

                 if (status.equals(0))
                 {
                     contacts = jsonObject.getJSONArray(TAG_MAIN);
                     Log.d("Contacts", String.valueOf(contacts));

                         for (int i = 0; i < contacts.length(); i++)
                         {
                         JSONObject c = contacts.getJSONObject(i);
                         String newsid = c.getString(TAG_NEWSID);
                         Log.d("NEWSID", newsid);
                         String title = c.getString(TAG_TITLE);
                         String newscat = c.getString(TAG_NEWSCAT);
                         String metatitle = c.getString(TAG_METATITLE);
                         String newscover = c.getString(TAG_NEWSCOVER);
                         String location = c.getString(TAG_LOCATION);
                         String metades = c.getString(TAG_METADES);
                         String newsshortdesc = c.getString(TAG_NEWSSHORTDESC);
                         String newsdes = c.getString(TAG_NEWSDES);
                         String tags = c.getString(TAG_TAGS);
                         String updatedate = c.getString(TAG_UPDATEDATE);
                         String sndate = c.getString(TAG_SNDATE);
                         String chknot = c.getString(TAG_CHKNOT);
                         String pagecount = c.getString(TAG_PAGECOUNT);
                         String hellogalli = c.getString(TAG_HELLOGALLI);
                         // String status = c.getString(TAG_STATUS);

                         HashMap<String, String> contact = new HashMap<String, String>();

                         contact.put(TAG_NEWSID, newsid);
                         contact.put(TAG_TITLE, title);
                         contact.put(TAG_NEWSCAT, newscat);
                         contact.put(TAG_METATITLE, metatitle);
                         contact.put(TAG_LOCATION, location);
                         contact.put(TAG_NEWSCOVER, newscover);
                         contact.put(TAG_METADES, metades);
                         contact.put(TAG_NEWSSHORTDESC, newsshortdesc);
                         contact.put(TAG_NEWSDES, newsdes);
                         contact.put(TAG_TAGS, tags);
                         contact.put(TAG_UPDATEDATE, updatedate);
                         contact.put(TAG_SNDATE, sndate);
                         contact.put(TAG_CHKNOT, chknot);
                         contact.put(TAG_PAGECOUNT, pagecount);
                         contact.put(TAG_HELLOGALLI, hellogalli);
                         //  contact.put(TAG_STATUS, status);
                         contactList.add(contact);
                     }
                 }
                   else if(status.equals(1))
                    {
                        Toast.makeText(getApplicationContext(), "There was some error! Please try again.", Toast.LENGTH_LONG).show();
                    }



            } catch (JSONException e) {
                e.printStackTrace();
            }
             return contactList;
    }
    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> aVoid)
    {
        super.onPostExecute(aVoid);
        if (progressDialog.isShowing())
            progressDialog.dismiss();
        ListAdapter adapter = new SimpleAdapter(
                MainActivity.this, contactList,
                R.layout.list_item, new String[] { TAG_NEWSID, TAG_TITLE,
                TAG_NEWSDES }, new int[] { R.id.name,
                R.id.email, R.id.mobile });

        setListAdapter(adapter);

    }
}

}

从uriin listview获取数据时出现

错误

04-29 18:19:13.770 30932-30958/com.example.esi_4.jsonparser D/Response:: >[{"news_id":"A9E70","title":"Adarsh Society Demolish Karne Ke Order Diye Bombay HC Ne ","news_cat":"","meta_title":"Adarsh Society Demolish Karne Ke Order Diye Bombay HC Ne ","meta_desc":"Adarsh Society Demolish Karne Ke Order Diye Bombay HC Ne ","news_cover":"IMG_20160429_154050.jpg","location":"","breaking_news":"1","news_desc":"<p>??<br \/>\r\nAdarsh Society Demolish Karne Ke Order Diye Bombay HC Ne&nbsp;<\/p>\r\n\r\n<p>? Bombay High Court Ne Adarsh Housing Society building Demolish Karne Ke Order Diye&nbsp;<\/p>\r\n\r\n<p>?The court also directed the state to initiate criminal prosecution against officers involved in the scam&nbsp;<\/p>\r\n\r\n<p>?Aur Plot restore Karne Ko Bhi Kaha Gaya&nbsp;<\/p>\r\n\r\n<p>?The society, originally meant to be a six-storey structure to house Kargil war heroes and war widows, was converted into a 100-metre-tall building<\/p>\r\n\r\n<p>?Court Ne Defence Ministry Ko Apne Officers Ke Khilaf Pehle Hi Action Na Lene Ke Liye Departmental Inquiry Conduct Karne &nbsp;Ko Kaha Hai&nbsp;<\/p>\r\n\r\n<p><br \/>\r\nGallinews.com&nbsp;<br \/>\r\nDownload Mobile Apps from<br \/>\r\nhttps:\/\/t.co\/LmRWlGO5Pm<\/p>\r\n\r\n<p>Facebook (For More IMP News)like&nbsp;<br \/>\r\nhttps:\/\/m.facebook.com\/gallinews\/<br \/>\r\nhttps:\/\/twitter.com\/gallinews?s=08<\/p>\r\n","news_short_desc":"","tags":"","sn_date":"0000-00-00 00:00:00","page_count":"15","update_date":"2016-04-29 05:16:49","chk_not":"0","hello_galli":"0","status":"0"},{"news_id":"BA8D7","title":"Firing at Wadi Bunder P Demello","news_cat":"","meta_title":"Firing at Wadi Bunder P Demello","meta_desc":"Firing at Wadi Bunder P Demello","news_cover":"dowbshshgsnload.jpg","location":"","breaking_news":"1","news_desc":"<p>??<\/p>\r\n\r\n<p>Firing at Wadi Bunder P Demello<\/p>\r\n\r\n<p>?Wadi Bunder Bridge ke niche Crime Branch(Spl Branch) ki team ne &nbsp;ki Firing&nbsp;<\/p>\r\n\r\n<p>?Drugs Peddlers ko pakadne aye &nbsp;Spl Branch ke officers ne ek drug peddlers ko tab goli maari tab woh Police par firing karne ki koshish kar raha tha<\/p>\r\n\r\n<p>?Drugs Peddlers ki gang Masjid Bunder Track par Bhag nikle&nbsp;<\/p>\r\n\r\n<p>Or Waha se pathar Marne lage<\/p>\r\n\r\n<p>\u26abOr Track or Andhera ka Sahara le kar Sab &nbsp;Peddlers Bhag gaya<\/p>\r\n\r\n<p>?Local People Complaint abt Firing jab un ko zor se Awaz ayee<\/p>\r\n\r\n<p>Gallinews.com&nbsp;<br \/>\r\nDownload Mobile Apps from<br \/>\r\nhttps:\/\/t.co\/LmRWlGO5Pm<\/p>\r\n\r\n<p>Facebook (For More IMP News)like&nbsp;<br \/>\r\nhttps:\/\/m.facebook.com\/gallinews\/<br \/>\r\nhttps:\/\/twitter.com\/gallinews?s=08<\/p>\r\n","news_short_desc":"","tags":"","sn_date":"0000-00-00 00:00:00","page_count":"183","update_date":"2016-04-28 13:35:06","chk_not":"0","hello_galli":"0","status":"0"},{"news_id":"CF6F2","title":"Trupti Desai ke khilaf mamla darj ","news_cat":"","meta_title":"Trupti Desai ke khilaf mamla darj ","meta_desc":"Trupti Desai ke khilaf mamla darj ","news_cover":"IMG_20160429_001010.jpg","location":"","breaking_news":"1","news_desc":"<p>??<\/p>\r\n\r\n<p>Trupti Desai ke khilaf mamla darj&nbsp;<\/p>\r\n\r\n<p>??Tardeo Police may Hua Bombay Police Act Ke Tahet mamla Darj<\/p>\r\n\r\n<p>?Trupti Desai par Bombay police Act ke tahet mamla darj<\/p>\r\n\r\n<p>?Bina Permission &nbsp;K Protest Karne Par Kiya Gya Case Registered&nbsp;<\/p>\r\n\r\n<p><br \/>\r\nGallinews.com&nbsp;<br \/>\r\nDownload Mobile Apps from<br \/>\r\nhttps:\/\/t.co\/LmRWlGO5Pm<\/p>\r\n\r\n<p>Facebook (For More IMP News)like&nbsp;<br \/>\r\nhttps:\/\/m.facebook.com\/gallinews\/<br \/>\r\nhttps:\/\/twitter.com\/gallinews?s=08<\/p>\r\n","news_short_desc":"","tags":"","sn_date":"0000-00-00 00:00:00","page_count":"123","update_date":"2016-04-28 11:43:52","chk_not":"0","hello_galli":"0","status":"0"},{"news_id":"364CF","title":"Trupti Desai Detainrd by  Police ","news_cat":"","meta_title":"Trupti Desai Detainrd by  Police ","meta_desc":"Trupti Desai Detainrd by  Police ","news_cover":"Screenshot_2016-04-28-21-56-17-picsay_1.jpg","location":"","breaking_news":"1","news_desc":"<p>???<\/p>\r\
04-29 18:19:13.830 30932-30958/com.example.esi_4.jsonparser W/System.err: org.json.JSONException: Value [{"tags":"","status":"0","location":"","sn_date":"0000-00-00 00:00:00","news_cover":"IMG_20160429_154050.jpg","page_count":"15","news_id":"A9E70","title":"Adarsh Society Demolish Karne Ke Order Diye Bombay HC Ne ","meta_desc":"Adarsh Society Demolish Karne Ke Order Diye Bombay HC Ne ","news_cat":"","chk_not":"0","hello_galli":"0","news_short_desc":"","update_date":"2016-04-29 05:16:49","breaking_news":"1","meta_title":"Adarsh Society Demolish Karne Ke Order Diye Bombay HC Ne ","news_desc":"<p>??<br \/>\r\nAdarsh Society Demolish Karne Ke Order Diye Bombay HC Ne&nbsp;<\/p>\r\n\r\n<p>? Bombay High Court Ne Adarsh Housing Society building Demolish Karne Ke Order Diye&nbsp;<\/p>\r\n\r\n<p>?The court also directed the state to initiate criminal prosecution against officers involved in the scam&nbsp;<\/p>\r\n\r\n<p>?Aur Plot restore Karne Ko Bhi Kaha Gaya&nbsp;<\/p>\r\n\r\n<p>?The society, originally meant to be a six-storey structure to house Kargil war heroes and war widows, was converted into a 100-metre-tall building<\/p>\r\n\r\n<p>?Court Ne Defence Ministry Ko Apne Officers Ke Khilaf Pehle Hi Action Na Lene Ke Liye Departmental Inquiry Conduct Karne &nbsp;Ko Kaha Hai&nbsp;<\/p>\r\n\r\n<p><br \/>\r\nGallinews.com&nbsp;<br \/>\r\nDownload Mobile Apps from<br \/>\r\nhttps:\/\/t.co\/LmRWlGO5Pm<\/p>\r\n\r\n<p>Facebook (For More IMP News)like&nbsp;<br \/>\r\nhttps:\/\/m.facebook.com\/gallinews\/<br \/>\r\nhttps:\/\/twitter.com\/gallinews?s=08<\/p>\r\n"},{"tags":"","status":"0","location":"","sn_date":"0000-00-00 00:00:00","news_cover":"dowbshshgsnload.jpg","page_count":"183","news_id":"BA8D7","title":"Firing at Wadi Bunder P Demello","meta_desc":"Firing at Wadi Bunder P Demello","news_cat":"","chk_not":"0","hello_galli":"0","news_short_desc":"","update_date":"2016-04-28 13:35:06","breaking_news":"1","meta_title":"Firing at Wadi Bunder P Demello","news_desc":"<p>??<\/p>\r\n\r\n<p>Firing at Wadi Bunder P Demello<\/p>\r\n\r\n<p>?Wadi Bunder Bridge ke niche Crime Branch(Spl Branch) ki team ne &nbsp;ki Firing&nbsp;<\/p>\r\n\r\n<p>?Drugs Peddlers ko pakadne aye &nbsp;Spl Branch ke officers ne ek drug peddlers ko tab goli maari tab woh Police par firing karne ki koshish kar raha tha<\/p>\r\n\r\n<p>?Drugs Peddlers ki gang Masjid Bunder Track par Bhag nikle&nbsp;<\/p>\r\n\r\n<p>Or Waha se pathar Marne lage<\/p>\r\n\r\n<p>⚫Or Track or Andhera ka Sahara le kar Sab &nbsp;Peddlers Bhag gaya<\/p>\r\n\r\n<p>?Local People Complaint abt Firing jab un ko zor se Awaz ayee<\/p>\r\n\r\n<p>Gallinews.com&nbsp;<br \/>\r\nDownload Mobile Apps from<br \/>\r\nhttps:\/\/t.co\/LmRWlGO5Pm<\/p>\r\n\r\n<p>Facebook (For More IMP News)like&nbsp;<br \/>\r\nhttps:\/\/m.facebook.com\/gallinews\/<br \/>\r\nhttps:\/\/twitter.com\/gallinews?s=08<\/p>\r\n"},{"tags":"","status":"0","location":"","sn_date":"0000-00-00 00:00:00","news_cover":"IMG_20160429_001010.jpg","page_count":"123","news_id":"CF6F2","title":"Trupti Desai ke khilaf mamla darj ","meta_desc":"Trupti Desai ke khilaf mamla darj ","news_cat":"","chk_not":"0","hello_galli":"0","news_short_desc":"","update_date":"2016-04-28 11:43:52","breaking_news":"1","meta_title":"Trupti Desai ke khilaf mamla darj ","news_desc":"<p>??<\/p>\r\n\r\n<p>Trupti Desai ke khilaf mamla darj&nbsp;<\/p>\r\n\r\n<p>??Tardeo Police may Hua Bombay Police Act Ke Tahet mamla Darj<\/p>\r\n\r\n<p>?Trupti Desai par Bombay police Act ke tahet mamla darj<\/p>\r\n\r\n<p>?Bina Permission &nbsp;K Protest Karne Par Kiya Gya Case Registered&nbsp;<\/p>\r\n\r\n<p><br \/>\r\nGallinews.com&nbsp;<br \/>\r\nDownload Mobile Apps from<br \/>\r\nhttps:\/\/t.co\/LmRWlGO5Pm<\/p>\r\n\r\n<p>Facebook (For More IMP News)like&nbsp;<br \/>\r\nhttps:\/\/m.facebook.com\/gallinews\/<br \/>\r\nhttps:\/\/twitter.com\/gallinews?s=08<\/p>\r\n"},{"tags":"","status":"0","location":"","sn_date":"0000-00-00 00:00:00","news_cover":"Screenshot_2016-04-28-21-56-17-picsay_1.jpg","page_count":"135","news_id":"364CF","title":"Trupti Desai Detainrd by  Police ","meta_desc":"Trupti Desai Detainrd by  Police ","news_cat
04-29 18:19:13.850 30932-30958/com.example.esi_4.jsonparser W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
04-29 18:19:13.860 30932-30958/com.example.esi_4.jsonparser W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:159)
04-29 18:19:13.860 30932-30958/com.example.esi_4.jsonparser W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:172)
04-29 18:19:13.860 30932-30958/com.example.esi_4.jsonparser W/System.err:     at com.example.esi_4.jsonparser.MainActivity$GetContacts.doInBackground(MainActivity.java:82)
04-29 18:19:13.860 30932-30958/com.example.esi_4.jsonparser W/System.err:     at com.example.esi_4.jsonparser.MainActivity$GetContacts.doInBackground(MainActivity.java:61)
04-29 18:19:13.860 30932-30958/com.example.esi_4.jsonparser W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-29 18:19:13.860 30932-30958/com.example.esi_4.jsonparser W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-29 18:19:13.870 30932-30958/com.example.esi_4.jsonparser W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-29 18:19:13.870 30932-30958/com.example.esi_4.jsonparser W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-29 18:19:13.870 30932-30958/com.example.esi_4.jsonparser W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-29 18:19:13.870 30932-30958/com.example.esi_4.jsonparser W/System.err:     at java.lang.Thread.run(Thread.java:841)
04-29 18:19:13.910 30932-30932/com.example.esi_4.jsonparser E/ViewRootImpl: sendUserActionEvent() mView == null

1 个答案:

答案 0 :(得分:0)

这里的问题是JSON文件。格式不正确,当您尝试从格式不正确的JSON文件中读取时,应用程序将崩溃。由于没有其他信息是我唯一的猜测。首先尝试验证JSON:http://jsonlint.com/

复制并粘贴您的JSON,您将看到此错误。

Error: Parse error on line 64:
...1",      "news_desc": "???<\/p>\r\
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'