(Volley)如何处理Post方法后返回JsonObject

时间:2018-08-14 15:47:24

标签: android json post android-volley

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

{     “ operationStatus”: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();

            }

有什么方法可以在凌空抽空方法之后获取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();

                    }

1 个答案:

答案 0 :(得分:0)

尝试:

        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:)