如何发送{数据:[{id:}]请求服务器在使用截击和传递ObjectRequest在体内?

时间:2022-10-22 22:48:30

I want to pass {"data:"[{"id":"12"}]} in volley request body.

我想在截击请求体中传递{"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);


}

please guide me how to pass this things in android with volley , i try so many times but not success.

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

3 个解决方案

#1


0  

Try This

试试这个

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();
}

#2


0  

Try this

试试这个

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();
}

#3


0  

You can use JsonObjectRequest

您可以使用JsonObjectRequest

Here is the example you can try https://*.com/a/28344904/6676466

这里有一个示例,您可以尝试https://*.com/a/28344904/6676466

in this example there is pass your jsonString inside new JSONObject(postParam).

在这个示例中,在新的JSONObject(postParam)中传递jsonString。

#1


0  

Try This

试试这个

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();
}

#2


0  

Try this

试试这个

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();
}

#3


0  

You can use JsonObjectRequest

您可以使用JsonObjectRequest

Here is the example you can try https://*.com/a/28344904/6676466

这里有一个示例,您可以尝试https://*.com/a/28344904/6676466

in this example there is pass your jsonString inside new JSONObject(postParam).

在这个示例中,在新的JSONObject(postParam)中传递jsonString。