2

我想在凌空请求正文中传递 {"data:"[{"id":"12"}]} 。

private void reqrej(final String currentLat) {


    String insertData = "http://www.xxxxxxxx.com/makeinforequest.php";
    final StringRequest stringRequest = new StringRequest(Request.Method.POST, insertData, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            Log.i("json", response.toString());

            tv.setText(response.toString());

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

        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {

            //  String mylong = String.valueOf(mCurrentLocation.getLongitude()).toString();

            Map<String, String> params = new HashMap<String, String>();
            params.put("data", "");


           //where I can type request code?

            return checkParams(params);


        }

        private Map<String, String> checkParams(Map<String, String> map) {
            Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, String> pairs = (Map.Entry<String, String>) it.next();
                if (pairs.getValue() == null) {
                    map.put(pairs.getKey(), "");
                }
            }
            return map;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);


}

请指导我如何在 android 中使用 volley 传递这些东西,我尝试了很多次但没有成功。

4

3 回答 3

0

试试这个

try {
RequestQueue requestQueue = Volley.newRequestQueue(this);
 try {
        JSONObject jsonObject=new JSONObject();
        JSONArray jsonArray=new JSONArray();
        JSONObject idJson=new JSONObject();
        idJson.put("id","12");
        jsonArray.put(jsonObject);
        jsonObject.put("data",jsonArray);

    } catch (JSONException e) {
        e.printStackTrace();
    }
final String mRequestBody = jsonObject.toString();

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Log.i("LOG_RESPONSE", response);
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e("LOG_RESPONSE", error.toString());
    }
}) {
    @Override
    public String getBodyContentType() {
        return "application/json; charset=utf-8";
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        try {
            return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
        } catch (UnsupportedEncodingException uee) {
            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
            return null;
        }
    }



    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String responseString = "";
        if (response != null) {
            responseString = String.valueOf(response.statusCode);
        }
        return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
    }
};

requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
于 2018-01-31T13:38:33.923 回答
0

试试这个

try
{
   JSONObject obj = new JSONObject();
   JSONArray jsonArray = new JSONArray();

   obj.put("id", "12");
   jsonArray.put(obj);

   JSONObject jsonObjectdata = new JSONObject();
   jsonObjectdata.put("data", jsonArray);

   Log.i("Json Response",""+ jsonObjectdata.toString());

} catch (JSONException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
}
于 2018-01-31T13:40:52.310 回答
0

您可以使用JsonObjectRequest

这是您可以尝试的示例https://stackoverflow.com/a/28344904/6676466

在这个例子中,里面有你的 jsonString new JSONObject(postParam)

于 2018-01-31T13:44:17.583 回答