【问题标题】:(Volley) How to handle returning JsonObject after Post Method(Volley) Post 方法后如何处理返回的 JsonObject
【发布时间】:2019-01-21 12:55:41
【问题描述】:

我在 Volley 中使用 Post 方法发送对象。我的 api 返回像邮递员这样的数据,这就是我想要的。

{ “操作状态”:3, “消息”:[ { “消息”:“哈塔!” } ] }

但我试图让我的方法在 onResponse 中处理这些数据。但它只返回 200。

StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("LOG_RESPONSE", response);
                Toast.makeText(DetailAirdrop.this, response, Toast.LENGTH_SHORT).show();

            }

有没有什么方法可以在volley中的post方法之后获取jsonObject?


已解决

解决方案:

JsonObjectRequest postRequest = new JsonObjectRequest( Request.Method.POST, url,
                jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Toast.makeText(DetailAirdrop.this, response.toString(), Toast.LENGTH_SHORT).show();

                    }

【问题讨论】:

  • 你覆盖了 parsenetworkresponse 吗?
  • 请不要将解决方案显示为问题的一部分。如果您的问题已通过其他人发布的答案得到解决,您应该接受此答案。如果您找到了解决问题的方法并希望分享,请写下您自己问题的答案并接受。

标签: android json post android-volley


【解决方案1】:

试试看:

        Volley.newRequestQueue(getActivity()).add(new StringRequest(Request.Method.GET,"LINK", new Response.Listener<String>() {
        @Override
        public void onResponse(String s) {
            JSONObject mJsonObject;

            try {
                mJsonObject = new JSONObject(s);

               Toast.makeText(DetailAirdrop.this, 
               mJsonObject.getString("operationStatus") Toast.LENGTH_SHORT).show();

                JSONArray mJsonArray = mJsonObject.getJSONArray("messages");
                int a = mJsonArray.length();
                for (int i = 0; i < a; i++) {

                    JSONObject jo = mJsonArray.getJSONObject(i);

             Toast.makeText(DetailAirdrop.this, 
           mModel.setID(jo.getString("message"), Toast.LENGTH_SHORT).show();

                }
            } catch (JSONException e) {
                Toast.makeText(getActivity(), "Crash", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }



        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {

        }
    }));

我测试过它会起作用 如果它工作我的回答者TY :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 2016-05-04
    • 2015-11-18
    • 1970-01-01
    相关资源
    最近更新 更多