I have a JSONObjectRequest that wont get called everytime. So there will be no response. I also have another JSONObjectRequest that will always be called. Sometimes i change the code in the MainActivity and it will be called again and then not. The backend works just fine. Currently this is how the method looks like:
public void getPictureOfAllIngredients( final GetAllResponseListener callback){
RequestQueue queue = Volley.newRequestQueue(context);
String url ="";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
String status = response.getString("status");
String message = response.getString("message");
if(status.equals("success")){
callback.onResponse(response.toString());
}else{
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, error.toString(), Toast.LENGTH_SHORT).show();
}
});
queue.add(jsonObjectRequest);
}
This is how the method looks that calls it:
public void initDataIngredients() throws IOException {
data = (ArrayList<IngredientEntity>) ObjectSerializer.deserialize(sharedPreferences.getString("ingredients", null));
if( data == null || data.size() == 0
){
ingredientService.getPictureOfAllIngredients(new IngredientService.GetAllResponseListener() {
@Override
public void onError(String Message) {
String x = Message;
}
@Override
public void onResponse(String Message) {
mapper= new RecipeMapper();
data = mapper.parseIngredientDatabaseEntityToRegularEntity(Message);
try {
editor.putString("ingredients", ObjectSerializer.serialize(data)).apply();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
data = (ArrayList<IngredientEntity>) ObjectSerializer.deserialize(sharedPreferences.getString("ingredients", null));
}
}
And this is how the method looks that always gets called:
public void getAllRecipes( final GetAllResponseListener callback){
RequestQueue queue = Volley.newRequestQueue(context);
String url ="";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
String status = response.getString("status");
String message = response.getString("message");
if(status.equals("success")){
callback.onResponse(response.toString());
}else{
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, error.toString(), Toast.LENGTH_SHORT).show();
}
});
queue.add(jsonObjectRequest);
}
And this is how the method looks that calls the working request:
public void initDataRecipes(){
ArrayList<RecipeEntity> array = new ArrayList<RecipeEntity>();
//if(sharedPreferences.getString("recipes",null) == null) {
ingredientService.getAllRecipes(new IngredientService.GetAllResponseListener() {
@Override
public void onError(String Message) {
}
@Override
public void onResponse(String Message) {
mapper= new RecipeMapper();
recipes = mapper.parseRecipeDatabaseEntityToRegularEntity(Message);
try {
editor.putString("ingredients", ObjectSerializer.serialize(data)).apply();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
J just cant figure out why one gets called an the other one not. I also looked up in the debugger where it goes and it just jumps the responses both. Any help will be much appreciated.
EDIT
I figured out the service gets only called when the onCreate method is called two times. So when i navigate out of the main page and navigate back into it, the service will be called. Any guesses??
I fixed it by myself.
The flaw was the async behavior of Volley. I set it by creating a new Intent and recalling the main activity
onResponse().