Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用volley库获取android中JSONObject的值_Android_Android Volley_Android Webservice - Fatal编程技术网

使用volley库获取android中JSONObject的值

使用volley库获取android中JSONObject的值,android,android-volley,android-webservice,Android,Android Volley,Android Webservice,我想使用volley库获取android中JSONObject的值 这是我的maintactivity.java代码 String url = "http://someexample.com/signin.php"; StringRequest postRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {

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

这是我的maintactivity.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);
这是我的错误堆栈

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”对象的值。

错误:

{
   "type":"1",
   "data":{
      "userID":"197",
      "name":"Hasnain",
      "email":"example@outlook.com",
      "paymentstatus":1
   },
   "title":"Message",
   "message":"User loged in successfully",
   "_explicitType":null
}
org.json.JSONException:java.lang.String类型的值1 无法转换为JSONObject

从错误日志中,我可以看到您正在尝试将JSONObject转换为字符串,因此您得到的是JSONException

您需要通过这种方式从JSONObject获取类型字符串值

请尝试以下代码:

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

JSONObject jsonResponse = new JSONObject(response).getJSONObject("type");
String myObjAsString = jsonResponse.toString();
我希望有帮助

错误:

org.json.JSONException:java.lang.String类型的值1 无法转换为JSONObject

从错误日志中,我可以看到您正在尝试将JSONObject转换为字符串,因此您得到的是JSONException

您需要通过这种方式从JSONObject获取类型字符串值

请尝试以下代码:

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

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

我希望有帮助

这是因为您想要一个JSON对象作为响应,但仍然使用字符串请求。请尝试使用JSON对象请求代替JSON字符串请求。这将起作用

这是因为您想要一个JSON对象作为响应,但仍然使用字符串请求。尝试使用JSON对象请求代替JSON字符串请求。这将起作用

这是因为您想要一个JSON对象作为响应,但仍然使用字符串请求。请尝试使用JSON对象请求代替JSON字符串请求。这将起作用类型为的JSONObject

try {
     JSONObject jsonResponse = new JSONObject(response);
     String myObjAsString = jsonResponse.getString("type");
     mTV.setText(myObjAsString.toString());
}catch(JSONException e) {
  e.printStackTrace();
}
使用类型获取JSONObject

try {
     JSONObject jsonResponse = new JSONObject(response);
     String myObjAsString = jsonResponse.getString("type");
     mTV.setText(myObjAsString.toString());
}catch(JSONException e) {
  e.printStackTrace();
}

类型不是JSON对象。这是字符串看看Rajesh jadav的回答我对json是新手@AmolSawant96Kuli如果你在这个网站上查看我的json回复,它显示我的回复有五个对象,包括type。type不是json对象。这是字符串看看Rajesh jadav的回答我对json是新手@AmolSawant96Kuli如果你在这个网站上查看我的json回复,它显示我的回复有五个对象,包括类型。添加一些文本会更好添加一些文本会更好