且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Android Volley JsonObjectRequest从php获取字符串响应?

更新时间:2023-11-07 08:55:16

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