【问题标题】:Get response from Json Post request从 Json Post 请求中获取响应
【发布时间】:2015-04-18 23:34:02
【问题描述】:

我正在发送带有两个参数 id 和字符串的 POST 请求。

public void sendParams(String id,String stringSave, final VolleyCallback vc) {

    final String URL = "http://maps.b1.finki.ukim.mk/MapController/save";
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("cId", id);
    params.put("positions", stringSave);

    RequestQueue rq=Volley.newRequestQueue(GridTilesActivity.this);
    JsonRequest jr=new JsonRequest(Request.Method.POST,URL,params,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    vc.onSuccess(response);

                    Log.d("Response ",response.toString());
                    System.out.println(response);
                    String string=response.optString("positions");
                    Log.d("Positions",string);
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {

        }
    });

    rq.add(jr);
}

现在点击按钮,我想从这个 POST 中获取响应。

btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                sendParams(mapId,pom,new VolleyCallback() {
                    @Override`enter code here`
                    public void onSuccess(JSONObject result) {
                        Log.d("Result","" + result.toString());
                    }
                });

但我什么也没得到。这段代码有什么问题..?

我在 ErrorResponse 上添加了 Log.d("Error",volleyError.toString()) 并得到了这个:

04-19 23:52:00.375 12825-12825/com.example.bukic.mapyourroom D/Error: com.android.volley.ParseError: org.json.JSONException: End of input at character 0 of

【问题讨论】:

  • 您没有记录错误响应,也许这就是您得到的响应?
  • hm 我添加登录错误响应并得到这个:04-19 23:52:00.375 12825-12825/com.example.bukic.mapyourroom D/Error: com.android.volley.ParseError: org.json.JSONException:在字符 0 处输入结束

标签: java android json android-volley


【解决方案1】:

首先,您为什么不尝试点击按钮上的 Volley Request。然后你可以处理onResponse。希望有帮助吗?

【讨论】: