【问题标题】:Android Json Post shows JSONExepection (End of input at character 0 of)Android Json Post 显示 JSONExepection(字符 0 处的输入结束)
【发布时间】:2020-04-26 20:13:40
【问题描述】:

我尝试将一些 JSON 值发布到我的 mysql 表中,但我不断收到 JSONException(字符 0 处的输入结束)。我已经尝试使用邮递员,它工作正常,但是当我尝试在我的安卓设备中运行它时出现错误这是我的 java 代码

            String url = "//MY url goes here//";
            StringRequest stringRequest = new StringRequest(Request.Method.POST, url, response -> {
            try {
                    JSONObject jsonObject = new JSONObject(response);
                   String success = jsonObject.getString("message");
                    Toast.makeText(getAplicationContext, request_type + " " +success, Toast.LENGTH_SHORT).show();
                    progressBar1.setVisibility(View.INVISIBLE);
                     if (success.equals("Request sent Successfully")) {
                        Toast.makeText(getAplicationContext.this, request_type +" Request Sent Successfully", Toast.LENGTH_SHORT).show();
                        progressBar1.setVisibility(View.INVISIBLE);
                    } else {
                        Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                    }

                } catch (JSONException e) {
                    Toast.makeText(getApplicationContext(), "Exception " + e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }, error -> Toast.makeText(getApplicationContext(), "Error :: " + error.getMessage(), Toast.LENGTH_SHORT).show()) {
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<>();
                    params.put("status", status);
                    params.put("type", type);
                    params.put("req", request);
                    return params;
                }
            };

            RequestQueue requestQueue = Volley.newRequestQueue(this);

            requestQueue.add(stringRequest);

我能够使用邮递员成功地向其发布数据,但它在 android 中抛出 JsonException 错误,所以我认为它必须来自我上面的代码。任何帮助或建议将不胜感激 谢谢

【问题讨论】:

    标签: android json android-volley


    【解决方案1】:

    我建议您查看 API 的邮递员响应并断言它返回了您期望的正确 json 对象。例如。 'message' 键可以是一个对象而不是字符串,即 jsonObject.getJSONObject("message") 另外,考虑使用谷歌的 gson 库来解析带有 POJO 的响应

    例如

          if (callBack == null) {
                    throw new IllegalArgumentException("Callback cannot be null!!!");
                }
                HashMap<String, String> params = new HashMap<>();
                params.put("status", status);
                        params.put("type", type);
                        params.put("req", request);
    
                RequestQueue requestQueue = Volley.newRequestQueue(_context);
    
                JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, URL, new JSONObject(params),
                        new com.android.volley.Response.Listener<JSONObject>() {
    
                            @Override
                            public void onResponse(JSONObject response) {
                                    RespObj retObj = gson.fromJson(response.toString(), RespObj.class);
    callBack.onResponse(retObj)  
                        }, new com.android.volley.Response.ErrorListener() {
    
                    @Override
                    public void onErrorResponse(VolleyError error) {
    
                }) {
    
                    @Override
                    public String getBodyContentType() {
                        return "application/json; charset=utf-8";
                    }
    
                    @Override
                    public Map<String, String> getHeaders() {
                        return headers;
                    }
                };
                jsonObjReq.setRetryPolicy(policy);
                jsonObjReq.setTag(tag);
                requestQueue.add(jsonObjReq);
        /*pojo*/
    public class RespObj{
     private String message;
    public String getMessage(){
     return message;
    }
    /**add this to gradle*/
    implementation 'com.google.code.gson:gson:2.8.5'
    }
    

    【讨论】:

    • 如果它是一个对象,那么应该如何编写代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多