I am trying to fetch JSON response from the server for networking I am using Retrofit2 and Rxjava2 to fetch data from the server. Below is my JSON response from the server.
{
"status": "Success",
"panHolderName": "xxxxxx",
"dateOfBirth": "xx-xx-xxxx",
"gender": "xxx",
"residence": null,
"occupation": "xxxx",
"address": "xxxxxxx",
"pincode": "xxxxxx"
}
When I am using API in Postman it is fetching above response properly but when I am using RXJAva2 and retrofit to fetch response it is showing panHolderName
field only even if there is no error showing. Why are other fields' data not showing up?
Below is my code:
RetrofitClient.class
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getInstance(){
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(22, TimeUnit.SECONDS)
.readTimeout(22, TimeUnit.SECONDS)
.writeTimeout(22, TimeUnit.SECONDS)
.build();
if(retrofit == null)
retrofit = new Retrofit.Builder()
.baseUrl("http://68.169.58.46:8080/portal/androidIntegrationAPI/")
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build();
return retrofit;
}
}
ApiService.class
public interface ApiService {
@POST("getPanDetails")
Observable<PanDetails> getDetails(@Body JsonObject jsonObject);
}
PanDetails.class
public class PanDetails {
@SerializedName("status")
@Expose
String status;
@SerializedName("panHolderName")
@Expose
String panHolderName;
@SerializedName("dateOfBirth")
@Expose
String dateOfBirth;
@SerializedName("gender")
@Expose
String gender;
@SerializedName("residence")
@Expose
String residence;
@SerializedName("occupation")
@Expose
String occupation;
@SerializedName("address")
@Expose
String address;
@SerializedName("pincode")
@Expose
String pincode;
public PanDetails(String status, String panHolderName, String dateOfBirth, String gender, String residence,
String occupation, String address, String pincode) {
this.status = status;
this.panHolderName = panHolderName;
this.dateOfBirth = dateOfBirth;
this.gender = gender;
this.residence = residence;
this.occupation = occupation;
this.address = address;
this.pincode = pincode;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getPanHolderName() {
return panHolderName;
}
public void setPanHolderName(String panHolderName) {
this.panHolderName = panHolderName;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getResidence() {
return residence;
}
public void setResidence(String residence) {
this.residence = residence;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPincode() {
return pincode;
}
public void setPincode(String pincode) {
this.pincode = pincode;
}
}
PersonalInfo.class
private void sendPan(String str1){
Retrofit retrofit = RetrofitClient.getInstance();
ApiService apiService = retrofit.create(ApiService.class);
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("panNo",str1);
apiService.getDetails(jsonObject).subscribeOn(Schedulers.io())
.subscribe(new Observer<PanDetails>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(PanDetails value) {
prg.dismiss();
name.setText(value.getPanHolderName());
Log.d("name",nam);
mydob.setText(value.getDateOfBirth());
address.setText(value.getAddress());
pin.setText(value.getPincode());
if(value.getGender().equals("Male")){
gender.setSelection(1);
}
else{
gender.setSelection(2);
}
}
@Override
public void onError(final Throwable e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
prg.dismiss();
TastyToast.makeText(getApplicationContext(),e.getMessage(),
TastyToast.LENGTH_LONG,TastyToast.INFO).show();
}
});
}
@Override
public void onComplete() {
}
});
}
What am I doing wrong?
It is throwing error after
name.setText(value.getPanHolderName());
that's why it is not printing other values. You need to observe on the main thread as you are setting UI data.
Add in build.gradle
implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
implementation "io.reactivex.rxjava2:rxjava:2.1.1"
and
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())