Estou tentando pegar valores de um json
e jogar em um recycleview
usando OkHttp
, ele está pegando os valores e jogando na list, sem erros, porém não estou conseguindo passar esses valores para o adapter, a lista está chegando nula no fragment
, tenho quase certeza que o problema é com a thread
, provavelmente a thread principal está pegando o valor da lista antes da thread do callback
setar...
Como solucionar?
Classe onde pego os valores do json: (creio que o problema é no método sendGetRecipes
)
public class HttpRequest {
private List<Recipes> recipesList;
public void sendGetRecipes(String url) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
call.cancel();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String strResponse = response.body().string();
setRecipesList(strResponse);
Log.d("HTTP", "PASSOU!!!!!");
Log.d("HTTP", strResponse);
}
});
}
public void setRecipesList(String response) {
recipesList = new ArrayList<>();
List<Ingredients> ingredientsList = new ArrayList<>();
List<Steps> stepsList = new ArrayList<>();
if (response != null) {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Recipes recipes = new Recipes();
recipes.setId(jsonObject.getInt("id"));
recipes.setName(jsonObject.getString("name"));
recipes.setImage(jsonObject.getString("image"));
recipes.setServings(jsonObject.getInt("servings"));
JSONArray ingrJsonArray = jsonObject.getJSONArray("ingredients");
for (int j = 0; j < ingrJsonArray.length(); j++) {
JSONObject ingrJsonObject = ingrJsonArray.getJSONObject(j);
Ingredients ingredients = new Ingredients();
ingredients.setIngredient(ingrJsonObject.getString("ingredient"));
ingredients.setMeasure(ingrJsonObject.getString("measure"));
ingredients.setQuantity(ingrJsonObject.getInt("quantity"));
ingredientsList.add(ingredients);
//Log.d("ARRAY-JSON-INGR", ingrJsonObject.getString("ingredient"));
}
JSONArray stepsJsonArray = jsonObject.getJSONArray("steps");
for (int j = 0; j < stepsJsonArray.length(); j++) {
JSONObject stepsJsonObject = stepsJsonArray.getJSONObject(j);
Steps steps = new Steps();
steps.setId(stepsJsonObject.getInt("id"));
steps.setDescription(stepsJsonObject.getString("description"));
steps.setShortDescription(stepsJsonObject.getString("shortDescription"));
steps.setVideoUrl(stepsJsonObject.getString("videoUrl"));
steps.setThumbnailUrl(stepsJsonObject.getString("thumbnailUrl"));
stepsList.add(steps);
//Log.d("ARRAY-JSON-INGR", ingrJsonObject.getString("ingredient"));
}
recipes.setIngredientsList(ingredientsList);
recipes.setStepsList(stepsList);
recipesList.add(recipes);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
public List<Recipes> getRecipesList() {
return recipesList;
}
}
Fragment (comentei no local onde ele tenta pegar a list)
public class MainFragment extends Fragment {
private static final String TAG = MainFragment.class.getSimpleName();
private RecyclerView recipesRecycleView;
private List<Recipes> recipesList;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
recipesRecycleView = (RecyclerView) view.findViewById(R.id.recipesRecycleView);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recipesRecycleView.setLayoutManager(layoutManager);
HttpRequest httpRequest = new HttpRequest();
httpRequest.setRecipesList(Recipes.RECIPES_URL);
recipesList = httpRequest.getRecipesList(); // <------------- AQUI
RecipesAdapter recipesAdapter = new RecipesAdapter(getActivity(), recipesList);
recipesRecycleView.setAdapter(recipesAdapter);
return view;
}
}
Não postei o código do adapter pois o problema não é lá