繁体   English   中英

如何使用JSON解析从任何URL解析数据

[英]How to parse data from any url using JSON parsing

当我解析来自给定URL的数据时,它会给出logcat显示的响应。 但我无法将数据导入列表,并且在运行此程序时它没有在模拟器上提供任何输出或数据。

这是我的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);
        }

    }

}

您声明的密钥是错误的。
在响应页面中提供密钥

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM