Search code examples
androidrequest-queueing

My request queue on android studio doesn't work


so i have this requestqueue like this with 'com.android.volley:volley:1.1.1' and permission.INTERNET

    public TextView txt;
    public String text="";
    private static final String 
    url="http://192.168.100.7/diari/tampil_penyakit.php";
    public RequestQueue requestQueue;
    public StringRequest stringRequest;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt=(TextView) findViewById(R.id.txt);
        requestQueue= Volley.newRequestQueue(MainActivity.this);
        stringRequest= new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                    text=response;
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                text="Error";
            }
        });
        requestQueue.add(stringRequest);
        text=text+" empty";
        txt.setText(text);
    }

but the txt only show "empty" and i already tried the url with postman, can somebody help me


Solution

  • Change your onResponse to this:

    stringRequest= new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                        text=response;
                        text=text+" empty";
                        txt.setText(text);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    text="Error";
                }
    

    It wasn't working because what you were basically doing was declaring the variable text like text=""; then setting up your queue, after setting it up setting your textview's text with your empty variable without waiting for a response from the server.