0

我正在使用 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。请提供帮助

4

2 回答 2

1

您上面的网址http://systems.syspearl.com/api正在响应 url not found(404)...可能是您的问题..请检查您的 url 路径并重试...或者如果您的回复为空可能是服务器从不发送响应....所以你可以检查邮递员进行确认

于 2019-09-29T06:52:36.673 回答
0

url http://systems.syspearl.com/api显示错误......我建议首先尝试从文档https://developer.android.com/training/volley/simple发出一个简单的凌空请求

于 2019-09-29T06:57:53.853 回答