I am new to android programming and can anyone help me or point out why its giving me this error I want to fetch some data from the server such as under the Hardware json and get the names and status, but when i call api its shows me this.
Asked
Active
Viewed 167 times
-1
-
how does your json look like? post a sample response – Muhannad Fakhouri Nov 19 '19 at 07:03
-
@MuhannadFakhouri I've edited my post kindly check it above. – Yen Bico Nov 19 '19 at 07:05
-
3Possible duplicate of [Retrofit2 Android: Expected BEGIN\_ARRAY but was BEGIN\_OBJECT at line 1 column 2 path $](https://stackoverflow.com/questions/36177629/retrofit2-android-expected-begin-array-but-was-begin-object-at-line-1-column-2) – Saurabh Thorat Nov 19 '19 at 07:05
-
There you go ... your response starts with {, but you're considering it to be an array – Muhannad Fakhouri Nov 19 '19 at 07:06
-
i see. how do i fix it tho? @MuhannadFakhouri – Yen Bico Nov 19 '19 at 07:08
-
See my answer below – Muhannad Fakhouri Nov 19 '19 at 07:09
5 Answers
0
Your response return an object instead of array.
Instead of
@GET("system_monitor")
Call<List<ObjectList>> getHardware();
use
@GET("system_monitor")
Call<ObjectList> getHardware();
And then use it like below:
Call<ObjectList> call = webRequestAPI.getHardware();
call.enqueue(new Callback<ObjectList>() {
@Override
public void onResponse(Call<ObjectList> call, Response<ObjectList> response) {
if (!response.isSuccessful()) {
textViewHardwareName.setText("Code: " + response.code());
return;
}
ObjectList system_monitor = response.body();
...
}
@Override
public void onFailure(Call<ObjectList> call, Throwable t) {
textViewHardwareName.setText(t.getMessage());
}
});

Md. Asaduzzaman
- 14,963
- 2
- 34
- 46
0
Change the line
public void onResponse(Call<List<ObjectList>> call, Response<List<ObjectList>> response) {
to
public void onResponse(Call<List<ObjectList>> call, Response<ObjectList> response) {

Muhannad Fakhouri
- 1,468
- 11
- 14
0
As per your code, you are expecting response as List. But Actual response is object. So, you need to generate model class based on your response and set in code for output.
Your Model should be like :
public class Application {
ArrayList<Object> hardware = new ArrayList<Object>();
Header HeaderObject;
ArrayList<Object> software = new ArrayList<Object>();
// Getter Methods
public Header getHeader() {
return HeaderObject;
}
// Setter Methods
public void setHeader( Header headerObject ) {
this.HeaderObject = headerObject;
}
}
public class Header {
Stamp StampObject;
private String frame_id;
private float seq;
// Getter Methods
public Stamp getStamp() {
return StampObject;
}
public String getFrame_id() {
return frame_id;
}
public float getSeq() {
return seq;
}
// Setter Methods
public void setStamp( Stamp stampObject ) {
this.StampObject = stampObject;
}
public void setFrame_id( String frame_id ) {
this.frame_id = frame_id;
}
public void setSeq( float seq ) {
this.seq = seq;
}
}
public class Stamp {
private float secs;
private float nsecs;
// Getter Methods
public float getSecs() {
return secs;
}
public float getNsecs() {
return nsecs;
}
// Setter Methods
public void setSecs( float secs ) {
this.secs = secs;
}
public void setNsecs( float nsecs ) {
this.nsecs = nsecs;
}
}
Then change below line :
public void onResponse(Call<List<ObjectList>> call, Response<Application> response) {

Samir Bhatt
- 3,041
- 2
- 25
- 39
0
Change this:
@GET("system_monitor")
Call<List<ObjectList>> getHardware();
to
@GET("system_monitor")
Call<ObjectList> getHardware();

faranjit
- 1,567
- 1
- 15
- 22
0
The best thing for your scenario hardware and Software are as objects , which have two property 1.Name 2. Object status.
So I recommend you to create a class name as System and put there these two variables so finally your class looks like :
Class System
{
String object_name;
boolean object_status;
}
and your getter setter . And update your model class like this
@SerializedName("hardware")
@Expose
public List<System> hardware;
@SerializedName("software")
@Expose
public List<System> software;
and change your retrofit response holder as.
public void onResponse(Call<List<ObjectList>> call, Response<ObjectList>
response) {

Ashish srivastava
- 628
- 4
- 17