有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用JSON解析从任何url解析数据

当我解析来自给定URL的数据时,它会给出响应,如logcat所示。 但当我运行这个程序时,我无法将数据输入列表,它也没有在emulator上给出任何输出或数据

这是我的MainActivity代码:

public class MainActivity extends ListActivity {

    private ProgressDialog pDialog;

    // URL to get contacts JSON
    private static String url = "http://docs.blackberry.com/sampledata.json";

    private static final String TAG_Type = "type";
    private static final String TAG_Color = "color";
    private static final String TAG_Fuel = "fuel";


    // Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList;

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

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

        ListView lv = getListView();

        // Calling async task to get json
        new GetContacts().execute();
    }

    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONArray contacts = new JSONArray(jsonStr);

                    // looping through All Contacts
                    for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);

                        // String id = c.getString(TAG_ID);
                        String name = c.getString(TAG_Type);
                        String email = c.getString(TAG_Color);
                        String address = c.getString(TAG_Fuel);

                        // tmp hashmap for single contact
                        HashMap<String, String> contact = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        contact.put(TAG_Type, name);
                        contact.put(TAG_Color, email);
                        contact.put(TAG_Fuel, address);

                        // adding contact to contact list
                        contactList.add(contact);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            ListAdapter adapter = new SimpleAdapter(MainActivity.this,
                    contactList, R.layout.list_item, new String[] { TAG_Type,
                            TAG_Color, TAG_Fuel }, new int[] { R.id.name,
                            R.id.email, R.id.mobile });

            setListAdapter(adapter);
        }

    }

}

共 (1) 个答案

  1. # 1 楼答案

    您声明的密钥是错误的
    按响应页面中的方式给出密钥

    private static final String TAG_Type = "vehicleType";
    private static final String TAG_Color = "vehicleColor";
    private static final String TAG_Fuel = "fuel";