java - 使用 http volley 发出 get 请求时响应为空

标签 java android android-volley

我正在使用 volley 从 http url 发送和检索 json 数据,但是在获取响应时我收到一个空的 toast,该 url 是正确的,' http://systems.syspearl.com/api '但我没有收到任何回应。下面是我使用过的代码。

     JSONObject jsonobject = new JSONObject();
     jsonobject.put("amount", "500");

    jsonobject.put("merchant_id", "104");

 jsonobject.put("narrative", "John");
 jsonobject.put("reference", "Steven");
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(
Request.Method.POST,url, jsonobject,
new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        Toast.makeText(Main9Activity.this, response.toString(), Toast.LENGTH_SHORT).show();

    }
}, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(Main9Activity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
    }
}) {
   @Override
  public Map<String, String> getHeaders() throws AuthFailureError {
 Map<String,String >headers=new HashMap<String,String>();
 headers.put(“Content-Type”, “application/json“);
 headers.put(“AuthKey”, Auth);
 return headers;

}

我研究过这个问题,我发现的最接近的是这个https://stackoverflow.com/a/31613565/7871886遗憾的是,在这篇文章中,选择的解决方案是使用字符串请求发送 json,但我无法这样做,因为数据必须以这种精确的格式发送到 url

     {
    “amount”:500,
    “merchant_id”:”104”,
    “narrative”:”John”,
    “reference”:”Steven”
  }

后一篇文章中的其他答案还建议在 http 中添加 s,这不是我要发送到的 url。请提供帮助

最佳答案

您的上述网址http://systems.syspearl.com/api正在响应未找到 url(404)...可能是您的问题..请检查您的 url 路径并重试... 或者,如果您的响应为空,则服务器可能永远不会发送响应......因此您可以检查 postman 进行确认

关于java - 使用 http volley 发出 get 请求时响应为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58149591/

相关文章:

android - 在向 wifiiManager 添加新的 wifiConfiguration 时总是得到 -1

android - Volley 和 AsyncTask

android - 使用 Volley 在 HashMap 中发送 Int

Java:如何创建 Map<String,Object> 对象数组

java - 如何获取 EC2 Windows 实例 AWS 上的用户计数

java - 添加、删除、更新数据时如何刷新 JTable

android - 如何使用 Volley 发送带有 JSON 正文的 POST 请求?

java - 字体渲染中的换行

android - 在 android 中重新加载 Activity 时,alertdialog 消失

java - 获取 java.io.IOException : read failed, 套接字可能已关闭或在 BluetoothSocket.connect() 上超时