I got a quite weird problem, forgive me if it became an attention problem, but I'm running on coffee right now!
I have this Json:
[
{
"address":"RS 239, Km 18,2 nÂș 4631 - Novo Hamburgo",
"closingTime":"06:00",
"description":"Curta como quiser.",
"distance":"6,328.35 km",
"iconUrl":"~\/Images\/Establishment\/Bar Alternativo.png",
"idEstablishment":5,
"name":"Bar Alternativo",
"openingTime":"22:30",
"phone":"(51) 3778-1820",
"type":"Casa Noturna \/ Balada"
}
]
And when I try deserialize this one using this code:
public static ArrayList<Establishment> serializeEstablishmentList(String json) {
ObjectMapper mapper = new ObjectMapper();
ArrayList<Establishment> establishments = null;
try {
establishments = mapper.readValue(json, new TypeReference<List<Establishment>>(){});
} catch (IOException e) {
e.printStackTrace();
}
return establishments;
}
My distance property do not get its value, image from debugger:
here goes my Establishment class:
public class Establishment {
private long idEstablishment;
private Drawable icon;
private String iconUrl;
private String name;
private String type;
private boolean workingStatus;
private String openingTime;
private String closingTime;
private String distance;
private String phone;
private String description;
private String address;
public Establishment() {
}
public Establishment(long idEstablishment, Drawable icon, String iconUrl, String name, String type, boolean workingStatus, String openingTime, String closingTime, String distance, String phone, String description, String address) {
this.idEstablishment = idEstablishment;
this.icon = icon;
this.iconUrl = iconUrl;
this.name = name;
this.type = type;
this.workingStatus = workingStatus;
this.openingTime = openingTime;
this.closingTime = closingTime;
this.distance = distance;
this.phone = phone;
this.description = description;
this.address = address;
}
public Establishment(long id, Drawable icon, String name, boolean workingStatus, String openingTime,
String closingTime, String distance) {
this.idEstablishment = id;
this.icon = icon;
this.name = name;
this.workingStatus = workingStatus;
this.openingTime = openingTime;
this.closingTime = closingTime;
this.distance = distance;
}
public long getIdEstablishment() {
return idEstablishment;
}
public void setIdEstablishment(long idEstablishment) {
this.idEstablishment = idEstablishment;
}
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean getWorkingStatus() {
return workingStatus;
}
public String getWorkingStatusLabel(){
return workingStatus ? "Aberto" : "Fechado";
}
public void setWorkingStatus(boolean workingStatus) {
this.workingStatus = workingStatus;
}
public String getOpeningTime() {
return openingTime;
}
public void setOpeningTime(String openingTime) {
this.openingTime = openingTime;
}
public String getClosingTime() {
return closingTime;
}
public void setClosingTime(String closingTime) {
this.closingTime = closingTime;
}
public String getDistance() {
return distance;
}
public void setDistance(String distance) {
this.distance = this.distance;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isWorkingStatus() {
return workingStatus;
}
public String getIconUrl() {
return iconUrl;
}
public void setIconUrl(String iconUrl) {
this.iconUrl = iconUrl;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Any idea?
Your setter method is wrong:
public void setDistance(String distance) {
this.distance = this.distance;
}
This should be:
public void setDistance(String distance) {
this.distance = distance;
}
Related
I'm creating an app in Android Studio, which connects to a Cloud Firestore database. In the database I have the following structure:
package com.example.gym.models;
public class PopularModel {
String name;
String description;
String rating;
String discount;
String type;
String img_url;
public PopularModel() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getRating() {
return rating;
}
public void setRating(String rating) {
this.rating = rating;
}
public String getDiscount() {
return discount;
}
public void setDiscount(String discount) {
this.discount = discount;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getImg_url() {
return img_url;
}
public void setImg_url(String img_url) {
this.img_url = img_url;
}
}
When i run the program, it says No setter/field for type found on class com.example.gym.models.PopularModel. Can someone help me please?
Have you tried adding a parameterized constructor ?
package com.example.gym.models;
public class PopularModel {
String name;
String description;
String rating;
String discount;
String type;
String img_url;
public PopularModel() {
}
public PopularModel(String name, String description, String rating, String discount, String type, String img_url) {
this.name = name;
this.description = description;
this.rating = rating;
this.discount= discount;
this.type = type;
this.img_url = img_url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getRating() {
return rating;
}
public void setRating(String rating) {
this.rating = rating;
}
public String getDiscount() {
return discount;
}
public void setDiscount(String discount) {
this.discount = discount;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getImg_url() {
return img_url;
}
public void setImg_url(String img_url) {
this.img_url = img_url;
}
}
I am trying to making a POST request using retrofit2 but I am getting ErrorCode: 401. I am not finding the proper solution on the web that can help me out to solve my problem.
Data load for POST request
InsertAcitvity.java
private void insertData() {
String name = mName.getText().toString().trim();
String email = mEmail.getText().toString().trim();
String phone = mPhone.getText().toString().trim();
String full_address = mFull_address.getText().toString().trim();
String name_of_university = mName_of_university.getText().toString().trim();
int graduation_year = Integer.parseInt(mGraduation_year.getText().toString().trim());
double cgpa = Double.parseDouble(mCgpa.getText().toString().trim()) ;
String experience_in_months = mExperience_in_months.getText().toString().trim();
String current_work_place_name = mCurrent_work_place_name.getText().toString().trim();
String applying_in = mApplying_in.getText().toString().trim();
int expected_salary = Integer.parseInt(mExpected_salary.getText().toString().trim()) ;
String reference = mFeference.getText().toString().trim();
String github_project_url = mGithub_project_url.getText().toString().trim();
long creationTime= System.currentTimeMillis();
long updateTime= System.currentTimeMillis();
//String token = LoginActivity.token;
Intent intent = getIntent();
// declare token as member variable
String token = intent.getStringExtra("TOKEN_STRING");
String CvUniqueId = UUID.randomUUID().toString();
String tsync_id = UUID.randomUUID().toString();
cvFileObject = new CvFileObject(CvUniqueId);
mainObject = new MainObjectClass(tsync_id, name, email, phone, full_address, name_of_university, graduation_year, cgpa, experience_in_months,
current_work_place_name, applying_in, expected_salary,field_buzz_reference, github_project_url, cvFileObject, creationTime, updateTime);
ClientMethod clientMethod = ApiClient.getClient().create(ClientMethod.class);
Call<MainResponseObjectClass> call = clientMethod.getValue(token,mainObject);
call.enqueue(new Callback<MainResponseObjectClass>() {
#Override
public void onResponse(#NonNull Call<MainResponseObjectClass> call, #NonNull Response<MainResponseObjectClass> response) {
if (response.isSuccessful()){
Toast.makeText(InsertActivity.this, response.body().getTsync_id(), Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(InsertActivity.this, "access denied:(",Toast.LENGTH_LONG).show();
Log.d("ErrorCode", "onResponse: " + response.code());
}
}
#Override
public void onFailure(#NonNull Call<MainResponseObjectClass> call, #NonNull Throwable t) {
Log.d("Error", "onFailure: " + t.getMessage());
}
});
}
ClientMethod.java
public interface ClientMethod {
#POST("api/login/")
Call<Token>login(#Body Login login);
#POST("/api/v0/recruiting-entities/")
Call<MainResponseObjectClass> getValue(#Header("Authorization") String AutToken, #Body MainObjectClass mainObjectClass);
}
ApliClient.java
public class ApiClient {
public static final String BASE_URL = "https://myapi.test.com";
private static Retrofit retrofit = null;
public static Retrofit getClient(){
if (retrofit == null){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
POST request code
CvFileObject.java
public class CvFileObject {
#SerializedName("tsync_id")
private String tsync_id;
public CvFileObject(String tsync_id) {
this.tsync_id = tsync_id;
}
public String getTsync_id() {
return tsync_id;
}
public void setTsync_id(String tsync_id) {
this.tsync_id = tsync_id;
}
}
MainObjectClass.java
public class MainObjectClass {
#SerializedName("tsync_id")
private String tsync_id;
#SerializedName("name")
private String name;
#SerializedName("email")
private String email;
#SerializedName("phone")
private String phone;
#SerializedName("full_address")
private String full_address;
#SerializedName("name_of_university")
private String name_of_university;
#SerializedName("graduation_year")
private int graduation_year;
#SerializedName("cgpa")
private double cgpa;
#SerializedName("experience_in_months")
private String experience_in_months;
#SerializedName("current_work_place_name")
private String current_work_place_name;
#SerializedName("applying_in")
private String applying_in;
#SerializedName("expected_salary")
private int expected_salary;
#SerializedName("reference")
private String reference;
#SerializedName("github_project_url")
private String github_project_url;
#SerializedName("cv_file")
private CvFileObject fileObject;
#SerializedName("on_spot_update_time")
long on_spot_update_time;
#SerializedName("on_spot_creation_time")
long on_spot_creation_time;
public MainObjectClass(String tsync_id, String name, String email, String phone, String full_address,
String name_of_university, int graduation_year, double cgpa, String experience_in_months,
String current_work_place_name, String applying_in, int expected_salary, String reference,
String github_project_url, CvFileObject fileObject, long on_spot_update_time, long on_spot_creation_time) {
this.tsync_id = tsync_id;
this.name = name;
this.email = email;
this.phone = phone;
this.full_address = full_address;
this.name_of_university = name_of_university;
this.graduation_year = graduation_year;
this.cgpa = cgpa;
this.experience_in_months = experience_in_months;
this.current_work_place_name = current_work_place_name;
this.applying_in = applying_in;
this.expected_salary = expected_salary;
this.reference = reference;
this.github_project_url = github_project_url;
this.fileObject = fileObject;
this.on_spot_update_time = on_spot_update_time;
this.on_spot_creation_time = on_spot_creation_time;
}
public String getTsync_id() {
return tsync_id;
}
public void setTsync_id(String tsync_id) {
this.tsync_id = tsync_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getFull_address() {
return full_address;
}
public void setFull_address(String full_address) {
this.full_address = full_address;
}
public String getName_of_university() {
return name_of_university;
}
public void setName_of_university(String name_of_university) {
this.name_of_university = name_of_university;
}
public int getGraduation_year() {
return graduation_year;
}
public void setGraduation_year(int graduation_year) {
this.graduation_year = graduation_year;
}
public double getCgpa() {
return cgpa;
}
public void setCgpa(double cgpa) {
this.cgpa = cgpa;
}
public String getExperience_in_months() {
return experience_in_months;
}
public void setExperience_in_months(String experience_in_months) {
this.experience_in_months = experience_in_months;
}
public String getCurrent_work_place_name() {
return current_work_place_name;
}
public void setCurrent_work_place_name(String current_work_place_name) {
this.current_work_place_name = current_work_place_name;
}
public String getApplying_in() {
return applying_in;
}
public void setApplying_in(String applying_in) {
this.applying_in = applying_in;
}
public int getExpected_salary() {
return expected_salary;
}
public void setExpected_salary(int expected_salary) {
this.expected_salary = expected_salary;
}
public String reference() {
return reference;
}
public void setreference(String reference) {
this.reference = reference;
}
public String getGithub_project_url() {
return github_project_url;
}
public void setGithub_project_url(String github_project_url) {
this.github_project_url = github_project_url;
}
public CvFileObject getFileObject() {
return fileObject;
}
public void setFileObject(CvFileObject fileObject) {
this.fileObject = fileObject;
}
public long getOn_spot_update_time() {
return on_spot_update_time;
}
public void setOn_spot_update_time(long on_spot_update_time) {
this.on_spot_update_time = on_spot_update_time;
}
public long getOn_spot_creation_time() {
return on_spot_creation_time;
}
public void setOn_spot_creation_time(long on_spot_creation_time) {
this.on_spot_creation_time = on_spot_creation_time;
}
}
POST request response code
CvFileResponseObject.java
public class CvFileResponseObject {
#SerializedName("id")
int id;
#SerializedName("tsync_id")
private String tsync_id;
public CvFileResponseObject(String tsync_id) {
this.tsync_id = tsync_id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTsync_id() {
return tsync_id;
}
public void setTsync_id(String tsync_id) {
this.tsync_id = tsync_id;
}
}
MainResponseObjectClass.java
public class MainResponseObjectClass {
#SerializedName("tsync_id")
private int tsync_id;
#SerializedName("name")
private String name;
#SerializedName("email")
private String email;
#SerializedName("phone")
private String phone;
#SerializedName("full_address")
private String full_address;
#SerializedName("name_of_university")
private String name_of_university;
#SerializedName("graduation_year")
private int graduation_year;
#SerializedName("cgpa")
private double cgpa;
#SerializedName("experience_in_months")
private String experience_in_months;
#SerializedName("current_work_place_name")
private String current_work_place_name;
#SerializedName("applying_in")
private String applying_in;
#SerializedName("expected_salary")
private String expected_salary;
#SerializedName("reference")
private String reference;
#SerializedName("github_project_url")
private String github_project_url;
#SerializedName("cv_file")
private CvFileResponseObject cvFileResponseObject;
#SerializedName("on_spot_update_time")
long on_spot_update_time;
#SerializedName("on_spot_creation_time")
long on_spot_creation_time;
#SerializedName("success")
private boolean success;
#SerializedName("message")
private String message;
public MainResponseObjectClass(int tsync_id, String name, String email, String phone,
String full_address, String name_of_university, int graduation_year,
double cgpa, String experience_in_months, String current_work_place_name,
String applying_in, String expected_salary, String reference,
String github_project_url, CvFileResponseObject cvFileResponseObject, long on_spot_update_time,
long on_spot_creation_time, boolean success, String message) {
this.tsync_id = tsync_id;
this.name = name;
this.email = email;
this.phone = phone;
this.full_address = full_address;
this.name_of_university = name_of_university;
this.graduation_year = graduation_year;
this.cgpa = cgpa;
this.experience_in_months = experience_in_months;
this.current_work_place_name = current_work_place_name;
this.applying_in = applying_in;
this.expected_salary = expected_salary;
this.reference = reference;
this.github_project_url = github_project_url;
this.cvFileResponseObject = cvFileResponseObject;
this.on_spot_update_time = on_spot_update_time;
this.on_spot_creation_time = on_spot_creation_time;
this.success = success;
this.message = message;
}
public int getTsync_id() {
return tsync_id;
}
public void setTsync_id(int tsync_id) {
this.tsync_id = tsync_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getFull_address() {
return full_address;
}
public void setFull_address(String full_address) {
this.full_address = full_address;
}
public String getName_of_university() {
return name_of_university;
}
public void setName_of_university(String name_of_university) {
this.name_of_university = name_of_university;
}
public int getGraduation_year() {
return graduation_year;
}
public void setGraduation_year(int graduation_year) {
this.graduation_year = graduation_year;
}
public double getCgpa() {
return cgpa;
}
public void setCgpa(double cgpa) {
this.cgpa = cgpa;
}
public String getExperience_in_months() {
return experience_in_months;
}
public void setExperience_in_months(String experience_in_months) {
this.experience_in_months = experience_in_months;
}
public String getCurrent_work_place_name() {
return current_work_place_name;
}
public void setCurrent_work_place_name(String current_work_place_name) {
this.current_work_place_name = current_work_place_name;
}
public String getApplying_in() {
return applying_in;
}
public void setApplying_in(String applying_in) {
this.applying_in = applying_in;
}
public String getExpected_salary() {
return expected_salary;
}
public void setExpected_salary(String expected_salary) {
this.expected_salary = expected_salary;
}
public String getField_buzz_reference() {
return field_buzz_reference;
}
public void setFeference(String reference) {
this.field_buzz_reference = reference;
}
public String getGithub_project_url() {
return github_project_url;
}
public void setGithub_project_url(String github_project_url) {
this.github_project_url = github_project_url;
}
public CvFileResponseObject getCvFileResponseObject() {
return cvFileResponseObject;
}
public void setCvFileResponseObject(CvFileResponseObject cvFileResponseObject) {
this.cvFileResponseObject = cvFileResponseObject;
}
public long getOn_spot_update_time() {
return on_spot_update_time;
}
public void setOn_spot_update_time(long on_spot_update_time) {
this.on_spot_update_time = on_spot_update_time;
}
public long getOn_spot_creation_time() {
return on_spot_creation_time;
}
public void setOn_spot_creation_time(long on_spot_creation_time) {
this.on_spot_creation_time = on_spot_creation_time;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Here I can see two APIs. But I am not sure which to which API you are getting 401. I can help you with the possibilities that you could check and please feel free to ask.
Assuming you are getting 401 for login API - the reason could be you are trying with wrong credentials.
Assuming you are getting 401 for getValue API - the reason could be you might passing the wrong token which you got from login response. Especially you need to cross-check the header with key and values.
And I have one suggestion - you could use header in Interceptor with retrofit setup itself.
I am trying to get the value of a JSON response and display it in my textView and editText. But I get a null object reference as an error.
JSON Response:
{
"srNo": 1,
"date": "11/14/2019 12:00:00 AM",
"fieldEngineer": "Angel",
"accountName": "Forever 21 Megamall",
"irNo": 1,
"joNo": 1,
"address": "Mandaluyong City",
"contactPerson": "Jansen Babon",
"designation": "",
"contactNo": "",
"email": "",
"timeIn": "00:00:00",
"timeOut": "00:00:00",
"productType": "Security",
"problem": ""
}
Java class:
private void fetchData() {
JsonObject paramObject = new JsonObject();
Call<ResObj> call = userService.userLogin(paramObject);
call.enqueue(new Callback<ResObj>() {
#Override
public void onResponse(Call<ResObj> call, retrofit2.Response<ResObj> response) {
ResObj resObj = response.body();
String srNo = resObj.getSrNo();
String date = resObj.getDate();
String fieldEngineer = resObj.getFieldEngineer();
String accountName = resObj.getAccountName();
String irNo = resObj.getIrNo();
String joNo = resObj.getJoNo();
String address = resObj.getAddress();
String contactPerson = resObj.getContactPerson();
String designation = resObj.getDesignation();
String contactNo = resObj.getContactNo();
String email = resObj.getEmail();
String timeIn = resObj.getTimeIn();
String timeOut = resObj.getTimeOut();
String productType = resObj.getProductType();
String problem = resObj.getProblem();
//the response I am getting here is null
tvSrNo.setText(srNo);
etdate.setText(date);
etfieldengineer.setText(fieldEngineer);
etaccname.setText(accountName);
etirno.setText(irNo);
etjono.setText(joNo);
JsonObject workObj = new JsonObject();
try {
workObj.addProperty("srNo", resObj.getSrNo());
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onFailure(Call<ResObj> call, Throwable t) {
}
});
}
I tried using this tvSrNo.setText(resObj.getSrNo()) instead of tvSrNo.setText(srNo) but it still gets the same problem.
I am also using Retrofit.
I expect the result that JSON data will be placed in an editText or textView. But apparently, the response is getting null.
ResObj class:
private String date;
private String address;
private String accountName;
private String contactPerson;
private String timeOut;
private String problem;
private String srNo;
private String fieldEngineer;
private String joNo;
private String irNo;
private String message;
private String designation;
private String email;
private String timeIn;
private String productType;
private boolean status;
private String contactNo;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String getContactPerson() {
return contactPerson;
}
public void setContactPerson(String contactPerson) {
this.contactPerson = contactPerson;
}
public String getTimeOut() {
return timeOut;
}
public void setTimeOut(String timeOut) {
this.timeOut = timeOut;
}
public String getProblem() {
return problem;
}
public void setProblem(String problem) {
this.problem = problem;
}
public String getSrNo() {
return srNo;
}
public void setSrNo(String srNo) {
this.srNo = srNo;
}
public String getFieldEngineer() {
return fieldEngineer;
}
public void setFieldEngineer(String fieldEngineer) {
this.fieldEngineer = fieldEngineer;
}
public String getJoNo() {
return joNo;
}
public void setJoNo(String joNo) {
this.joNo = joNo;
}
public String getIrNo() {
return irNo;
}
public void setIrNo(String irNo) {
this.irNo = irNo;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTimeIn() {
return timeIn;
}
public void setTimeIn(String timeIn) {
this.timeIn = timeIn;
}
public String getProductType() {
return productType;
}
public void setProductType(String productType) {
this.productType = productType;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
Logcat:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.android.ras.ResObj.getSrNo()' on a null object reference
at com.example.android.ras.MainActivity$3.onResponse(MainActivity.java:187)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:71)
at android.os.Handler.handleCallback(Handler.java:907)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
Change your String to Integer
public class Codebeautify {
private Integer srNo;
private String date;
private String fieldEngineer;
private String accountName;
private Integer irNo;
private Integer joNo;
private String address;
private String contactPerson;
private String designation;
private String contactNo;
private String email;
private String timeIn;
private String timeOut;
private String productType;
private String problem;
// Getter Methods
public Integer getSrNo() {
return srNo;
}
public String getDate() {
return date;
}
public String getFieldEngineer() {
return fieldEngineer;
}
public String getAccountName() {
return accountName;
}
public Integer getIrNo() {
return irNo;
}
public Integer getJoNo() {
return joNo;
}
public String getAddress() {
return address;
}
public String getContactPerson() {
return contactPerson;
}
public String getDesignation() {
return designation;
}
public String getContactNo() {
return contactNo;
}
public String getEmail() {
return email;
}
public String getTimeIn() {
return timeIn;
}
public String getTimeOut() {
return timeOut;
}
public String getProductType() {
return productType;
}
public String getProblem() {
return problem;
}
// Setter Methods
public void setSrNo(Integer srNo) {
this.srNo = srNo;
}
public void setDate(String date) {
this.date = date;
}
public void setFieldEngineer(String fieldEngineer) {
this.fieldEngineer = fieldEngineer;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public void setIrNo(Integer irNo) {
this.irNo = irNo;
}
public void setJoNo(Integer joNo) {
this.joNo = joNo;
}
public void setAddress(String address) {
this.address = address;
}
public void setContactPerson(String contactPerson) {
this.contactPerson = contactPerson;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
public void setEmail(String email) {
this.email = email;
}
public void setTimeIn(String timeIn) {
this.timeIn = timeIn;
}
public void setTimeOut(String timeOut) {
this.timeOut = timeOut;
}
public void setProductType(String productType) {
this.productType = productType;
}
public void setProblem(String problem) {
this.problem = problem;
}
}
Make your POJO class like this
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
#SerializedName("srNo")
#Expose
private Integer srNo;
#SerializedName("date")
#Expose
private String date;
#SerializedName("fieldEngineer")
#Expose
private String fieldEngineer;
#SerializedName("accountName")
#Expose
private String accountName;
#SerializedName("irNo")
#Expose
private Integer irNo;
#SerializedName("joNo")
#Expose
private Integer joNo;
#SerializedName("address")
#Expose
private String address;
#SerializedName("contactPerson")
#Expose
private String contactPerson;
#SerializedName("designation")
#Expose
private String designation;
#SerializedName("contactNo")
#Expose
private String contactNo;
#SerializedName("email")
#Expose
private String email;
#SerializedName("timeIn")
#Expose
private String timeIn;
#SerializedName("timeOut")
#Expose
private String timeOut;
#SerializedName("productType")
#Expose
private String productType;
#SerializedName("problem")
#Expose
private String problem;
public Integer getSrNo() {
return srNo;
}
public void setSrNo(Integer srNo) {
this.srNo = srNo;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getFieldEngineer() {
return fieldEngineer;
}
public void setFieldEngineer(String fieldEngineer) {
this.fieldEngineer = fieldEngineer;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public Integer getIrNo() {
return irNo;
}
public void setIrNo(Integer irNo) {
this.irNo = irNo;
}
public Integer getJoNo() {
return joNo;
}
public void setJoNo(Integer joNo) {
this.joNo = joNo;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getContactPerson() {
return contactPerson;
}
public void setContactPerson(String contactPerson) {
this.contactPerson = contactPerson;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTimeIn() {
return timeIn;
}
public void setTimeIn(String timeIn) {
this.timeIn = timeIn;
}
public String getTimeOut() {
return timeOut;
}
public void setTimeOut(String timeOut) {
this.timeOut = timeOut;
}
public String getProductType() {
return productType;
}
public void setProductType(String productType) {
this.productType = productType;
}
public String getProblem() {
return problem;
}
public void setProblem(String problem) {
this.problem = problem;
}
}
make sure to have Gson Converter in your retrofit instance
private static Retrofit getRetrofitInstance() {
return new Retrofit.Builder()
.baseUrl(ROOT_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
then make call and put the data in ArrayList
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.android.ras.ResObj.getSrNo()' on a null object reference
Note: NullPointerException because you did not declare SrNo inside the model class
Try to use a jason to java class generator:
http://www.jsonschema2pojo.org/
Source type: JSON
Annotation style: Gson( if you used GSON) or none
Include getters and setters
public class Example {
private Integer srNo;
private String date;
private String fieldEngineer;
private String accountName;
private Integer irNo;
private Integer joNo;
private String address;
private String contactPerson;
private String designation;
private String contactNo;
private String email;
private String timeIn;
private String timeOut;
private String productType;
private String problem;
public Integer getSrNo() {
return srNo;
}
public void setSrNo(Integer srNo) {
this.srNo = srNo;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getFieldEngineer() {
return fieldEngineer;
}
public void setFieldEngineer(String fieldEngineer) {
this.fieldEngineer = fieldEngineer;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public Integer getIrNo() {
return irNo;
}
public void setIrNo(Integer irNo) {
this.irNo = irNo;
}
public Integer getJoNo() {
return joNo;
}
public void setJoNo(Integer joNo) {
this.joNo = joNo;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getContactPerson() {
return contactPerson;
}
public void setContactPerson(String contactPerson) {
this.contactPerson = contactPerson;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTimeIn() {
return timeIn;
}
public void setTimeIn(String timeIn) {
this.timeIn = timeIn;
}
public String getTimeOut() {
return timeOut;
}
public void setTimeOut(String timeOut) {
this.timeOut = timeOut;
}
public String getProductType() {
return productType;
}
public void setProductType(String productType) {
this.productType = productType;
}
public String getProblem() {
return problem;
}
public void setProblem(String problem) {
this.problem = problem;
}
}
first : Check response output,
you can Log.i or Toast it,,,
If your response not load or null ... - (the Problem in here)
Second : if respon Ok, Check your ResObj.getSrNo().
Print again... check
String srNo = resObj.getSrNo();
Log.i srNo... (problem or not)
Or checkyour Class Codebeautify
JsonObject paramObject = new JsonObject();
Call<ResObj> call = userService.userLogin(paramObject); // paramObject is empty object
You are passing empty JsonObject to your API parameter.
So you have to add parameter value to your paramObject. like this
try {
JsonObject paramObject = new JsonObject();
paramObject.addProperty("mobile", mobile);
// add other properties if you have
} catch (JSONException e) {
e.printStackTrace();
}
after that you should call your api like
Call<ResObj> call = userService.userLogin(paramObject);
As i am seeing the problem is in parsing, The retrofit can not mapping as your response is n't having ResObj as root.
{ "ResObj": { "srNo": 1, "date": "11/14/201912: 00: 00AM", "fieldEngineer": "Angel", "accountName": "Forever21Megamall", "irNo": 1, "joNo": 1, "address": "MandaluyongCity", "contactPerson": "JansenBabon", "designation": "", "contactNo": "", "email": "", "timeIn": "00: 00: 00", "timeOut": "00: 00: 00", "productType": "Security", "problem": "" } }
Modify your response or change your request
Call<JSONObject> call = userService.userLogin(paramObject);
Later in extract the values manually.
Here is my JSONArray Response from Web service:
And Class Java :
public class Product {
private int id,price,discount;
private String name,image,description,discount_type,discount_exp;
#SerializedName("products")
private List<Product> products;
public Product()
{
}
}
response is null
Your POJO class is wrong
Try this
public class Products_Main
{
#SerializedName("current_page")
int current_page;
#SerializedName("data")
private List<Product> products;
public int getCurrent_page() {
return current_page;
}
public void setCurrent_page(int current_page) {
this.current_page = current_page;
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
}
and
class Product {
#SerializedName("id")
private int id;
#SerializedName("price")
int price;
#SerializedName("discount")
int discount;
#SerializedName("name")
private String name;
#SerializedName("image")
private String image;
#SerializedName("description")
private String description;
#SerializedName("discount_type")
private String discount_type;
#SerializedName("discount_exp")
private String discount_exp;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getDiscount() {
return discount;
}
public void setDiscount(int discount) {
this.discount = discount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDiscount_type() {
return discount_type;
}
public void setDiscount_type(String discount_type) {
this.discount_type = discount_type;
}
public String getDiscount_exp() {
return discount_exp;
}
public void setDiscount_exp(String discount_exp) {
this.discount_exp = discount_exp;
}
}
Any null data happens when the parsing failed.
This is the Java for the response you've shown
public class ProductResponse {
ProductPage products;
}
public class ProductPage {
int current_page;
#SerializedName("data")
private List<Product> products;
}
public class Product {
private int id,price,discount;
private String name,image,description,discount_type,discount_exp;
public Product() { }
}
You have to change like it will work
public class ProductModel {
private int current_page;
private ArrayList<DataModel> data = new ArrayList<>();
public int getCurrent_page() {
return current_page;
}
public void setCurrent_page(int current_page) {
this.current_page = current_page;
}
public ArrayList<DataModel> getData() {
return data;
}
public void setData(ArrayList<DataModel> data) {
this.data = data;
}
private class DataModel{
private int id,price,discount,shop_id,vote_id,vedeo_id,status;
private String name,image,description,discount_type,discount_exp,created_at,updated_at;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getDiscount() {
return discount;
}
public void setDiscount(int discount) {
this.discount = discount;
}
public int getShop_id() {
return shop_id;
}
public void setShop_id(int shop_id) {
this.shop_id = shop_id;
}
public int getVote_id() {
return vote_id;
}
public void setVote_id(int vote_id) {
this.vote_id = vote_id;
}
public int getVedeo_id() {
return vedeo_id;
}
public void setVedeo_id(int vedeo_id) {
this.vedeo_id = vedeo_id;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDiscount_type() {
return discount_type;
}
public void setDiscount_type(String discount_type) {
this.discount_type = discount_type;
}
public String getDiscount_exp() {
return discount_exp;
}
public void setDiscount_exp(String discount_exp) {
this.discount_exp = discount_exp;
}
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
public String getUpdated_at() {
return updated_at;
}
public void setUpdated_at(String updated_at) {
this.updated_at = updated_at;
}
}
}
The issue is that the products are contained within the data field of the JSON response. Retrofit doesn't know this so it can't deserialize the response into a Product array.
You should use it like that
=>Main API Model Class
public class APIResult {
#SerializedName("products")
private ProductModelClass products;
public ProductModelClass getProducts() {
return products;
}
public void setProducts(ProductModelClass products) {
this.products = products;
}
}
=>For data and current page model class
public class ProductModelClass {
#SerializedName("current_page")
private int current_page;
#SerializedName("data")
private ArrayList<ProductDataModel> data;
public int getCurrent_page() {
return current_page;
}
public void setCurrent_page(int current_page) {
this.current_page = current_page;
}
public ArrayList<ProductDataModel> getData() {
return data;
}
public void setData(ArrayList<ProductDataModel> data) {
this.data = data;
}
}
=>For Details of products Data
public class ProductDataModel {
#SerializedName("id")
private int id;
#SerializedName("name")
private String name;
#SerializedName("image")
private String image;
#SerializedName("description")
private String description;
#SerializedName("price")
private int price;
#SerializedName("discount")
private int discount;
#SerializedName("shop_id")
private int shop_id;
#SerializedName("discount_type")
private String discount_type;
#SerializedName("discount_exp")
private String discount_exp;
#SerializedName("discount_limit")
private String discount_limit;
#SerializedName("vote_id")
private String vote_id;
#SerializedName("video_id")
private String video_id;
#SerializedName("status")
private String status;
#SerializedName("created_at")
private String created_at;
#SerializedName("updated_at")
private String updated_at;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getDiscount() {
return discount;
}
public void setDiscount(int discount) {
this.discount = discount;
}
public int getShop_id() {
return shop_id;
}
public void setShop_id(int shop_id) {
this.shop_id = shop_id;
}
public String getDiscount_type() {
return discount_type;
}
public void setDiscount_type(String discount_type) {
this.discount_type = discount_type;
}
public String getDiscount_exp() {
return discount_exp;
}
public void setDiscount_exp(String discount_exp) {
this.discount_exp = discount_exp;
}
public String getDiscount_limit() {
return discount_limit;
}
public void setDiscount_limit(String discount_limit) {
this.discount_limit = discount_limit;
}
public String getVote_id() {
return vote_id;
}
public void setVote_id(String vote_id) {
this.vote_id = vote_id;
}
public String getVideo_id() {
return video_id;
}
public void setVideo_id(String video_id) {
this.video_id = video_id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
public String getUpdated_at() {
return updated_at;
}
public void setUpdated_at(String updated_at) {
this.updated_at = updated_at;
}
}
im trying to parse information from this website http://www.petwave.com/Dogs/Breeds/Airedale-Terrier.aspx.. at the end theres a table with a few attributes with star ratings. Right now i have only values with strings parsing.. is there anyway to parse ratings from a website? I would want to change these values such as Lifespan and origin into the star values ? any idea how to do this?
class Dog implements Serializable {
private String name;
private String origin;
private String lifeSpan;
private String sizeType;
private String otherNames;
private ArrayList<Detail> details;
private String thumbnailURL;
private String mainImageURL;
private String url;
private String articleText = "";
private Breed.Name breed;
private boolean basicDataReady = false;
private boolean detailDataReady = false;
Dog(String name, String sizeType, String thumbnailURL, String url, Breed.Name breed) {
this.name = name;
this.sizeType = sizeType;
this.thumbnailURL = thumbnailURL;
this.url = url;
this.breed = breed;
basicDataReady = true;
}
Dog(String name, String origin, String lifeSpan, String url, String thumbnailURL, Breed.Name breed) {
this.name = name;
this.origin = origin;
this.lifeSpan = lifeSpan;
this.url = url;
this.thumbnailURL = thumbnailURL;
this.breed = breed;
basicDataReady = true;
}
Dog(String name, String url, String thumbnailURL, Breed.Name breed) {
this.name = name;
this.url = url;
this.thumbnailURL = thumbnailURL;
this.breed = breed;
basicDataReady = true;
}
Dog(String url, Breed.Name breed) {
this.url = url;
this.breed = breed;
}
Dog() {
}
public String getArticleText() {
return articleText;
}
public void setArticleText(String articleText) {
this.articleText = articleText;
}
public String getOrigin() {
return origin;
}
public String getLifeSpan() {
return lifeSpan;
}
public String getSizeType() {
return sizeType;
}
public boolean isBasicDataReady() {
return basicDataReady;
}
public void setBasicDataReady(boolean basicDataReady) {
this.basicDataReady = basicDataReady;
}
public boolean isDetailDataReady() {
return detailDataReady;
}
public void setDetailDataReady(boolean detailDataReady) {
this.detailDataReady = detailDataReady;
}
public Breed.Name getBreed() {
return breed;
}
public void setBreed(Breed.Name breed) {
this.breed = breed;
}
public String getMainImageURL() {
return mainImageURL;
}
public void setMainImageURL(String mainImageURL) {
this.mainImageURL = mainImageURL;
}
public ArrayList<Detail> getDetails() {
return details;
}
public void setDetails(ArrayList<Detail> details) {
this.details = details;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOtherNames() {
return otherNames;
}
public void setOtherNames(String otherNames) {
this.otherNames = otherNames;
}
public String getThumbnailURL() {
return thumbnailURL;
}
public void setThumbnailURL(String thumbnailURL) {
this.thumbnailURL = thumbnailURL;
}
public String getUrl() {
return url;
}
static class Detail implements Serializable {
private final String key;
private final String value;
public Detail(String key, String value) {
this.key = key;
this.value = value;
}
public String getValue() {
return value;
}
public String getKey() {
return key;
}
}
}