I want to call a POST Method(REST API) in Retrofit with a JSON data. Postman and Volley library works well, I want to implement it in Retrofit..
I'm looking into it for last two days, not getting any solution.. I have referred this link and this link and much more which look similar but not working out for me..Might be i'm doing something wrong..
These are my inputs and output look like this
and my coding part lies here
public class Api {
private static Retrofit retrofit = null;
public static ApiInterface getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl("xxxx/Service1.svc/")
.addConverterFactory(GsonConverterFactory.create())
.build();
}
//Creating object for our interface
ApiInterface api = retrofit.create(ApiInterface.class);
return api; // return the APIInterface object
}}
and interface as
public interface ApiInterface {
@FormUrlEncoded
@POST("UserLogin")
Call<SignUpResponse> registration(@Field("Mobile_no") String mobile,
@Field("Password") String pass,
@Field("RegID") String regId);}
and updated Pojo class as
public class SignUpResponse {
@SerializedName("UserLoginResult")
@Expose
private UserLoginResult userLoginResult;
public UserLoginResult getUserLoginResult() {
return userLoginResult;
}
public void setUserLoginResult(UserLoginResult userLoginResult) {
this.userLoginResult = userLoginResult;
}}class UserLoginResult {
@SerializedName("Email_id")
@Expose
private String emailId;
@SerializedName("First_name")
@Expose
private String firstName;
@SerializedName("Last_name")
@Expose
private String lastName;
@SerializedName("Message")
@Expose
private String message;
@SerializedName("Mobile_no")
@Expose
private String mobileNo;
@SerializedName("Password")
@Expose
private String password;
@SerializedName("RegID")
@Expose
private String regID;
@SerializedName("Status")
@Expose
private String status;
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getMobileNo() {
return mobileNo;
}
public void setMobileNo(String mobileNo) {
this.mobileNo = mobileNo;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRegID() {
return regID;
}
public void setRegID(String regID) {
this.regID = regID;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}}
and on MainActivity
Api.getClient().registration("xxxNumberhere","rd","222").enqueue(new Callback<SignUpResponse>() {
@Override
public void onResponse(Call<SignUpResponse> call, Response<SignUpResponse> response) {
signUpResponsesData = response.body();
Toast.makeText(getApplicationContext(), response.body().getUserLoginResult().getMessage(), Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
@Override
public void onFailure(Call<SignUpResponse> call, Throwable t) {
Log.d("response", t.getStackTrace().toString());
progressDialog.dismiss();
}
});
and my dependencies are
compile 'com.squareup.retrofit2:retrofit:2.1.0'
// JSON Parsing
compile 'com.google.code.gson:gson:2.6.1'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
Thanks in advance..