get map inside map java android - java

I have this JSON response.
I want to fetch the times data that is inside datetime.
{
"code":200,
"status":"OK",
"results":{
"datetime":[
{
"times":{
"Imsak":"04:21",
"Sunrise":"-",
"Fajr":"04:31",
"Dhuhr":"11:41",
"Asr":"14:58",
"Sunset":"-",
"Maghrib":"17:54",
"Isha":"18:46",
"Midnight":"-"
},
"date":{
"timestamp":1617667200,
"gregorian":"2021-04-06",
"hijri":"1442-08-24"
}
}
],
"location":{
"latitude":-6.966667,
"longitude":110.416664,
"elevation":-9999.0,
"country":"",
"country_code":"ID",
"timezone":"Asia/Jakarta",
"local_offset":7.0
},
"settings":{
"timeformat":"HH:mm",
"school":"Ithna Ashari",
"juristic":"Shafii",
"highlat":"None",
"fajr_angle":18.0,
"isha_angle":17.0
}
}
}
How to get object "times" and take it into the map.
How to take the individual values as well as an array.

Below code will make "times" object. Like:
String json = "{\"code\":200,\"status\":\"OK\",\"results\":{\"datetime\":[{\"times\":{\"Imsak\":\"04:21\",\"Sunrise\":\"-\",\"Fajr\":\"04:31\",\"Dhuhr\":\"11:41\",\"Asr\":\"14:58\",\"Sunset\":\"-\",\"Maghrib\":\"17:54\",\"Isha\":\"18:46\",\"Midnight\":\"-\"},\"date\":{\"timestamp\":1617667200,\"gregorian\":\"2021-04-06\",\"hijri\":\"1442-08-24\"}}],\"location\":{\"latitude\":-6.966667,\"longitude\":110.416664,\"elevation\":-9999.0,\"country\":\"\",\"country_code\":\"ID\",\"timezone\":\"Asia/Jakarta\",\"local_offset\":7.0},\"settings\":{\"timeformat\":\"HH:mm\",\"school\":\"Ithna Ashari\",\"juristic\":\"Shafii\",\"highlat\":\"None\",\"fajr_angle\":18.0,\"isha_angle\":17.0}}}";
try {
JSONObject rootJsonObject = new JSONObject(json);
JSONObject resultJsonObject = rootJsonObject.getJSONObject("results");
JSONArray dateTimeJsonArray = resultJsonObject.getJSONArray("datetime");
JSONObject timeJsonObject = new JSONObject(dateTimeJsonArray.get(0).toString());
JSONObject timeJson = new JSONObject(timeJsonObject.getString("times"));
Log.d("TAG","Time json = "+timeJson);
}catch (JSONException jsonException){
Log.d("TAG","JSON Exception: "+jsonException.getLocalizedMessage());
}

public class Example {
#SerializedName("code")
#Expose
private Integer code;
#SerializedName("status")
#Expose
private String status;
#SerializedName("results")
#Expose
private Results results;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Results getResults() {
return results;
}
public void setResults(Results results) {
this.results = results;
}
}
public class Results {
#SerializedName("datetime")
#Expose
private List<Datetime> datetime = null;
#SerializedName("location")
#Expose
private Location location;
#SerializedName("settings")
#Expose
private Settings settings;
public List<Datetime> getDatetime() {
return datetime;
}
public void setDatetime(List<Datetime> datetime) {
this.datetime = datetime;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public Settings getSettings() {
return settings;
}
public void setSettings(Settings settings) {
this.settings = settings;
}
}
public class Datetime {
#SerializedName("times")
#Expose
private HashMap<String,String> times;
#SerializedName("date")
#Expose
private Date date;
public HashMap<String,String> getTimes() {
return times;
}
public void setTimes(HashMap<String,String> times) {
this.times = times;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
public class Settings {
#SerializedName("timeformat")
#Expose
private String timeformat;
#SerializedName("school")
#Expose
private String school;
#SerializedName("juristic")
#Expose
private String juristic;
#SerializedName("highlat")
#Expose
private String highlat;
#SerializedName("fajr_angle")
#Expose
private Double fajrAngle;
#SerializedName("isha_angle")
#Expose
private Double ishaAngle;
public String getTimeformat() {
return timeformat;
}
public void setTimeformat(String timeformat) {
this.timeformat = timeformat;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getJuristic() {
return juristic;
}
public void setJuristic(String juristic) {
this.juristic = juristic;
}
public String getHighlat() {
return highlat;
}
public void setHighlat(String highlat) {
this.highlat = highlat;
}
public Double getFajrAngle() {
return fajrAngle;
}
public void setFajrAngle(Double fajrAngle) {
this.fajrAngle = fajrAngle;
}
public Double getIshaAngle() {
return ishaAngle;
}
public void setIshaAngle(Double ishaAngle) {
this.ishaAngle = ishaAngle;
}
}
public class Location {
#SerializedName("latitude")
#Expose
private Double latitude;
#SerializedName("longitude")
#Expose
private Double longitude;
#SerializedName("elevation")
#Expose
private Double elevation;
#SerializedName("country")
#Expose
private String country;
#SerializedName("country_code")
#Expose
private String countryCode;
#SerializedName("timezone")
#Expose
private String timezone;
#SerializedName("local_offset")
#Expose
private Double localOffset;
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public Double getElevation() {
return elevation;
}
public void setElevation(Double elevation) {
this.elevation = elevation;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getTimezone() {
return timezone;
}
public void setTimezone(String timezone) {
this.timezone = timezone;
}
public Double getLocalOffset() {
return localOffset;
}
public void setLocalOffset(Double localOffset) {
this.localOffset = localOffset;
}
}

Related

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 828 path $.data.user.agent.identity_type

package com.cico.cicocashincashout.model.login.response;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Agent {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("first_name")
#Expose
private String firstName;
#SerializedName("last_name")
#Expose
private String lastName;
#SerializedName("user_id")
#Expose
private String userId;
#SerializedName("date_of_birth")
#Expose
private String dateOfBirth;
#SerializedName("email")
#Expose
private String email;
#SerializedName("gender")
#Expose
private String gender;
#SerializedName("business_name")
#Expose
private String businessName;
#SerializedName("business_address")
#Expose
private String businessAddress;
#SerializedName("business_phone")
#Expose
private String businessPhone;
#SerializedName("state_id")
#Expose
private Integer stateId;
#SerializedName("local_government_id")
#Expose
private Integer localGovernmentId;
#SerializedName("account_number")
#Expose
private String accountNumber;
#SerializedName("account_name")
#Expose
private String accountName;
#SerializedName("bank_id")
#Expose
private Integer bankId;
#SerializedName("bvn")
#Expose
private String bvn;
#SerializedName("agent_code")
#Expose
private String agentCode;
#SerializedName("identity_type")
#Expose
private Terminal identityType;
#SerializedName("business_type")
#Expose
private String businessType;
#SerializedName("super_agent_id")
#Expose
private Terminal superAgentId;
#SerializedName("status")
#Expose
private String status;
#SerializedName("wallet_no")
#Expose
private String walletNo;
#SerializedName("terminal_id")
#Expose
private Terminal terminalId;
#SerializedName("created_at")
#Expose
private String createdAt;
#SerializedName("updated_at")
#Expose
private String updatedAt;
#SerializedName("activation_code")
#Expose
private String activationCode;
#SerializedName("bank_code")
#Expose
private String bankCode;
#SerializedName("uuid")
#Expose
private String uuid;
#SerializedName("api_key")
#Expose
private String apiKey;
#SerializedName("commission_value")
#Expose
private Terminal commissionValue;
#SerializedName("webhook")
#Expose
private Terminal webhook;
#SerializedName("full_name")
#Expose
private String fullName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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 getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBusinessName() {
return businessName;
}
public void setBusinessName(String businessName) {
this.businessName = businessName;
}
public String getBusinessAddress() {
return businessAddress;
}
public void setBusinessAddress(String businessAddress) {
this.businessAddress = businessAddress;
}
public String getBusinessPhone() {
return businessPhone;
}
public void setBusinessPhone(String businessPhone) {
this.businessPhone = businessPhone;
}
public Integer getStateId() {
return stateId;
}
public void setStateId(Integer stateId) {
this.stateId = stateId;
}
public Integer getLocalGovernmentId() {
return localGovernmentId;
}
public void setLocalGovernmentId(Integer localGovernmentId) {
this.localGovernmentId = localGovernmentId;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public Integer getBankId() {
return bankId;
}
public void setBankId(Integer bankId) {
this.bankId = bankId;
}
public String getBvn() {
return bvn;
}
public void setBvn(String bvn) {
this.bvn = bvn;
}
public String getAgentCode() {
return agentCode;
}
public void setAgentCode(String agentCode) {
this.agentCode = agentCode;
}
public Terminal getIdentityType() {
return identityType;
}
public void setIdentityType(Terminal identityType) {
this.identityType = identityType;
}
public String getBusinessType() {
return businessType;
}
public void setBusinessType(String businessType) {
this.businessType = businessType;
}
public Terminal getSuperAgentId() {
return superAgentId;
}
public void setSuperAgentId(Terminal superAgentId) {
this.superAgentId = superAgentId;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getWalletNo() {
return walletNo;
}
public void setWalletNo(String walletNo) {
this.walletNo = walletNo;
}
public Terminal getTerminalId() {
return terminalId;
}
public void setTerminalId(Terminal terminalId) {
this.terminalId = terminalId;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
public String getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
public String getActivationCode() {
return activationCode;
}
public void setActivationCode(String activationCode) {
this.activationCode = activationCode;
}
public String getBankCode() {
return bankCode;
}
public void setBankCode(String bankCode) {
this.bankCode = bankCode;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getApiKey() {
return apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
public Terminal getCommissionValue() {
return commissionValue;
}
public void setCommissionValue(Terminal commissionValue) {
this.commissionValue = commissionValue;
}
public Terminal getWebhook() {
return webhook;
}
public void setWebhook(Terminal webhook) {
this.webhook = webhook;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
}
from the response, identity type is actually a string, I really don't know where I am getting it wrong
"identity_type": "driver's license",
You have mentioned identity_type is an object but you are receiving a string
#SerializedName("identity_type")
#Expose
private Terminal identityType;
Change Terminal to String it would work.
Your identity_type is coming as String but you have declared as Terminal.
Change from
#SerializedName("identity_type")
#Expose
private Terminal identityType;
To
#SerializedName("identity_type")
#Expose
private String identityType;
Hope this will help you.

How to display JSON data from API server in TextView and EditText in Android?

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.

JsonIllegalstateexception:- Expected BEGIN_ARRAY but was String at line 1 column 61 . $ customer.Data

There is JSON file and i want get through POJO so i Created POJO file as well but at customer data it show in some error like i mention in title i don't know what's happening i mean my whole team don't why it's happening.
I've try everything which i would try but nothing is helping me out
{
"Success": true,
"Message": "Device Not Found",
"Customer_Data": [{
"customerid": 12365,
"customername": "Mukesh SHARMA",
"gender": "M",
"personalcontact": 999999999,
"homecontact": 999999999,
"emailid": "mukeshr334#gmail.com",
"address": "RockStar",
"designation": "Chief Life Insurance Advisor",
"stateid": 22,
"statename": "Uttar Pradesh",
"cityid": 557,
"cityname": "Sant Ravidas Nagar",
"pincode": 221304,
"birthdate": "14-11-1983",
"marriagedate": "2018-01-01",
"category": "-- Select --",
"mdrttick": false,
"profilepicture": "http://mdddddddtaupdate.in/profile_pic/12365.jpg",
"branchname": "ModelTown",
"branchcode": "123",
"licbranchid": 20596,
"division": "Delhi DO-II",
"licdivisionentryid": 51,
"password": "123456",
"employeename": "Shahnawaz Khan",
"empcontact": 999999999,
"empmail": "shane_madddian#yahoo.com",
"dealername": "Sachinder",
"deacontact": 999999999,
"deamail": "ddd#gamil.com",
"club_member": "-- Select --",
"lifeinsurance": false,
"nonlife": false,
"healthinsurance": false,
"mutualfunds": false,
"other": false,
"efrom": "A",
"edate": "2019-05-09 13:03:17.63214",
"website": "",
"maxdate": "01-01-2000 00:00:00"
}],
"Customer_Device": "",
"Customer_Event": "",
"All_Event": [{
"event_id": 6,
"event_name": "Test Event - 1",
"start_date": "01/06/2019",
"end_date": "02/06/2019",
"address_1": "Mumbai",
"address_2": "Mumbai",
"location_link": "https://goo.gl/maps/vTia6DQxwmiA5kvz6",
"pincode": 400060,
"state_id": 10,
"city_id": 355,
"sechudel": "Test",
"itinerary": "Test",
"edate": "2019-05-09T17:00:05.95592",
"eventimg": "http://zaidicorp.in/login/ProcessImage/636935218388448729.png"
}],
"Status": 2,
"Currentdate": "5/18/2019"
}
This is my POJO
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
import java.util.List;
public class OtpCheck implements Serializable {
#SerializedName("Success")
#Expose
private Boolean success;
#SerializedName("Message")
#Expose
private String message;
#SerializedName("Customer_Data")
#Expose
private List<CustomerData> customerData = null;
#SerializedName("Customer_Device")
#Expose
private String customerDevice;
#SerializedName("Customer_Event")
#Expose
private String customerEvent;
#SerializedName("All_Event")
#Expose
private List<EventDetail> getEventDetails = null;
#SerializedName("Status")
#Expose
private Integer status;
#SerializedName("Currentdate")
#Expose
private String currentdate;
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<CustomerData> getCustomerData() {
return customerData;
}
public void setCustomerData(List<CustomerData> customerData) {
this.customerData = customerData;
}
public String getCustomerDevice() {
return customerDevice;
}
public void setCustomerDevice(String customerDevice) {
this.customerDevice = customerDevice;
}
public String getCustomerEvent() {
return customerEvent;
}
public void setCustomerEvent(String customerEvent) {
this.customerEvent = customerEvent;
}
public List<EventDetail> getEventDetails() {
return getEventDetails;
}
public void setEventDetails(List<EventDetail> getEventDetails) {
this.getEventDetails = getEventDetails;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getCurrentdate() {
return currentdate;
}
public void setCurrentdate(String currentdate) {
this.currentdate = currentdate;
}
public static class EventDetail implements Serializable {
#SerializedName("event_id")
#Expose
private Integer eventId;
#SerializedName("event_name")
#Expose
private String eventName;
#SerializedName("start_date")
#Expose
private String startDate;
#SerializedName("end_date")
#Expose
private String endDate;
#SerializedName("address_1")
#Expose
private String address1;
#SerializedName("address_2")
#Expose
private String address2;
#SerializedName("location_link")
#Expose
private String locationLink;
#SerializedName("pincode")
#Expose
private Integer pincode;
#SerializedName("state_id")
#Expose
private Integer stateId;
#SerializedName("city_id")
#Expose
private Integer cityId;
#SerializedName("sechudel")
#Expose
private String sechudel;
#SerializedName("itinerary")
#Expose
private String itinerary;
#SerializedName("edate")
#Expose
private String edate;
#SerializedName("eventimg")
#Expose
private String eventimg;
public Integer getEventId() {
return eventId;
}
public void setEventId(Integer eventId) {
this.eventId = eventId;
}
public String getEventName() {
return eventName;
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public String getLocationLink() {
return locationLink;
}
public void setLocationLink(String locationLink) {
this.locationLink = locationLink;
}
public Integer getPincode() {
return pincode;
}
public void setPincode(Integer pincode) {
this.pincode = pincode;
}
public Integer getStateId() {
return stateId;
}
public void setStateId(Integer stateId) {
this.stateId = stateId;
}
public Integer getCityId() {
return cityId;
}
public void setCityId(Integer cityId) {
this.cityId = cityId;
}
public String getSechudel() {
return sechudel;
}
public void setSechudel(String sechudel) {
this.sechudel = sechudel;
}
public String getItinerary() {
return itinerary;
}
public void setItinerary(String itinerary) {
this.itinerary = itinerary;
}
public String getEdate() {
return edate;
}
public void setEdate(String edate) {
this.edate = edate;
}
public String getEventimg() {
return eventimg;
}
public void setEventimg(String eventimg) {
this.eventimg = eventimg;
}
}
public static class CustomerData implements Serializable {
#SerializedName("customerid")
#Expose
private Integer customerid;
#SerializedName("customername")
#Expose
private String customername;
#SerializedName("gender")
#Expose
private String gender;
#SerializedName("personalcontact")
#Expose
private Integer personalcontact;
#SerializedName("homecontact")
#Expose
private Integer homecontact;
#SerializedName("emailid")
#Expose
private String emailid;
#SerializedName("address")
#Expose
private String address;
#SerializedName("designation")
#Expose
private String designation;
#SerializedName("stateid")
#Expose
private Integer stateid;
#SerializedName("statename")
#Expose
private String statename;
#SerializedName("cityid")
#Expose
private Integer cityid;
#SerializedName("cityname")
#Expose
private String cityname;
#SerializedName("pincode")
#Expose
private Integer pincode;
#SerializedName("birthdate")
#Expose
private String birthdate;
#SerializedName("marriagedate")
#Expose
private String marriagedate;
#SerializedName("category")
#Expose
private String category;
#SerializedName("mdrttick")
#Expose
private Boolean mdrttick;
#SerializedName("profilepicture")
#Expose
private String profilepicture;
#SerializedName("branchname")
#Expose
private String branchname;
#SerializedName("branchcode")
#Expose
private String branchcode;
#SerializedName("licbranchid")
#Expose
private Integer licbranchid;
#SerializedName("division")
#Expose
private String division;
#SerializedName("licdivisionentryid")
#Expose
private Integer licdivisionentryid;
#SerializedName("password")
#Expose
private String password;
#SerializedName("employeename")
#Expose
private String employeename;
#SerializedName("empcontact")
#Expose
private Integer empcontact;
#SerializedName("empmail")
#Expose
private String empmail;
#SerializedName("dealername")
#Expose
private String dealername;
#SerializedName("deacontact")
#Expose
private Integer deacontact;
#SerializedName("deamail")
#Expose
private String deamail;
#SerializedName("club_member")
#Expose
private String clubMember;
#SerializedName("lifeinsurance")
#Expose
private Boolean lifeinsurance;
#SerializedName("nonlife")
#Expose
private Boolean nonlife;
#SerializedName("healthinsurance")
#Expose
private Boolean healthinsurance;
#SerializedName("mutualfunds")
#Expose
private Boolean mutualfunds;
#SerializedName("other")
#Expose
private Boolean other;
#SerializedName("efrom")
#Expose
private String efrom;
#SerializedName("edate")
#Expose
private String edate;
#SerializedName("website")
#Expose
private String website;
#SerializedName("maxdate")
#Expose
private String maxdate;
public Integer getCustomerid() {
return customerid;
}
public void setCustomerid(Integer customerid) {
this.customerid = customerid;
}
public String getCustomername() {
return customername;
}
public void setCustomername(String customername) {
this.customername = customername;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getPersonalcontact() {
return personalcontact;
}
public void setPersonalcontact(Integer personalcontact) {
this.personalcontact = personalcontact;
}
public Integer getHomecontact() {
return homecontact;
}
public void setHomecontact(Integer homecontact) {
this.homecontact = homecontact;
}
public String getEmailid() {
return emailid;
}
public void setEmailid(String emailid) {
this.emailid = emailid;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public Integer getStateid() {
return stateid;
}
public void setStateid(Integer stateid) {
this.stateid = stateid;
}
public String getStatename() {
return statename;
}
public void setStatename(String statename) {
this.statename = statename;
}
public Integer getCityid() {
return cityid;
}
public void setCityid(Integer cityid) {
this.cityid = cityid;
}
public String getCityname() {
return cityname;
}
public void setCityname(String cityname) {
this.cityname = cityname;
}
public Integer getPincode() {
return pincode;
}
public void setPincode(Integer pincode) {
this.pincode = pincode;
}
public String getBirthdate() {
return birthdate;
}
public void setBirthdate(String birthdate) {
this.birthdate = birthdate;
}
public String getMarriagedate() {
return marriagedate;
}
public void setMarriagedate(String marriagedate) {
this.marriagedate = marriagedate;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public Boolean getMdrttick() {
return mdrttick;
}
public void setMdrttick(Boolean mdrttick) {
this.mdrttick = mdrttick;
}
public String getProfilepicture() {
return profilepicture;
}
public void setProfilepicture(String profilepicture) {
this.profilepicture = profilepicture;
}
public String getBranchname() {
return branchname;
}
public void setBranchname(String branchname) {
this.branchname = branchname;
}
public String getBranchcode() {
return branchcode;
}
public void setBranchcode(String branchcode) {
this.branchcode = branchcode;
}
public Integer getLicbranchid() {
return licbranchid;
}
public void setLicbranchid(Integer licbranchid) {
this.licbranchid = licbranchid;
}
public String getDivision() {
return division;
}
public void setDivision(String division) {
this.division = division;
}
public Integer getLicdivisionentryid() {
return licdivisionentryid;
}
public void setLicdivisionentryid(Integer licdivisionentryid) {
this.licdivisionentryid = licdivisionentryid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmployeename() {
return employeename;
}
public void setEmployeename(String employeename) {
this.employeename = employeename;
}
public Integer getEmpcontact() {
return empcontact;
}
public void setEmpcontact(Integer empcontact) {
this.empcontact = empcontact;
}
public String getEmpmail() {
return empmail;
}
public void setEmpmail(String empmail) {
this.empmail = empmail;
}
public String getDealername() {
return dealername;
}
public void setDealername(String dealername) {
this.dealername = dealername;
}
public Integer getDeacontact() {
return deacontact;
}
public void setDeacontact(Integer deacontact) {
this.deacontact = deacontact;
}
public String getDeamail() {
return deamail;
}
public void setDeamail(String deamail) {
this.deamail = deamail;
}
public String getClubMember() {
return clubMember;
}
public void setClubMember(String clubMember) {
this.clubMember = clubMember;
}
public Boolean getLifeinsurance() {
return lifeinsurance;
}
public void setLifeinsurance(Boolean lifeinsurance) {
this.lifeinsurance = lifeinsurance;
}
public Boolean getNonlife() {
return nonlife;
}
public void setNonlife(Boolean nonlife) {
this.nonlife = nonlife;
}
public Boolean getHealthinsurance() {
return healthinsurance;
}
public void setHealthinsurance(Boolean healthinsurance) {
this.healthinsurance = healthinsurance;
}
public Boolean getMutualfunds() {
return mutualfunds;
}
public void setMutualfunds(Boolean mutualfunds) {
this.mutualfunds = mutualfunds;
}
public Boolean getOther() {
return other;
}
public void setOther(Boolean other) {
this.other = other;
}
public String getEfrom() {
return efrom;
}
public void setEfrom(String efrom) {
this.efrom = efrom;
}
public String getEdate() {
return edate;
}
public void setEdate(String edate) {
this.edate = edate;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
public String getMaxdate() {
return maxdate;
}
public void setMaxdate(String maxdate) {
this.maxdate = maxdate;
}
}
}
Expected BEGIN_ARRAY but was String at line 1 column 61 . $ customer.Data
Customer_Event is Arraylist
#SerializedName("Customer_Event")
#Expose
private List<CustomerEvent> customerEvent = null;
Gson Library expecting the Array first not the Object. there is nothing wrong in your code. but need to modify the JSON Structure here.
Try using this structure :
Note rootArray is starting point/ is used for your reference
rootArray[
// object
{
"Key": value,
"Key": value,
"Key": value
}
// array1
"Customer_Data":[
{
"Key": value,
"Key": value,
"Key": value
}
],
// array 2
"All_Event": [
{
"Key": value,
"Key": value,
"Key": value
}
]
]

Why i'm getting expected begin_array but was String

This is my json data
{
"Success": true,
"Message": "User Not Found",
"Customer_Data": "",
"Customer_Device": "",
"Customer_Event": "",
"All_Event": [
{
"event_id": 6,
"event_name": "Test Event - 1",
"start_date": "01/06/2019",
"end_date": "01/06/2019",
"address_1": "Mumbai",
"address_2": "Mumbai",
"location_link": "https://goo.gl/maps/vTia6DQxwmiA5kvz6",
"pincode": 400060,
"state_id": 10,
"city_id": 355,
"sechudel": "Test",
"itinerary": "Test",
"edate": "2019-05-09T17:00:05.95592",
"eventimg": "http://zaidicorp.in/login/ProcessImage/636935218388448729.png"
}
],
"Status": 1,
"Currentdate": "5/16/2019 11:40:54 AM"
}
this is my pojo file
package mytraining.com.mytraining.Pojo;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
import java.util.List;
public class OtpCheck implements Serializable {
#SerializedName("Success")
#Expose
private Boolean success;
#SerializedName("Message")
#Expose
private String message;
#SerializedName("Customer_Data")
#Expose
private List<CustomerData> customerData;
#SerializedName("Customer_Device")
#Expose
private List<CustomerDevice> customerDevice;
#SerializedName("Customer_Event")
#Expose
private List<CustomerEvent> customerEvent;
#SerializedName("Event")
#Expose
private List<EventDetail> eventDetails;
#SerializedName("Status")
#Expose
private Integer status;
#SerializedName("Currentdate")
#Expose
private String currentdate;
public List<EventDetail> getEventDetails() {
return eventDetails;
}
public void setEventDetails(List<EventDetail> eventDetails) {
this.eventDetails = eventDetails;
}
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<CustomerData> getCustomerData() {
return customerData;
}
public void setCustomerData(List<CustomerData> customerData) {
this.customerData = customerData;
}
public List<CustomerDevice> getCustomerDevice() {
return customerDevice;
}
public void setCustomerDevice(List<CustomerDevice> customerDevice) {
this.customerDevice = customerDevice;
}
public List<CustomerEvent> getCustomerEvent() {
return customerEvent;
}
public void setCustomerEvent(List<CustomerEvent> customerEvent) {
this.customerEvent = customerEvent;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getCurrentdate() {
return currentdate;
}
public void setCurrentdate(String currentdate) {
this.currentdate = currentdate;
}
/**
* Inner Class For Customer Data
*/
public static class CustomerData implements Serializable {
#SerializedName("cust_id")
#Expose
private Integer custId;
#SerializedName("cust_email")
#Expose
private String custEmail;
#SerializedName("customername")
#Expose
private String customername;
#SerializedName("personalcontact")
#Expose
private String personalcontact;
public Integer getCustId() {
return custId;
}
public void setCustId(Integer custId) {
this.custId = custId;
}
public String getCustEmail() {
return custEmail;
}
public void setCustEmail(String custEmail) {
this.custEmail = custEmail;
}
public String getCustomername() {
return customername;
}
public void setCustomername(String customername) {
this.customername = customername;
}
public String getPersonalcontact() {
return personalcontact;
}
public void setPersonalcontact(String personalcontact) {
this.personalcontact = personalcontact;
}
}
/**
* Inner Class For Customer Device
*/
public static class CustomerDevice implements Serializable {
#SerializedName("cust_id")
#Expose
private Integer custId;
#SerializedName("Cust_device")
#Expose
private String custDevice;
#SerializedName("bg_device_Brand")
#Expose
private String bgDeviceBrand;
#SerializedName("bg_device_model")
#Expose
private String bgDeviceModel;
#SerializedName("android_ver")
#Expose
private String androidVer;
#SerializedName("device_mac")
#Expose
private String deviceMac;
#SerializedName("opt")
#Expose
private Object opt;
public Integer getCustId() {
return custId;
}
public void setCustId(Integer custId) {
this.custId = custId;
}
public String getCustDevice() {
return custDevice;
}
public void setCustDevice(String custDevice) {
this.custDevice = custDevice;
}
public String getBgDeviceBrand() {
return bgDeviceBrand;
}
public void setBgDeviceBrand(String bgDeviceBrand) {
this.bgDeviceBrand = bgDeviceBrand;
}
public String getBgDeviceModel() {
return bgDeviceModel;
}
public void setBgDeviceModel(String bgDeviceModel) {
this.bgDeviceModel = bgDeviceModel;
}
public String getAndroidVer() {
return androidVer;
}
public void setAndroidVer(String androidVer) {
this.androidVer = androidVer;
}
public String getDeviceMac() {
return deviceMac;
}
public void setDeviceMac(String deviceMac) {
this.deviceMac = deviceMac;
}
public Object getOpt() {
return opt;
}
public void setOpt(Object opt) {
this.opt = opt;
}
}
/**
* Inner Class For Customer Event
*/
public static class CustomerEvent implements Serializable {
#SerializedName("ticket_no")
#Expose
private String ticketNo;
#SerializedName("seat_no")
#Expose
private String seatNo;
#SerializedName("event_name")
#Expose
private String eventName;
#SerializedName("start_date")
#Expose
private String startDate;
#SerializedName("end_date")
#Expose
private String endDate;
#SerializedName("location_link")
#Expose
private String locationLink;
#SerializedName("edate")
#Expose
private String edate;
public String getTicketNo() {
return ticketNo;
}
public void setTicketNo(String ticketNo) {
this.ticketNo = ticketNo;
}
public String getSeatNo() {
return seatNo;
}
public void setSeatNo(String seatNo) {
this.seatNo = seatNo;
}
public String getEventName() {
return eventName;
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public String getLocationLink() {
return locationLink;
}
public void setLocationLink(String locationLink) {
this.locationLink = locationLink;
}
public String getEdate() {
return edate;
}
public void setEdate(String edate) {
this.edate = edate;
}
}
/**
* Inner Class For Event Detail
*/
public static class EventDetail implements Serializable {
#SerializedName("event_id")
#Expose
private Integer eventId;
#SerializedName("event_name")
#Expose
private String eventName;
#SerializedName("start_date")
#Expose
private String startDate;
#SerializedName("end_date")
#Expose
private String endDate;
#SerializedName("address_1")
#Expose
private String address1;
#SerializedName("address_2")
#Expose
private String address2;
#SerializedName("location_link")
#Expose
private String locationLink;
#SerializedName("pincode")
#Expose
private Integer pincode;
#SerializedName("state_id")
#Expose
private Integer stateId;
#SerializedName("city_id")
#Expose
private Integer cityId;
#SerializedName("sechudel")
#Expose
private String sechudel;
#SerializedName("itinerary")
#Expose
private String itinerary;
#SerializedName("edate")
#Expose
private String edate;
#SerializedName("eventimg")
#Expose
private String eventimg;
public Integer getEventId() {
return eventId;
}
public void setEventId(Integer eventId) {
this.eventId = eventId;
}
public String getEventName() {
return eventName;
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public String getLocationLink() {
return locationLink;
}
public void setLocationLink(String locationLink) {
this.locationLink = locationLink;
}
public Integer getPincode() {
return pincode;
}
public void setPincode(Integer pincode) {
this.pincode = pincode;
}
public Integer getStateId() {
return stateId;
}
public void setStateId(Integer stateId) {
this.stateId = stateId;
}
public Integer getCityId() {
return cityId;
}
public void setCityId(Integer cityId) {
this.cityId = cityId;
}
public String getSechudel() {
return sechudel;
}
public void setSechudel(String sechudel) {
this.sechudel = sechudel;
}
public String getItinerary() {
return itinerary;
}
public void setItinerary(String itinerary) {
this.itinerary = itinerary;
}
public String getEdate() {
return edate;
}
public void setEdate(String edate) {
this.edate = edate;
}
public String getEventimg() {
return eventimg;
}
public void setEventimg(String eventimg) {
this.eventimg = eventimg;
}
}
}
this my is java calling class
private void eventDetial(Map<String, String> map) {
Call<OtpCheck> call = apiInterface.eventShow(map);
call.enqueue(new Callback<OtpCheck>() {
#Override
public void onResponse(Call<OtpCheck> call, Response<OtpCheck> response) {
List<OtpCheck.EventDetail> data = response.body().getEventDetails();
for (int i = 0; i < 1; i++) {
image = data.get(i).getEventimg();
Log.i("Data", "data");
Glide.with(getActivity()).load(image).into(imageView);
}
}
#Override
public void onFailure(Call<OtpCheck> call, Throwable t) {
Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
I tried multiple ways from StackOverflow but still not getting error
expected begin_array but was String
Firstly got Error from below field, this 3 field is String but you set ArrayList. But it will be String. Like below.
"Customer_Data": "",
"Customer_Device": "",
"Customer_Event": "",
#SerializedName("Customer_Data")
#Expose
private String customerData;
#SerializedName("Customer_Device")
#Expose
private String customerDevice;
#SerializedName("Customer_Event")
#Expose
private String customerEvent;
Also, Have no any event field. so need change from
#SerializedName("Event")
#Expose
private List<EventDetail> eventDetails;
To
#SerializedName("All_Event")
#Expose
private List<EventDetail> eventDetails;
Your problem is in your #SerializedName:
#SerializedName("Event")
#Expose
private List<EventDetail> eventDetails;
The tag SerializedName must have the name of the element of the json. Then you must changue it for:
#SerializedName("All_Event")
#Expose
private List<EventDetail> eventDetails;

null response usin Retrofit Library in android

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;
}
}

Categories