title: how to check if Retrofit API call was successful in Android
body: <p>here is my code:<br>
  by this method, I send some data to my API server<br>
  if data received successfully <code>success</code> variable will be true inside <code>onResponse</code> method</p>
  
  <pre><code>private boolean success=false;
  
  public boolean commit(){
  
          requestHandler.insertFields(tableName,values).enqueue(new Callback&lt;DatabaseModel&gt;() {
              @Override
              public void onResponse(Call&lt;DatabaseModel&gt; call,
                                     Response&lt;DatabaseModel&gt; response) {
                  if (response.isSuccessful())
                  Log.i("debug8","result is:"+response.body().toString());
                  else
                      Log.e("debug8","error in server response:"+response.toString());
  
  
                  success=true;
  
              }
  
              @Override
              public void onFailure(Call&lt;DatabaseModel&gt; call, Throwable t) {
                  Log.e("tempo",t.getMessage());
              }
          });
  
          return success;//returns false before finishing the request
  }
  </code></pre>
  
  <p>and in my activity i want to check the result like this:</p>
  
  <pre><code>        Database db=new Database("cars");
      db.put("field1","val1")
              .put("field2","val2");
  
      if(db.commit())
          Toast.makeText(this, "data inserted successfully", Toast.LENGTH_SHORT).show();
      else
          Toast.makeText(this, "failed to insert new record", Toast.LENGTH_SHORT).show();
  </code></pre>
  
  <p>the problem is that always the commit method result is false because code returns false before retrofit enqueue method finishes its job</p>
  
  <p>is it a good idea to make a custom listener or something like that to do this?<br>
  thanks in advance</p>