How to get string response from php using android volley JsonObjectRequest?

自闭症网瘾萝莉.ら 提交于 2019-12-24 08:50:03

问题


ctually when we call API and send request in JSON format we are expecting response also come into JSON format. But here back end team sending me response in String format therefore my onErrorResponse () method get called. Here my status code is 200. But due to format of response not executed onResponse () method. So will you please help me to handle this? Might be I have to use CustomRequest here. Any suggestoin will be appreciated. Thanks

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

    }

}

Here the Response coming like string format that is Value OK,

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

回答1:


You can use StringRequest for that:

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;
  }
};


来源:https://stackoverflow.com/questions/50344562/how-to-get-string-response-from-php-using-android-volley-jsonobjectrequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!