【问题标题】:How to parse data from any url using JSON parsing如何使用 JSON 解析从任何 url 解析数据
【发布时间】:2015-12-11 08:20:46
【问题描述】:

当我从给定的 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);
        }

    }

}

【问题讨论】:

    标签: java android json parsing


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-29
      • 1970-01-01
      • 2019-08-11
      • 2015-08-12
      • 2016-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多