java–Volley JSONException:字符0的输入结束

java–Volley JSONException:字符0的输入结束,第1张

概述我见过其他人遇到过这个问题,但没有一个帖子可以帮助我.我正在尝试将Volley用于我的REST调用库,当我尝试使用带有JSON对象的Put调用作为参数时,我收到错误:org.json.JSONException:字符输入结束0的.这是代码:protectedvoidupdateClientDeviceStatus(Activityactivity,finalint @H_403_0@我见过其他人遇到过这个问题,但没有一个帖子可以帮助我.我正在尝试将Volley用于我的REST调用库,当我尝试使用带有JSON对象的Put调用作为参数时,我收到错误:org.Json.JsONException:字符输入结束0的.

@H_403_0@这是代码:

@H_403_0@

protected voID updateClIEntDeviceStatus(Activity activity, final int status) {    JsONObject JsonParams = new JsONObject();    try {        JsonParams.put("statusID", String.valueOf(status));    } catch (JsONException e1) {        e1.printstacktrace();    }    Log.i(LOG_TAG, "Json: " + JsonParams.toString());    String url = Constants.API_URL + "clIEnt/device/" + getdeviceid();    // Request a response from the provIDed URL.    JsonObjectRequest request = new JsonObjectRequest            (Request.Method.PUT, url, JsonParams, new Response.Listener<JsONObject>() {                @OverrIDe                public voID onResponse(JsONObject response) {                    Log.i(LOG_TAG, "updated clIEnt status");                    Log.i(LOG_TAG, "response: " + response.toString());                }            }, new Response.ErrorListener() {                @OverrIDe                public voID one rrorResponse(VolleyError error) {                    Log.i(LOG_TAG, "error with: " + error.getMessage());                    if (error.networkResponse != null)                        Log.i(LOG_TAG, "status code: " + error.networkResponse.statusCode);                }            }) {        @OverrIDe        public Map<String, String> getheaders() throws AuthFailureError {            Map<String, String>  params = new HashMap<String, String>();            params.put("User-Agent", getUserAgent());            params.put("X-BC-API", getKey());            return params;        }        @OverrIDe        public String getbodyContentType() {            return "application/Json";        }    };    request.setRetryPolicy(new DefaultRetryPolicy(20000, 3, DefaultRetryPolicy.DEFAulT_BACKOFF_MulT));    MySingleton.getInstance(activity).addToRequestQueue(request);    }}
@H_403_0@JsonParams日志显示:

@H_403_0@

Json: {"statusID":"1"}
@H_403_0@我还缺少另一种设置吗?看来请求无法解析JsON对象.我甚至尝试创建一个HashMap然后使用它来创建一个JsON对象,但我仍然得到相同的结果.

解决方法:

@H_403_0@我也遇到过这个问题.

@H_403_0@这不一定是因为服务器端存在问题 – 它只是意味着JsonObjectRequest的响应为空.

@H_403_0@很可能服务器应该向您发送内容,而且它的响应为空是一个错误.但是,如果这是服务器的行为方式,那么要解决此问题,您需要更改JsonObjectRequest解析其响应的方式,这意味着创建JsonObjectRequest的子类,并将parseNetworkResponse覆盖到下面的示例.

@H_403_0@

    @OverrIDe    protected Response<JsONObject> parseNetworkResponse(NetworkResponse response) {        try {            String JsonString = new String(response.data,                    httpheaderParser.parseCharset(response.headers, PROTOCol_CHARSET));            JsONObject result = null;            if (JsonString != null && JsonString.length() > 0)                 result = new JsONObject(JsonString);            return Response.success(result,                    httpheaderParser.parseCacheheaders(response));        } catch (UnsupportedEnCodingException e) {            return Response.error(new ParseError(e));        } catch (JsONException je) {            return Response.error(new ParseError(je));        }    } 
@H_403_0@请记住,使用此修复程序,并且在服务器响应为空的情况下,请求回调将返回空引用来代替JsONObject.

总结

以上是内存溢出为你收集整理的java – Volley JSONException:字符0的输入结束全部内容,希望文章能够帮你解决java – Volley JSONException:字符0的输入结束所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1098415.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存