본문 바로가기

[android] volley post request

by 김홍중 2021. 5. 14.

url에 보내고자하는 ip를 작성합니다.

private void postRequeset() {
        String url = "http://[ip]:3000/info";
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.start();

        JSONObject params = new JSONObject();

        try {
            params.put("name", name);
            params.put("title", title);
            params.put("etc", etc);
        } catch (JSONException e){
            e.printStackTrace();
        }

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, url, params,
                        response -> {
                            Toast.makeText(getApplicationContext(), "msg from server : " + response, Toast.LENGTH_LONG).show();
                        }, error -> {
                            Toast.makeText(getApplicationContext(), "fail : msg from server", Toast.LENGTH_LONG).show();
        });

        requestQueue.add(jsonObjReq);
    }

 

댓글