Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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
如何使用android volley JsonObjectRequest[com.android.volley.ParseError:org.json.JSONException]处理来自php的字符串响应?_Json_Android Volley_Android Json_Jsonexception - Fatal编程技术网

如何使用android volley JsonObjectRequest[com.android.volley.ParseError:org.json.JSONException]处理来自php的字符串响应?

如何使用android volley JsonObjectRequest[com.android.volley.ParseError:org.json.JSONException]处理来自php的字符串响应?,json,android-volley,android-json,jsonexception,Json,Android Volley,Android Json,Jsonexception,实际上,当我们调用API并以JSON格式发送请求时,我们期望响应也会变成JSON格式。但在这里,后端团队以字符串格式向我发送响应,因此调用了onErrorResponse()方法。这里我的状态码是200。但由于响应的格式没有在onResponse()方法上执行。你能帮我处理这件事吗?可能我必须在这里使用CustomRequest。如有任何建议,将不胜感激。谢谢 我使用了截取json对象请求来访问服务器 我的请求格式是: 我的SampleJsonObjTask类是: public class Sa

实际上,当我们调用API并以JSON格式发送请求时,我们期望响应也会变成JSON格式。但在这里,后端团队以字符串格式向我发送响应,因此调用了onErrorResponse()方法。这里我的状态码是200。但由于响应的格式没有在onResponse()方法上执行。你能帮我处理这件事吗?可能我必须在这里使用CustomRequest。如有任何建议,将不胜感激。谢谢

我使用了截取json对象请求来访问服务器

我的请求格式是:

我的SampleJsonObjTask类是:

public class SampleJsonObjTask {
    public static ProgressDialog progress;
    private static RequestQueue queue;
    JSONObject main;
    JsonObjectRequest req;
    private MainActivity context;
    public SampleJsonObjTask(MainActivity context, JSONObject main) {

        progress = new ProgressDialog(context);
        progress.setMessage("Loading...");
        progress.setCanceledOnTouchOutside(false);
        progress.setCancelable(false);
        progress.show();
        this.context = context;
        this.main = main;
        ResponseTask();
    }

    private void ResponseTask() {
        if (queue == null) {
            queue = Volley.newRequestQueue(context);
        }
        req = new JsonObjectRequest(Request.Method.POST, "", main,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        progress.dismiss();
                        Log.e("response","response--->"+response.toString());

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("error", "error--->" + error.toString());
                NetworkResponse response = error.networkResponse;
                Log.e("statusCode","statusCode--->"+response.statusCode);
                String json = null;
                if (response != null && response.data != null) {
                    switch (response.statusCode) {
                        case 400:
                            break;

                        case 401:
                            json = new String(response.data);
                            break;
                        case 502:
                            json = new String(response.data);
                            break;
                        case 200:
                            json = new String(response.data);
                            break;
                    }
                    progress.dismiss();
                }
            }
        })

        {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Content-Type", "application/json");
                return params;
            }
        };
        req.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 0, 1f));
        queue.add(req);

    }}
如何使用json对象请求获取此字符串响应。

RequestQueue queue=Volley.newRequestQueue(上下文);
RequestQueue queue = Volley.newRequestQueue(context);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        dialog.cancel();
                        Log.d("response", response);
                    }catch (Exception e){
                        volleyResponseCallBack.onFailure("Something went wrong!");
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    String message = null;
                    dialog.cancel();

                    if (volleyError instanceof NetworkError) {
                        message = "Cannot connect to Internet...Please check your connection!";
                    } else if (volleyError instanceof ServerError) {
                        message = "The server could not be found. Please try again after some time!!";
                    } else if (volleyError instanceof AuthFailureError) {
                        message = "Cannot connect to Internet...Please check your connection!";
                    } else if (volleyError instanceof ParseError) {
                        message = "Parsing error! Please try again after some time!!";
                    } else if (volleyError instanceof NoConnectionError) {
                        message = "Cannot connect to Internet...Please check your connection!";
                    } else if (volleyError instanceof TimeoutError) {
                        message = "Connection TimeOut! Please check your internet connection.";
                    }

                    if (!(message == null)) {
                        Log.d("response", message);
                    }
                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Content-Type", "application/json");
            return params;
        }
    };


    stringRequest.setRetryPolicy(new DefaultRetryPolicy(
            0,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    queue.add(stringRequest);
StringRequest StringRequest=新的StringRequest(Request.Method.POST,url, 新的Response.Listener(){ @凌驾 公共void onResponse(字符串响应){ 试一试{ dialog.cancel(); Log.d(“响应”,响应); }捕获(例外e){ 截击回应回撤。失败(“出了点问题!”); } } }, 新的Response.ErrorListener(){ @凌驾 公共错误响应(截击错误截击错误){ 字符串消息=null; dialog.cancel(); if(网络错误的截击错误实例){ message=“无法连接到Internet…请检查您的连接!”; }else if(服务器错误的截击错误实例){ message=“找不到服务器。请稍后再试!!”; }else if(截击错误实例AuthFailureError){ message=“无法连接到Internet…请检查您的连接!”; }else if(截击错误实例解析错误){ message=“解析错误!请稍后再试!!”; }else if(截击错误实例NoConnectionError){ message=“无法连接到Internet…请检查您的连接!”; }else if(截击错误TimeoutError的实例){ message=“连接超时!请检查您的internet连接。”; } 如果(!(消息==null)){ Log.d(“响应”,消息); } } }) { @凌驾 受保护的映射getParams()引发AuthFailureError{ Map params=新的HashMap(); 参数put(“内容类型”、“应用程序/json”); 返回参数; } }; stringRequest.setRetryPolicy(新的DefaultRetryPolicy( 0, DefaultRetryPolicy.DEFAULT\u最大重试次数, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 添加(stringRequest);
我在下面给出了使用JsonObjectRequest-Response处理的可能性。不使用stringRequest?com.android.volley.ParseError:org.json.JSONException:java.lang.String类型的值OK无法转换为JSONObject记录来自服务器的响应,您从服务器得到了什么,因为这是json解析异常,。首先打印response你得到了什么?是的,我已经打印了日志,response是org.json.JSONException:java.lang.String类型的值OK不能转换为JSONObject实际上,当我们调用API并以json格式发送请求时,我们期望响应也会变成json格式。但在这里,后端团队以字符串格式向我发送响应,因此调用了onErrorResponse()方法。这里我的状态码是200。但由于响应的格式没有在onResponse()方法上执行。你能帮我处理这件事吗?可能我必须在这里使用CustomRequest。任何建议都将予以通知。谢谢
com.android.volley.ParseError: org.json.JSONException: Value OK of type java.lang.String cannot be converted to JSONObject
RequestQueue queue = Volley.newRequestQueue(context);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        dialog.cancel();
                        Log.d("response", response);
                    }catch (Exception e){
                        volleyResponseCallBack.onFailure("Something went wrong!");
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    String message = null;
                    dialog.cancel();

                    if (volleyError instanceof NetworkError) {
                        message = "Cannot connect to Internet...Please check your connection!";
                    } else if (volleyError instanceof ServerError) {
                        message = "The server could not be found. Please try again after some time!!";
                    } else if (volleyError instanceof AuthFailureError) {
                        message = "Cannot connect to Internet...Please check your connection!";
                    } else if (volleyError instanceof ParseError) {
                        message = "Parsing error! Please try again after some time!!";
                    } else if (volleyError instanceof NoConnectionError) {
                        message = "Cannot connect to Internet...Please check your connection!";
                    } else if (volleyError instanceof TimeoutError) {
                        message = "Connection TimeOut! Please check your internet connection.";
                    }

                    if (!(message == null)) {
                        Log.d("response", message);
                    }
                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Content-Type", "application/json");
            return params;
        }
    };


    stringRequest.setRetryPolicy(new DefaultRetryPolicy(
            0,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    queue.add(stringRequest);