android - 使用 volley 库在 android 中获取 JSONObject 的值

标签 android android-volley android-webservice

我想使用 volley 库在 android 中获取 JSONObject 的值。

这是我的 MainActvity.java 代码。

 String url = "http://someexample.com/signin.php";
        StringRequest postRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response).getJSONObject("type");
                            String myObjAsString = jsonResponse.toString();

                            mTV.setText(myObjAsString.toString());

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }
        ) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                // the POST parameters:
                params.put("email", "example@outlook.com");
                params.put("password", "********");
                return params;
            }
        };
        Volley.newRequestQueue(this).add(postRequest);

这是我的网络服务响应。

{
   "type":"1",
   "data":{
      "userID":"197",
      "name":"Hasnain",
      "email":"example@outlook.com",
      "paymentstatus":1
   },
   "title":"Message",
   "message":"User loged in successfully",
   "_explicitType":null
}

这是我的错误堆栈。

09-08 11:22:21.068    4705-4825/com.example.alphabooksingintest D/OpenGLRenderer﹕ Enabling debug mode 0
09-08 11:22:21.135    4705-4742/com.example.alphabooksingintest W/art﹕ Suspending all threads took: 18.063ms
09-08 11:22:21.212    4705-4705/com.example.alphabooksingintest W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
09-08 11:22:30.150    4705-4742/com.example.alphabooksingintest W/art﹕ Suspending all threads took: 16.658ms
09-08 11:22:30.918    4705-4705/com.example.alphabooksingintest W/System.err﹕ org.json.JSONException: Value 1 at type of type java.lang.String cannot be converted to JSONObject
09-08 11:22:30.926    4705-4705/com.example.alphabooksingintest W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:100)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at org.json.JSONObject.getJSONObject(JSONObject.java:613)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at com.example.alphabooksingintest.MainActivity$1.onResponse(MainActivity.java:42)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at com.example.alphabooksingintest.MainActivity$1.onResponse(MainActivity.java:38)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5312)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
09-08 11:23:37.309    4705-4742/com.example.alphabooksingintest W/art﹕ Suspending all threads took: 5.080ms

我想从响应中获取“type”对象的值。

最佳答案

错误:

org.json.JSONException: Value 1 at type of type java.lang.String cannot be converted to JSONObject

从错误日志中,我可以看到您正在尝试将 JSONObject 转换为 String,因此您得到了 JSONException

您需要通过这种方式从 JSONObject 获取 type String 值。

试试这段代码:

JSONObject jsonResponse = new JSONObject(response);
String myObjAsString = jsonResponse.getString("type");

代替

JSONObject jsonResponse = new JSONObject(response).getJSONObject("type");
String myObjAsString = jsonResponse.toString();

希望对您有所帮助!

关于android - 使用 volley 库在 android 中获取 JSONObject 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32450663/

相关文章:

android - GCM(Google Cloud Messaging)在android编程中是否比Web服务通信有任何优势

java - 使用 ksoap2 创建多级标签的 Soap 请求

java - 使用 Java Vuforia API 需要通过 com.vuforia.Vuforia 类进行初始化

android - 如何使用 Volley 库在 Android 中发送授权 header ?

java - 如何在Android上的所有 fragment 中保留对Web服务的身份验证?

Android 截击 : how to make json request without returned object?

android - 有什么办法可以保持 Volley 请求直到响应完成

android - FQL,用于获取用户如何连接到已登录用户(如链接到)

android - Android logcat 中的 SRIB_DCS 日志记录是什么?如何停止它?

android - SQLite 中 "not equal"的语法是什么?