title: Retrofit 2 Calls onFailure Method after Posting Parameters to API
body: <p>I have an app that posts several variables to an API and returns responses from the API via <code>Retrofit2</code>. This is actually a payment model where the user's <code>paymentAmount</code> , <code>paymentType</code> ,<code>userID</code> ,<code>billNumber</code> is sent to the API for payment process and responses are received in an <code>AlertDialog</code> to tell whether payment process was successful or not.</p>
  
  <p>The issue here is that the API successfully receives my variables to initialize a payment process however it doesn't return the <code>onResponse</code> method. Rather the <code>onFailure</code> method is called making it unable to receive responses from the API.</p>
  
  <p>This is the log i recieve when i call Throwable <code>t.getCause()</code> from the <code>onFailure</code> method. Please Help.</p>
  
  <pre><code>04-23 20:20:32.368 25592-25592/com.example.gamor.rhema E/ContentValues: Unable to submit post to API.java.net.SocketException: Socket closed
  </code></pre>
  
  <p><strong>API Response Format</strong></p>
  
  <pre><code>{
    "RespCode": "0",
    "PaymentNumber": "PYMNT000000000375",
    "PaymentAmount": 0.5,
    "PaymentDate": "4/23/2018 6:55:09 PM",
    "Respmessage": "Successful"
  }
  </code></pre>
  
  <p><strong>My API Interface - APIServicePayBill.java</strong></p>
  
  <pre><code>public interface APIServicePayBill {
  
   @POST("PayBills")
   @FormUrlEncoded
   Call&lt;PostPayBill&gt; savePostPayBill(@Field("AccountNumber") String AccountNumber,
                                    @Field("Amount") float Amount,
                                    @Field("Paytype") String Paytype,
                                    @Field("BillNumber") String BillNumber);
  }
  </code></pre>
  
  <p><strong>JSON POJO Class - PostPayBill.java</strong></p>
  
  <pre><code>public class PostPayBill {
  
  @SerializedName("RespCode")
  @Expose
  private String respCode;
  @SerializedName("PaymentNumber")
  @Expose
  private String paymentNumber;
  @SerializedName("PaymentAmount")
  @Expose
  private float paymentAmount;
  @SerializedName("PaymentDate")
  @Expose
  private String paymentDate;
  @SerializedName("Respmessage")
  @Expose
  private String respmessage;
  
  public String getRespCode() {
      return respCode;
  }
  
  public void setRespCode(String respCode) {
      this.respCode = respCode;
  }
  
  public String getPaymentNumber() {
      return paymentNumber;
  }
  
  public void setPaymentNumber(String paymentNumber) {
      this.paymentNumber = paymentNumber;
  }
  
  public float getPaymentAmount() {
      return paymentAmount;
  }
  
  public void setPaymentAmount(float paymentAmount) {
      this.paymentAmount = paymentAmount;
  }
  
  public String getPaymentDate() {
      return paymentDate;
  }
  
  public void setPaymentDate(String paymentDate) {
      this.paymentDate = paymentDate;
  }
  
  public String getRespmessage() {
      return respmessage;
  }
  
  public void setRespmessage(String respmessage) {
      this.respmessage = respmessage;
  }
  
  @Override
  public String toString() {
      return "PostPayBill{" +
              "respCode='" + respCode + '\'' +
              ", paymentNumber='" + paymentNumber + '\'' +
              ", paymentAmount=" + paymentAmount +
              ", paymentDate='" + paymentDate + '\'' +
              ", respmessage='" + respmessage + '\'' +
              '}';
  }
  }
  </code></pre>
  
  <p><strong>ApiUtils Class - ApiUtils.java</strong></p>
  
  <pre><code>public class ApiUtils {
  
  private ApiUtils() {}
  
  public static final String BASE_URL = "http://10.1.123.11/api/";
  
  public static APIServicePayBill getUserServicePayBill(){
      return RetrofitClient.getClient(BASE_URL).create(APIServicePayBill.class);
  }
  
  }
  </code></pre>
  
  <p><strong>API Initialising</strong></p>
  
  <pre><code>apiServicePayBill = ApiUtils.getUserServicePayBill();
  </code></pre>
  
  <p><strong>Payment Method</strong></p>
  
  <pre><code>private void sentPayBillsDetails(String AccountNumber, float Amount, String Paytype, String BillNumber ) {
      apiServicePayBill.savePostPayBill(AccountNumber,Amount,Paytype,BillNumber).enqueue(new Callback&lt;PostPayBill&gt;() {
          @Override
          public void onResponse(Call&lt;PostPayBill&gt; call, Response&lt;PostPayBill&gt; response) {
              if (response.isSuccessful()){
                  Log.i(TAG, "post submitted to API." + response.body().toString());
  
                  if ( progressDialog!=null &amp;&amp; progressDialog.isShowing() ){
                      progressDialog.dismiss();
                  }
  
                  AlertDialog.Builder registrationAlert = new AlertDialog.Builder(MakePaymentActivity.this);
                  registrationAlert.setTitle(Html.fromHtml("&lt;font color='#125688'&gt;PAYMENT STATUS&lt;/font&gt;"));
                  registrationAlert.setIcon(R.drawable.ic_check_box);
                  registrationAlert.setMessage("Payment Number: "+response.body().getPaymentNumber()+
                                              "\nPayment Amount: "+response.body().getPaymentAmount()+
                                              "\nPayment Date: "+response.body().getPaymentDate()+
                                              "\nMessage: "+response.body().getRespmessage());
                  registrationAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialogInterface, int i) {
  
                          gotoRegistrationPage();
                      }
                  });
  
                  registrationAlert.setCancelable(false);
  
                  AlertDialog alertDialog = registrationAlert.create();
                  alertDialog.show();
              }
  
          }
  
          @Override
          public void onFailure(Call&lt;PostPayBill&gt; call, Throwable t) {
              Log.e(TAG, "Unable to submit post to API."+t.getCause());
  
              if ( progressDialog!=null &amp;&amp; progressDialog.isShowing() ){
                  progressDialog.dismiss();
              }
              AlertDialog.Builder registrationAlert = new AlertDialog.Builder(MakePaymentActivity.this);
              registrationAlert.setTitle(Html.fromHtml("&lt;font color='#125688'&gt;PAYMENT STATUS&lt;/font&gt;"));
              registrationAlert.setIcon(R.drawable.ic_check_box);
              registrationAlert.setMessage("Unable to submit post to API");
              registrationAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialogInterface, int i) {
  
                      gotoFaliedPage();
                  }
              });
  
              registrationAlert.setCancelable(false);
  
              AlertDialog alertDialog = registrationAlert.create();
              alertDialog.show();
  
          }
      });
  }
  </code></pre>
  
  <p><strong>Gradle File</strong></p>
  
  <pre><code>  // Retrofit
  implementation 'com.squareup.retrofit2:retrofit:2.4.0'
  
  // JSON Parsing
  implementation 'com.google.code.gson:gson:2.6.1'
  implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
  </code></pre>