M.Hamdi
M.Hamdi

Reputation: 15

Getting volley response in another class

I made the first request successfully and I like to access my Volley response in other class , how can I do that because when I try to do that it return a null response

Upvotes: 0

Views: 726

Answers (2)

Rashed Zzaman
Rashed Zzaman

Reputation: 123

At first its depends on your json data formate .......

  1. You need to create a class
  2. Declare a method in your class with a parameter string or array and call this method where you get json response

or follow this code

public class MyJsonParser {
public  static  Vector<MyModel> getJsonResponse(Context context,String response)
throws JSONException, IOException
{

final JSONObject stringResponse = new JSONObject(response);
final JSONObject responseHolder = stringResponse.getJSONObject("data"); 
  final JSONArray jsonArray = responseHolder .getJSONArray("arrayData");

MyModel myModel;
Vector<MyModel> myArray = new Vector<MyModel>();
for (int i= 0;i<jsonArray .length();i++){
    myModel= new MyModel();
    JSONObject propertyHolder = jsonArray .getJSONObject(i);
    myModel.setId(propertyHolder,"id");
    myModel.setName(propertyHolder, "name");
    myArray .addElement(myModel);
    myModel= null;
}
return myArray ;
}


   }

and call this method where you get json response like this....

  MyJsonParser.getJsonResponse(context,response);

Upvotes: 0

H.T
H.T

Reputation: 161

use interface and callback

public interface RestCallBack {

    void onStart(String action);

    void onComplete(String response, String action,Exception e);
}

And in your onResponse

 listener.onComplete(response, action, null);

Then you can implement this interface in any class where you want the response.

Upvotes: 2

Related Questions