-1

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

Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71

1 Answers1

0

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.

ivan
  • 1,177
  • 8
  • 23
  • wow, thanks, so when i put the txt.set below requestqueue.add(), it doesn't wait for the response from the server to set text? – Iki Sangadji Jun 22 '19 at 07:02
  • @IkiSangadji Thats the whole point of the requests that the code doesn't waits for it because it can take an undefinite amount of time, and it would have to freeze the whole main thread. – ivan Jun 22 '19 at 23:40