我有一个应用程序会将多个变量发布到API,并通过Retrofit2
从API返回响应。这实际上是一种付款模式,用户的paymentAmount
,paymentType
,userID
,billNumber
会发送到API进行付款,并在AlertDialog
中收到回复判断付款流程是否成功。
此处的问题是API成功接收我的变量以初始化付款流程,但它不会返回onResponse
方法。而是调用onFailure
方法,使其无法从API接收响应。
当我从t.getCause()
方法调用Throwable onFailure
时,这是我收到的日志。请帮助。
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
API响应格式
{
"RespCode": "0",
"PaymentNumber": "PYMNT000000000375",
"PaymentAmount": 0.5,
"PaymentDate": "4/23/2018 6:55:09 PM",
"Respmessage": "Successful"
}
我的API接口 - APIServicePayBill.java
public interface APIServicePayBill {
@POST("PayBills")
@FormUrlEncoded
Call<PostPayBill> savePostPayBill(@Field("AccountNumber") String AccountNumber,
@Field("Amount") float Amount,
@Field("Paytype") String Paytype,
@Field("BillNumber") String BillNumber);
}
JSON POJO类 - PostPayBill.java
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 + '\'' +
'}';
}
}
ApiUtils类 - ApiUtils.java
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);
}
}
API初始化
apiServicePayBill = ApiUtils.getUserServicePayBill();
付款方式
private void sentPayBillsDetails(String AccountNumber, float Amount, String Paytype, String BillNumber ) {
apiServicePayBill.savePostPayBill(AccountNumber,Amount,Paytype,BillNumber).enqueue(new Callback<PostPayBill>() {
@Override
public void onResponse(Call<PostPayBill> call, Response<PostPayBill> response) {
if (response.isSuccessful()){
Log.i(TAG, "post submitted to API." + response.body().toString());
if ( progressDialog!=null && progressDialog.isShowing() ){
progressDialog.dismiss();
}
AlertDialog.Builder registrationAlert = new AlertDialog.Builder(MakePaymentActivity.this);
registrationAlert.setTitle(Html.fromHtml("<font color='#125688'>PAYMENT STATUS</font>"));
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<PostPayBill> call, Throwable t) {
Log.e(TAG, "Unable to submit post to API."+t.getCause());
if ( progressDialog!=null && progressDialog.isShowing() ){
progressDialog.dismiss();
}
AlertDialog.Builder registrationAlert = new AlertDialog.Builder(MakePaymentActivity.this);
registrationAlert.setTitle(Html.fromHtml("<font color='#125688'>PAYMENT STATUS</font>"));
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();
}
});
}
Gradle文件
// 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'