Sander Bakker
Sander Bakker

Reputation: 35

Android Volley can't get value in separate class

public class VolleyStringRequest {
    String url;
    String body;
    String value;
    public VolleyStringRequest(String url, String body){
        this.url = url;
        this.body = body;
        value= "";
    }
    public StringRequest createStringRequest(){
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Do something with the response
                        Log.e("Response", response);
                        try{
                            JSONObject o = new JSONObject(response);
                            JSONArray values=o.getJSONArray("response");
                            value += values.toString();
                        }  catch (JSONException ex){}

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // Handle error
                    }
                }) {
            @Override
            public byte[] getBody() throws AuthFailureError {
                return body.getBytes();
            };
            @Override
            public String getBodyContentType() {
                return "application/json";
            }
        };
        return stringRequest;
    }

    public String getValue() {
        return value;
    }
}

I wrote this code in a seperate class to prevent code repetition but when I run this inside a fragment like this:

RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());
        String url= "http://grwn.ddns.net:1337/results";
        final String body = "{\"id\":1}";
        VolleyStringRequest volleyStringRequest = new VolleyStringRequest(url, body);
        rq.add(volleyStringRequest.createStringRequest());
        volleyStringRequest.getValue();

And call the getValue() method. This method is always empty like: "". Does anyone know how I can enhance my class so this code will work? This issue is not because of a bad link or bad request. I can log the response and that does work (ofcourse inside VolleyStringRequest)

Upvotes: 0

Views: 548

Answers (1)

Maxim Shoustin
Maxim Shoustin

Reputation: 77910

You run:

VolleyStringRequest volleyStringRequest = new VolleyStringRequest(url, body);
rq.add(volleyStringRequest.createStringRequest());
volleyStringRequest.getValue();

But remember createStringRequest is async method and value is populated after some delay a.e. inside public void onResponse(String response)

So when you call volleyStringRequest.getValue(); you get empty string

To make it work you can write some interface as:

  public interface RequestHandlerInterface(){
    void onResponse(String resp);
  }

And pass it to VolleyStringRequest constructor:

    RequestHandlerInterface rh = this;  //Your main class should implement this method
    RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());
    String url= "http://grwn.ddns.net:1337/results";
    final String body = "{\"id\":1}";
    VolleyStringRequest volleyStringRequest = new VolleyStringRequest(url, body, rh);
    rq.add(volleyStringRequest.createStringRequest());

Next, change your VolleyStringRequest:

public class VolleyStringRequest {
    String url;
    String body;
    String value;
    public VolleyStringRequest(String url, String body, RequestHandlerInterface rh){
        this.url = url;
        this.body = body;
        this.rh = rh;
        value= "";
    }
  //...
}

And once you got response from POST, call the callback as:

  @Override
 public void onResponse(String response) {
     // Do something with the response
       Log.e("Response", response);
         try{
          JSONObject o = new JSONObject(response);
          JSONArray values=o.getJSONArray("response");
          value += values.toString();
           if(this.rh != null){
             this.rh.onResponse(value);
           }
          }  catch (JSONException ex){}
}

So in bottom line instead to call volleyStringRequest.getValue();

you have:

@Override
void onResponse(String resp){
     // here you go
 }

that will be called when you get POST response

Upvotes: 1

Related Questions