【问题标题】:How to get string response from php using android volley JsonObjectRequest?如何使用 android volley JsonObjectRequest 从 php 获取字符串响应?
【发布时间】:2018-10-24 22:54:34
【问题描述】:

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

public class SampleJsonObjTask {
    public static ProgressDialog progress;
    private static RequestQueue queue;
    JSONObject main;
    JsonObjectRequest req;
    private MainActivity context;
    private String prd,us,ver,fha,ve,ves,sz,cat,pa,h,t,en,pha,pur,dip;
    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) {
                progress.dismiss();//error.getMessage()
                /*back end team sending me response in String format therefore my onErrorResponse () method get called. Here my status code is 200.*/
            }

        })

        {
            @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);

    }

}

这里的响应类似于字符串格式的值 OK,

com.android.volley.ParseError: org.json.JSONException: Value OK of type java.lang.String cannot be converted to JSONObject

【问题讨论】:

    标签: android-volley android-json jsonexception jsonobjectrequest


    【解决方案1】:

    您可以为此使用StringRequest

    StringRequest request = new StringRequest(StringRequest.Method.POST, url, new Response.Listener<String>() {
      @Override
      public void onResponse(String response) { }
    }, new Response.ErrorListener() {
      @Override
      public void onErrorResponse(VolleyError error) {
      }
    }) {
      @Override
      public String getBodyContentType() {
        return "application/json; charset=utf-8";
      }
    
      @Override
      public byte[] getBody() {
        try {
          JSONObject jsonObject = new JSONObject();
          /* fill your json here */
          return jsonObject.toString().getBytes("utf-8");
        } catch (Exception e) { }
    
        return null;
      }
    };
    

    【讨论】:

      猜你喜欢
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-03
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      相关资源
      最近更新 更多