title: Rapid API Error code 500 with Retrofit2 Android
body: <p>I am using spoonacular API for a recipe app project. The problem occurs when trying to making multiple GET requests to the API. The first request is a simple search with a query parameter. The resulting JSON of the first request contains a Recipe ID and I use that ID to make the second GET request , where the problem occurs.
  The API responds only when I make the request the first time but after that it responds with error code 500 [Internal Server Error].</p>
  
  <p>I have tested the GET request on Postman but there it works fine every time.
  I'm new to working with API's and any help would be immensely appreciated.</p>
  
  <p>This is my Retrofit Service Class</p>
  
  <p>public class ServiceGenerator {</p>
  
  <pre><code>public static final String API_BASE_URL = "https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com/";
  
  private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
  
  private static Retrofit.Builder builder =
          new Retrofit.Builder()
                  .baseUrl(API_BASE_URL)
                  .addConverterFactory(GsonConverterFactory.create());
  
  private static Retrofit retrofit = builder.build();
  
  public static &lt;S&gt; S createService(Class&lt;S&gt; serviceClass, final String HostName, final String KeyVal)
  {
      if (!TextUtils.isEmpty(HostName) &amp;&amp; !TextUtils.isEmpty(KeyVal))
      {
          HeadersInterceptor interceptor = new HeadersInterceptor(HostName,KeyVal);
  
          if (!httpClient.interceptors().contains(interceptor))
          {
              httpClient.addInterceptor(interceptor);
              builder.client(httpClient.build());
              retrofit = builder.build();
          }
      }
  
      return retrofit.create(serviceClass);
  }
  </code></pre>
  
  <p>This is the Interceptor I am using to add Headers with the request.</p>
  
  <p>public class HeadersInterceptor implements Interceptor {</p>
  
  <pre><code>private String HostName,KeyVal;
  
  HeadersInterceptor(final String HostName,final String KeyVal) {
      this.HostName = HostName;
      this.KeyVal = KeyVal;
  }
  
  @NotNull
  @Override
  public Response intercept(@NotNull Chain chain) throws IOException {
      Request original = chain.request();
  
      Request.Builder builder = original.newBuilder()
              .addHeader("X-RapidAPI-Host",HostName)
              .addHeader("X-RapidAPI-Key",KeyVal);
  
      Request request = builder.build();
      return chain.proceed(request);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is my Fragment which makes a search query and SUCCESSFULLY return results[Receipe ID's]</p>
  
  <p>public class ListSelectedFragments extends Fragment {</p>
  
  <pre><code>private ProgressBar PreviewFragPrg;
  private final String TAG = "ListSelectedFragment-&gt;";
  
  private PreviewRecipeAdapter adapter;
  private RecyclerView SelectedItemRV;
  private ArrayList&lt;RecipePreviewHolder&gt; RecipePreviewsList = new ArrayList&lt;&gt;();
  
  public ListSelectedFragments() {
      // Required empty public constructor
  }
  
  
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      final View view = inflater.inflate(R.layout.fragment_list_selected_fragments, container, false);
      SelectedItemRV = view.findViewById(R.id.SelectedItemRV);
      TextView DisplayNameTV = view.findViewById(R.id.DisplayNameTV);
      PreviewFragPrg = view.findViewById(R.id.PreviewFragPrg);
      ImageView BackBtn = view.findViewById(R.id.BackBtn);
  
      BackBtn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              if (getFragmentManager() != null) {
                  getFragmentManager().popBackStackImmediate();
              }
          }
      });
  
      if (getArguments() != null) {
          final String QueryTag = getArguments().getString("QueryTag");
          final String CuisineName = getArguments().getString("CuisineName");
  
          if(CuisineName!=null){
              DisplayNameTV.setText(CuisineName);
          }
          if(QueryTag!=null){
              ProcessQuery(QueryTag);
          }
      }
  
      return view;
  }
  
  private void ProcessQuery(final String QueryStr){
  
      String hostname = getResources().getString(R.string.spoonacular_host_name);
      String key      = getResources().getString(R.string.spoonacular_apikey_val);
  
  
      final ServiceGenerator.GetDataService mService =
              ServiceGenerator.createService(ServiceGenerator.GetDataService.class, hostname,key);
  
      Call&lt;RecipeInfoModel&gt; call = mService.getRecipes(QueryStr);
  
      call.enqueue(new Callback&lt;RecipeInfoModel&gt;() {
          @Override
          public void onResponse(@NonNull Call&lt;RecipeInfoModel&gt; call,
                                 @NonNull Response&lt;RecipeInfoModel&gt; response)
          {
              Log.d(TAG, "Request Response Received");
              Log.d(TAG, response.toString());
  
              if (response.body() != null) {
                  Results[] mRES = response.body().getResults();
                  SetUpRecipePreviews(mRES);
                  PreviewFragPrg.setVisibility(View.GONE);
              }
          }
  
          @Override
          public void onFailure(@NonNull Call&lt;RecipeInfoModel&gt; call, @NonNull Throwable t) {
              Log.d(TAG, "Request Failed");
              Log.d(TAG, call.toString());
              Log.d(TAG, "Throwable -&gt;" + t);
              PreviewFragPrg.setVisibility(View.GONE);
              Toast.makeText(getActivity(),"Could not get required recipes",Toast.LENGTH_SHORT).show();
          }
      });
  
      Log.d(TAG, "User Inputed Request\n"+call.request().url().toString());
  
  }
  
  private void SetUpRecipePreviews(final Results[] mRES) {
  
      RecipePreviewsList.clear();
      adapter = new PreviewRecipeAdapter(getActivity(),RecipePreviewsList);
      SelectedItemRV.setLayoutManager(new GridLayoutManager(getActivity(), 2));
      SelectedItemRV.setAdapter(adapter);
  
      for (Results mRE : mRES) {
          String ImgUrls = mRE.getImage();
          RecipePreviewHolder obj = new RecipePreviewHolder(Integer.valueOf(mRE.getId()),
                  mRE.getTitle(), ImgUrls);
          Log.d("GlideLogs-&gt;","Rid-&gt;"+mRE.getId());
          Log.d("GlideLogs-&gt;","Img URL-&gt;"+ ImgUrls);
          Log.d("GlideLogs-&gt;","Name-&gt;"+mRE.getTitle());
  
          RecipePreviewsList.add(obj);
      }
  
      if(RecipePreviewsList.size()&gt;1){
          adapter.notifyDataSetChanged();
      }
  }
  </code></pre>
  
  <p>This is the Activity I transition to from my Fragment after clicking on a Recipe Card... Sending the Recipe ID in the extras. This function is called immediately after receiving intent extras.</p>
  
  <p>private void RetrieveRecipeInfo(final int recipeID) {</p>
  
  <pre><code>    String hostname = getResources().getString(R.string.spoonacular_host_name);
      String key   = getResources().getString(R.string.spoonacular_apikey_val);
  
  
      final ServiceGenerator.GetDataService mService =
              ServiceGenerator.createService(ServiceGenerator.GetDataService.class, hostname,key);
  
      Call&lt;RecipeDetailedInfo&gt; call = mService.getInformation(185071);
      Log.d(TAG , "Your GET Request:\n"+call.request().url().toString());
  
      call.enqueue(new Callback&lt;RecipeDetailedInfo&gt;() {
          @Override
          public void onResponse(@NonNull Call&lt;RecipeDetailedInfo&gt; call, @NonNull Response&lt;RecipeDetailedInfo&gt; response)
          {
              Log.d(TAG,"OnResponse()  Called\n");
              Log.d(TAG,"Response = "+ response);
  
              if(response.body()!=null) {
                  String obj = response.body().getSourceUrl();
                  Log.d(TAG,"Getting Recipe Info\n");
                  Log.d(TAG, String.valueOf(obj));
              }
          }
  
          @Override
          public void onFailure(@NonNull Call&lt;RecipeDetailedInfo&gt; call, @NonNull Throwable t){
          }
      });
  }
  </code></pre>
  
  <p>Using postman I get the results every time but in my application the API stops responding after the first request. Is there a problem with the way I'm including headers?</p>