C
C
Chesterfield252020-10-09 19:04:31
Java
Chesterfield25, 2020-10-09 19:04:31

How to add data from a remote server to a post request?

There is a post request that sends the request body. This request is sent using the Volley library in Android studio! I want the request data, that is, value, to the application to come from the remote server in the same way, so that these values ​​\u200b\u200bcan be changed at any time. Tell me how this can be implemented?

Request body:

params.put("api_key", "value");
params.put("amount", "value");
params.put("to", "value");
params.put("currency", "value");


Request code:

public class MainActivity extends AppCompatActivity {


    private  TextView post_response_data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button post_data = findViewById(R.id.post_data);
        post_response_data = findViewById(R.id.post_response_data);

        post_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                postRequest();
            }
        });

    }

    private void postRequest() {
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        String url = "https://faucetpay.io/api/v1/send";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                post_response_data.setText("Post Data : " + response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                post_response_data.setText("Post Data : Response Failed");
            }
        }){
            @Override
            protected Map<String,String> getParams(){
                Map<String, String>  params = new HashMap<String, String>();
                params.put("api_key", "value");
                params.put("amount", "value");
                params.put("to", "value");
                params.put("currency", "value");

                return params;
            }

            @Override
            public Map<String,String> getHeaders() throws AuthFailureError{
                Map<String, String>  params = new HashMap<String, String>();
                params.put("Content-Type", "application/x-www-form-urlencoded");
                return params;
            }
        };

        requestQueue.add(stringRequest);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2020-10-09
Hasanly @azerphoenix

Hello!

There is a post request that sends the request body. This request is sent using the Volley library in Android studio!

It's not entirely clear what you want to say. Android Studio is a development environment. Or by sending to AS do you mean they are displayed in the editor console?
I want the request data, that is, value, to the application to come from the remote server in the same way, so that these values ​​\u200b\u200bcan be changed at any time

Well, for starters, raise a small REST service on a remote server, which will send you data in json to the application. In fact, you need a backend

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question