android: передача массива значений с одинаковым именем параметра с помощью залпа - PullRequest
0 голосов
/ 08 мая 2019

Я хочу отправить запрос POST, как показано ниже param [] = 1 & param [] = 2 & param [] = 3, используя залп в Android.

1 Ответ

0 голосов
/ 08 мая 2019
try {
  RequestQueue requestQueue = Volley.newRequestQueue(this);
  String URL = "http://";
  JSONObject jsonBody = new JSONObject();
  jsonBody.put("Title", "Mrutunjay");
  jsonBody.put("Author", "Shivaji Sawant");
  final String requestBody = jsonBody.toString();

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

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

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String responseString = "";
        if (response != null) {
            responseString = String.valueOf(response.statusCode);
            // can get more details such as response.headers
        }
        return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
    }
};

requestQueue.add(stringRequest);
} catch (JSONException e) {
    e.printStackTrace();
}
...