title: Use Json in Fragment (textView) from OkHttp Request in seperate class
body: <p>If the button in <code>DietFragment</code> is pressed, it runs the <code>getJson()</code> method in <code>HttpRequestDietPlan</code>. Afterwards the <code>Json(mealId, title)</code> is used in the <code>DietFragment</code>.</p>
  
  <p><strong>Problem:</strong> <code>DietFragment</code> doesn't wait for the Request to finish.</p>
  
  <p>Future isn't possible because lowest Android version needs to be 4.4</p>
  
  <p><strong>DietFragment</strong></p>
  
  <pre><code>import android.os.Bundle;
  import android.support.annotation.Nullable;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  
  public class DietFragment extends Fragment {
  
      Button button;
      TextView mealOne;
  
      public int mealId;
  
      public DietPlan dietPlan = new DietPlan();
  
      HttpRequestDietPlan hrt = new HttpRequestDietPlan();
  
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_diet, null);
  
          ET = rootView.findViewById(R.id.targetCalories_Input);
          Tv1 = rootView.findViewById(R.id.targetCalories_Output);
          button = rootView.findViewById(R.id.testButton);
          mealOne = rootView.findViewById(R.id.meal1);
  
          button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  hrt.getJson();
  
                  // java.lang.IllegalMonitorStateException: object not locked by thread before wait()
  
                      mealId = hrt.dietPlan.meals.get(0).mealId;
                      title = hrt.dietPlan.meals.get(0).title;
                      mealOne.setText(title);
  
              }
          });
  
          return rootView;
      }
  }
  </code></pre>
  
  <p><strong>HttpRequestDietPlan</strong></p>
  
  <pre><code>import android.os.Build;
  import android.support.annotation.RequiresApi;
  
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import java.io.IOException;
  import java.util.concurrent.CompletableFuture;
  
  import okhttp3.Call;
  import okhttp3.Callback;
  import okhttp3.OkHttpClient;
  import okhttp3.Request;
  import okhttp3.Response;
  
  public class HttpRequestDietPlan {
  
      public DietPlan dietPlan = new DietPlan();
      public final CompletableFuture&lt;Response&gt; future = new CompletableFuture&lt;&gt;();
  
      public void getJson() {
  
          OkHttpClient client = new OkHttpClient();
  
          final Request request = new Request.Builder()
                  .addHeader("X-RapidAPI-Host", "spoonacular-recipe-food-nutrition-v1.p.rapidapi.com")
                  .addHeader("X-RapidAPI-Key", "KEY_KEY_KEY")
                  .url("https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com/recipes/mealplans/generate?timeFrame=day&amp;targetCalories=2000")
                  .build();
  
          client.newCall(request).enqueue(new Callback() {
              @Override
              public void onFailure(Call call, IOException e) {
                  call.cancel();
                  e.printStackTrace();
              }
  
              @Override
              public void onResponse(Call call, final Response response) throws IOException {
                  if (!response.isSuccessful()) {
                      throw new IOException("Unexpected code " + response);
                  } else {
                      try {
                          String jsonData = response.body().string();
                          JSONObject json = new JSONObject(jsonData);
                          JSONArray arrayMeals = json.getJSONArray("meals");
  
                          for (int i = 0; i &lt; arrayMeals.length(); i++) {
                              JSONObject object = arrayMeals.getJSONObject(i);
                              Meal meal = new Meal(
                                      object.getInt("id"),
                                      object.getString("title")
                              );
                              dietPlan.meals.add(meal);
                              System.out.println(meal);
                          }
                          } catch (JSONException e) {
                          e.printStackTrace();
                      }
                  }
              }
          });
      }
  }
  </code></pre>