Deepak nigam
Deepak nigam

Reputation: 119

Not getting data in onResponse in volley

I am not getting data in response in volley. In my activity I have created method to get List.

PackageActivity.java

 private List<Package> preparePackageData() {
    MakeNetworkRequest data = new MakeNetworkRequest(PackageActivity.this);
    packageList = data.getAllPackage();
    return packageList;
}

Below is my MakeNetworkRequest.java

package com.lab.demo5;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.util.List;


public class MakeNetworkRequest{
    private static final String JSON_URL = "http://localhost/android/package.php";
    String response;
    Context context;
    private List<Package> packageList=null;

    MakeNetworkRequest(Context ctx)
    {
        this.context=ctx;
    }

    protected List<Package> getAllPackage()
    {
        Log.d("getAllPackage","getAllPackage");
        sendRequest();
        return this.packageList;
    }

    public void sendRequest(){

       StringRequest stringRequest = new StringRequest(JSON_URL, new Response.Listener<String>() {
           @Override
           public void onResponse(String response) {
               Log.d("onResponse","onResponse");
               showJSON(response);
           }
       }, new Response.ErrorListener() {
           @Override
           public void onErrorResponse(VolleyError error) {
               Toast.makeText(context,error.toString(),Toast.LENGTH_LONG).show();
           }
       });

        RequestQueue requestQueue = Volley.newRequestQueue(context);
        requestQueue.add(stringRequest);
    }

    private void showJSON(String json){
        Log.d("showJSON","onResponse");
        ParseJSON pj = new ParseJSON(json);
        pj.parseResult();
        this.packageList = pj.packageList;

    }
}

I am getting "getAllPackage" in log but it is not show any data in log "onResponse".

Whereas the same code give me good result when I am placing this code in directly in Activity.

Upvotes: 0

Views: 528

Answers (1)

Damodar Dahal
Damodar Dahal

Reputation: 569

I think this error is caused by returning this.packageList from getAllPackages(). It returns null and you later changed the reference of this.packageList in showJson().

Also, I suggest that you should implement callbacks instead of this. Like this:

protected void getAllPackage(@NonNull ResultListener listener);

and then calling back when on onRespose() is called, like listener.onSuccess(data).

Also do note that you need to create the SuccessListener interface yourself. Something like this:

public interface ResultListener{
  onSuccess(List<Package> packages);
  onError(Exception exception);
}

By doing this, you can use this in your activity:

new MakeNetworkRequest(this).getAllPackages(new MakeNetworkRequest.ResultListener(){
  @Override 
  onSuccess(List<Package> packages){

  }
  @Override
  onError(Exception error){

  }
});

I have created a gist here.

Upvotes: 1

Related Questions