在android中显示Json数据

时间:2016-07-24 11:49:23

标签: android json

我试图在Android应用程序中显示json数据,但我有困难,我认为可能与json文件格式化的方式有关。我想在header数组中获取名称和代码的值。并且它无法正常工作

这是json文件

@ManagedProperty("#{portfolioService}")
private PortfolioService portfolioService;

这是javaclass

 {
status: "SUCCESS",
headers: 
{
id: "4",
name: "GLO",
code: "GLO",
background_color_code: "15B709",
text_color_code: "ffffff",
statusMessage: "Hi +234805, an ACCESS FEE of N20.00 will be charged in order to access this Platform"
},
statusMessage: "Movies Loaded successfully",
caption: "Discover"
}

我收到此错误

package com.example.cann;

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONStringer;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;

import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class CategoryActivity extends ListActivity {

    private static final String TAG_POSTS = "headers";
    public static final String TAG_PIC = "name";
    public static final String TAG_NOTE = "code";
    JSONParser jParser;
    private static final String URL_CATEGORY =  "https://dobox.tv/api/home.json?platform=Android&api=1.1";
    private BaseAdapter mAdapter;
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);
        lv = getListView();
        lv.setDivider(null);

        lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {

                Toast.makeText(CategoryActivity.this, "Item selected: " + position,
                        Toast.LENGTH_LONG).show();

                // Uncomment this to start a new Activity for a chosen item
                /* Intent i = new Intent(getApplicationContext(),
                        ItemListActivity.class);

                String category_id = ((TextView) view
                        .findViewById(R.id.category_id)).getText()
                        .toString();
                i.putExtra("category_id", category_id);

                startActivity(i);*/
            }
        });

                    new LoadComments().execute();
    }

    class LoadComments extends AsyncTask<Void, Void, ArrayList<HashMap<String,String>>> {
        private ProgressDialog pDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(CategoryActivity.this);

            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);

        }

        @Override
        protected ArrayList<HashMap<String, String>> doInBackground(Void... arg0) {

            ArrayList<HashMap<String, String>> categoryList = new ArrayList<HashMap<String, String>>();
            jParser = new JSONParser();

            JSONObject json = jParser.getJSONFromUrl(URL_CATEGORY);

            try {
                  JSONArray categories =json.getJSONArray("headers");
                for (int i = 0; i < categories.length(); i++) {
                    String state = categories.getJSONObject(i).getString("name");
                    String status = categories.getJSONObject(i).getString("code");

                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TAG_PIC, state);
                    map.put(TAG_NOTE, status);
                    categoryList.add(map);

                }
            }catch (Throwable e){
                e.printStackTrace();
            }
            return categoryList;
        }

        @Override
        protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
            super.onPostExecute(result);
            if(pDialog.isShowing()){
                pDialog.dismiss();
            }
            mAdapter = new CategoryListAdapter(CategoryActivity.this,result);
            lv.setAdapter(mAdapter);
            mAdapter.notifyDataSetChanged();
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

您提供的JSON文件不是有效的JSON格式。还有&#39;标题&#39;属性包含JSONObject,而不是JSONArray。当你试图将JSONObject作为JSONArray时,你得到了JSON Exception。

你可以试试这个:

{
   "status": "SUCCESS",
   "headers": [
   {
      "id": "4",
      "name": "GLO",
      "code": "GLO",
      "background_color_code": "15B709",
      "text_color_code": "ffffff",
      "statusMessage": "Hi +234805, an ACCESS FEE of N20.00 will be charged in order to access this Platform"
   },
   {
      "id": "5",
      "name": "PLU",
      "code": "PLU",
      "background_color_code": "15B709",
      "text_color_code": "ffffff",
      "statusMessage": "Hi +234805, an ACCESS FEE of N20.00 will be charged in order to access this Platform"
   }
   ],
   "statusMessage": "Movies Loaded successfully",
   "caption": "Discover"
}

请先检查您的JSON数据是否有效。您可以从herehere

获取帮助