JSON解析失败安卓工作室

时间:2015-07-29 04:57:46

标签: java android json android-listview android-studio

当我运行我的应用程序时,我得到了这个例外。当我在我的第一个json解析应用程序中使用它时,我的代码正常运行。但是当我在我的新应用程序中实现我的代码时,我得到了这个例外。但其他代码正常工作。

07-29 11:48:38.269    4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:111)
07-29 11:48:38.269    4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at org.json.JSONArray.<init>(JSONArray.java:96)
07-29 11:48:38.269    4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at org.json.JSONArray.<init>(JSONArray.java:108)
07-29 11:48:38.269    4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at com.fingerspot.mencobaagain.Product$LoadUsers.doInBackground(Product.java:89)
07-29 11:48:38.279    4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at com.fingerspot.mencobaagain.Product$LoadUsers.doInBackground(Product.java:59)
07-29 11:48:38.289    4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-29 11:48:38.289    4670-4679/com.fingerspot.mencobaagain I/dalvikvm﹕ Total arena pages for JIT: 14
07-29 11:48:38.289    4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-29 11:48:38.289    4670-4679/com.fingerspot.mencobaagain I/dalvikvm﹕ Total arena pages for JIT: 15
07-29 11:48:38.299    4670-4679/com.fingerspot.mencobaagain I/dalvikvm﹕ Total arena pages for JIT: 16
07-29 11:48:38.299    4670-4679/com.fingerspot.mencobaagain I/dalvikvm﹕ Total arena pages for JIT: 17
07-29 11:48:38.299    4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-29 11:48:38.299    4670-4679/com.fingerspot.mencobaagain I/dalvikvm﹕ Total arena pages for JIT: 18
07-29 11:48:38.299    4670-4679/com.fingerspot.mencobaagain I/dalvikvm﹕ Total arena pages for JIT: 19
07-29 11:48:38.299    4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-29 11:48:38.299    4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-29 11:48:38.309    4670-4679/com.fingerspot.mencobaagain I/dalvikvm﹕ Total arena pages for JIT: 20
07-29 11:48:38.309    4670-4767/com.fingerspot.mencobaagain W/System.err﹕ at java.lang.Thread.run(Thread.java:841)

这里是代码

protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        // getting JSON string from URL
        String json = jsonParser.makeHttpRequest(url, "GET",
                params);

        // Check your log cat for JSON reponse
        Log.d("Albums JSON: ", "> " + json);

        try {
            users = new JSONArray(json);

            if (users != null) {
                // looping through All albums

                for (int i = 0; i < users.length(); i++) {
                    JSONObject c = users.getJSONObject(i);
                    Toast.makeText(getBaseContext(),"errrrrrorrrrr",Toast.LENGTH_LONG).show();
                    // Storing each json item values in variable
                    String user_id = c.getString("user_id");
                    String name = c.getString("name");
                    String email = c.getString("email");
                    String display_name = c.getString("display_name");
                    String phone = c.getString("phone");
                    String ym = c.getString("ym");
                    String created_at = c.getString("created_at");
                    String last_login = c.getString("last_login");
                    String updated_at = c.getString("updated_at");
                    // creating new HashMap
                    //HashMap<String, String> map = new HashMap<String, String>();

                    items.add(new ProductClass(Integer.parseInt(user_id), name, email, display_name,phone, ym, created_at, last_login, updated_at));
                }
            }else{
                Log.d("Users: ", "null");
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

我预计我的代码在获取json对象时出现了问题。

注意:我在旧应用程序和新应用程序上使用的json是相同的。

我的json对象

{"DataUser":[{"user_id":1,"name":"Admin HO","email":"admin@kitasatu.net","display_name":null,"phone":null,"ym":null,"status":0,"location_id":0,"last_login":"2015-07-07 07:51:12","active":1,"created_at":"2015-05-02 00:00:00","updated_at":"2015-07-07 07:51:12"}]}

2 个答案:

答案 0 :(得分:3)

您应首先将json字符串解析为JSON Object。

    {
    "DataUser":[
        {
        "user_id":1,
        "name":"Admin HO",
        "email":"admin@kitasatu.net",
        "display_name":null,
        "phone":null,
        "ym":null,
        "status":0,
        "location_id":0,
        "last_login":"2015-07-07 07:51:12",
        "active":1,
        "created_at":"2015-05-02 00:00:00",
        "updated_at":"2015-07-07 07:51:12"
        }
    ]
   }

之后,您应该从之前解析的JSON对象中获取json数组。 &#34; DataUser&#34;

protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        // getting JSON string from URL
        String json = jsonParser.makeHttpRequest(url, "GET",
                params);

        // Check your log cat for JSON reponse
        Log.d("Albums JSON: ", "> " + json);

        JSONObject jsonObj = new JSONObject(json);

        try {
            users = jsonObj.getJSONArray("DataUser");

            if (users != null) {
                // looping through All albums

                for (int i = 0; i < users.length(); i++) {
                    JSONObject c = users.getJSONObject(i);
                    Toast.makeText(getBaseContext(),"errrrrrorrrrr",Toast.LENGTH_LONG).show();
                    // Storing each json item values in variable
                    String user_id = c.getString("user_id");
                    String name = c.getString("name");
                    String email = c.getString("email");
                    String display_name = c.getString("display_name");
                    String phone = c.getString("phone");
                    String ym = c.getString("ym");
                    String created_at = c.getString("created_at");
                    String last_login = c.getString("last_login");
                    String updated_at = c.getString("updated_at");
                    // creating new HashMap
                    //HashMap<String, String> map = new HashMap<String, String>();

                    items.add(new ProductClass(Integer.parseInt(user_id), name, email, display_name,phone, ym, created_at, last_login, updated_at));
                }
            }else{
                Log.d("Users: ", "null");
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

答案 1 :(得分:0)

我知道这不是解决问题的方法,但你应该GSON来解析你的json响应。因为许多开源项目正在使用gson。因为GSON提高了代码的可读性,并使解析时间比传统的最小化了30%。以下教程将帮助您理解它。http://kylewbanks.com/blog/Tutorial-Android-Parsing-JSON-with-GSON

GSon.jar

下载Gson.jar

如果您在创建gson模型时遇到问题,那么您可以从Gson models online

在线创建它们