使用Volley Android的多个对象请求

时间:2017-12-07 09:17:02

标签: android android-volley

我想调用API使用Volley。有一个多个对象请求所以我不知道正确的方法。我被尝试编码如下。但它没有给我Response.So可以任何人帮助我???

MainActivity

public void  getJsonResponsePost(){

    JSONObject jsonData = new JSONObject();
    JSONObject json = new JSONObject();

    /*{"data":{"lang_type":"1","keyword":"","latitude":23.022499999999997,"longitude":72.57139833333333,"category":6}}*/
    try {
        jsonData.put("data",json);
        json.put("lang_type","1");
        json.put("keyword","");
        json.put("latitude",23.022499999999997);
        json.put("longitude",72.57139833333333);
        json.put("category",6);
        Log.d("TAG",jsonData.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }


    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, json, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response)
                {
                    Log.d("String Response :",response.toString());
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d(" Error getting  :",error.toString());
        }
    });

    jsonObjectRequest.setTag(REQ_TAG);
    requestQueue.add(jsonObjectRequest);
}

我得到以下回复

String Response :: {"status":"0","message":"Please pass the language type."}

1 个答案:

答案 0 :(得分:1)

你能试试吗?

    try {
    json.put("lang_type","1");
    json.put("keyword","");
    json.put("latitude",23.022499999999997);
    json.put("longitude",72.57139833333333);
    json.put("category",6);
    jsonData.put("data",json);
    Log.d("TAG",jsonData.toString());
} catch (JSONException e) {
    e.printStackTrace();
}


JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonData, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response)
            {
                Log.d("String Response :",response.toString());
            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.d(" Error getting  :",error.toString());
    }
});
相关问题