如何使用Android Volley JsonObjectRequest从php获取字符串响应? [英] How to get string response from php using android volley JsonObjectRequest?

查看:324
本文介绍了如何使用Android Volley JsonObjectRequest从php获取字符串响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们调用API并以JSON格式发送请求时,我们期望响应也将变为JSON格式. 但是在这里,后端团队以String格式向我发送了响应,因此调用了我的onErrorResponse()方法.这里我的状态码是200.但是由于响应格式未执行onResponse()方法.所以,请您帮我解决一下吗?可能是我必须在这里使用CustomRequest.任何建议都将不胜感激.谢谢

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

    }

}

响应类似于字符串格式,即值确定",

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

推荐答案

您可以为此使用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;
  }
};

这篇关于如何使用Android Volley JsonObjectRequest从php获取字符串响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆