如何将两个参数传递给android retrofit2的api,以检查它们是否存在于远程服务器

时间:2018-04-16 15:08:17

标签: retrofit2

  

使用api网址http://192.168.0.19:2000/api/userprofile?phone=0722931069&Password=1111

的Json响应
{
  "Username":"abel",
  "ProfileId":"746737",
  "Password":"1111",
  "SaccaUssdId":"3728282",
  "PaybillNo":"74883",
  "Phonenumber":"0722931069",
  "CustomerType":"d",
  "RegistrationDate":"2005-12-09T00:00:00",
  "TimeStamp":"2005-12-09T00:00:00",
  "PreferredModeOfComm":"phonecall",
  "APIURL":"hhh",
  "APIUsername":"kplDL",
  "APIPassword":"JKSDjic"
  }
  

这是我的POJO课程,PLease帮助理清这个问题

public class Fetchnumber {
private String Username;
private String ProfileId;
private String Password;
private String SaccaUssdId;

private String PaybillNo;
private  String Phonenumber;
private  String CustomerType;
private String RegistrationDate;
private  String TimeStamp;
private String PreferredModeOfComm;
private String APIURL;
private String APIUsername;
private String APIPassword;
public Fetchnumber(String username, String profileId, String password, String saccaUssdId, String paybillNo, String phonenumber, String customerType, String registrationDate, String timeStamp, String preferredModeOfComm, String APIURL, String APIUsername, String APIPassword) {
  this.Username = username;
    this.ProfileId = profileId;
    this.Password = password;
    this.SaccaUssdId = saccaUssdId;
    this.PaybillNo = paybillNo;
    this.Phonenumber = phonenumber;
    this.CustomerType = customerType;
    this.RegistrationDate = registrationDate;
    this.TimeStamp = timeStamp;
    this.PreferredModeOfComm = preferredModeOfComm;
    this.APIURL = APIURL;
    this.APIUsername = APIUsername;
    this.APIPassword = APIPassword;

}

public String getUsername() {
    return Username;
}

public String getProfileId() {
    return ProfileId;
}

public String getPassword() {
    return Password;
}

public String getSaccaUssdId() {
    return SaccaUssdId;
}

public String getPaybillNo() {
    return PaybillNo;
}

public String getPhonenumber() {
    return Phonenumber;
}

public String getCustomerType() {
    return CustomerType;
}

public String getRegistrationDate() {
    return RegistrationDate;
}

public String getTimeStamp() {
    return TimeStamp;
}

public String getPreferredModeOfComm() {
    return PreferredModeOfComm;
}

public String getAPIURL() {
    return APIURL;
}

public String getAPIUsername() {
    return APIUsername;
}

public String getAPIPassword() {
    return APIPassword;
}

}

`请帮助我,我正在使用retrofit2库和Pinview和手机号来验证用户身份,但是一旦我完成了pin键,该应用就会收听pin键。 这是我如何声明基本URL和方法,它需要查询两个参数

String BASE_URL="http://192.168.0.19:2000/";
@GET("api/userprofile")

Call<Fetchnumber> getNumbers(@Query("phone" ) String phone, @Query("Password") String Password);

这是我在Logcat中得到的错误

  

java.lang.NullPointerException:尝试调用虚方法&#39; java.lang.String e.uzer.msacco.Fetchnumber.getPassword()&#39;在null对象引用上                                                                      在e.uzer.msacco.UserLogin $ 1 $ 1.onDataEntered(UserLogin.java:49)                                                                      在com.goodiebag.pinview.Pinview.onTextChanged(Pinview.java:406)                                                                      在android.widget.TextView.sendOnTextChanged(TextView.java:8320)                                                                      在android.widget.TextView.handleTextChanged(TextView.java:8385)                                                                      在android.widget.TextView $ ChangeWatcher.onTextChanged(TextView.java:10531)

这是改造库,它显示了我如何将两个参数传递给方法getnumbers

Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
Api api = retrofit.create(Api.class);
Call<Fetchnumber> call = api.getNumbers(phonenumbersubmission.phone_number.getText().toString(),pinview.getValue().toString().trim());
call.enqueue(new Callback<Fetchnumber>() {

以下是我处理Onresponse的方式

 public void onResponse(Call<Fetchnumber> call, final Response<Fetchnumber> response) {

            pinview.setPinViewEventListener(new Pinview.PinViewEventListener() {
                @Override
                public void onDataEntered(Pinview pinview, boolean b) {

                        if ((response.body().getPassword().equals(pinview.getValue().toString())&&( response.body().getPhonenumber().toString().equals(phonenumbersubmission.phone_number.toString())))) {

                            Toast.makeText(UserLogin.this, "Login successful", Toast.LENGTH_LONG).show();
                            Intent intent = new Intent(UserLogin.this, enrollment.class);
                            startActivity(intent);

                        } else {
                            Toast.makeText(UserLogin.this, "Wrong PIN", Toast.LENGTH_LONG).show();

getPassword getPhonenumber()来自POJO类。

非常感谢任何帮助,

感谢andvance。

1 个答案:

答案 0 :(得分:0)

@SerializedName注释添加到pojo类中的所有字段,并使用json响应中的相应键名称。它应该像

    public class Example {

@SerializedName("Username")
@Expose
private String username;
@SerializedName("ProfileId")
@Expose
private String profileId;
@SerializedName("Password")
@Expose
private String password;
@SerializedName("SaccaUssdId")
@Expose
private String saccaUssdId;
@SerializedName("PaybillNo")
@Expose
private String paybillNo;
@SerializedName("Phonenumber")
@Expose
private String phonenumber;
@SerializedName("CustomerType")
@Expose
private String customerType;
@SerializedName("RegistrationDate")
@Expose
private String registrationDate;
@SerializedName("TimeStamp")
@Expose
private String timeStamp;
@SerializedName("PreferredModeOfComm")
@Expose
private String preferredModeOfComm;
@SerializedName("APIURL")
@Expose
private String aPIURL;
@SerializedName("APIUsername")
@Expose
private String aPIUsername;
@SerializedName("APIPassword")
@Expose
private String aPIPassword;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getProfileId() {
return profileId;
}

public void setProfileId(String profileId) {
this.profileId = profileId;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getSaccaUssdId() {
return saccaUssdId;
}

public void setSaccaUssdId(String saccaUssdId) {
this.saccaUssdId = saccaUssdId;
}

public String getPaybillNo() {
return paybillNo;
}

public void setPaybillNo(String paybillNo) {
this.paybillNo = paybillNo;
}

public String getPhonenumber() {
return phonenumber;
}

public void setPhonenumber(String phonenumber) {
this.phonenumber = phonenumber;
}

public String getCustomerType() {
return customerType;
}

public void setCustomerType(String customerType) {
this.customerType = customerType;
}

public String getRegistrationDate() {
return registrationDate;
}

public void setRegistrationDate(String registrationDate) {
this.registrationDate = registrationDate;
}

public String getTimeStamp() {
return timeStamp;
}

public void setTimeStamp(String timeStamp) {
this.timeStamp = timeStamp;
}

public String getPreferredModeOfComm() {
return preferredModeOfComm;
}

public void setPreferredModeOfComm(String preferredModeOfComm) {
this.preferredModeOfComm = preferredModeOfComm;
}

public String getAPIURL() {
return aPIURL;
}

public void setAPIURL(String aPIURL) {
this.aPIURL = aPIURL;
}

public String getAPIUsername() {
return aPIUsername;
}

public void setAPIUsername(String aPIUsername) {
this.aPIUsername = aPIUsername;
}

public String getAPIPassword() {
return aPIPassword;
}

public void setAPIPassword(String aPIPassword) {
this.aPIPassword = aPIPassword;
}

}