0

Retrofit is throwing the above error. I have tried some of the solutions i saw here but none is working. Here is my code

public void getMaterials()
     {
         RestAdapter adapter = new RestAdapter.Builder().setEndpoint(Root_Url).build();
         materialAPI api = adapter.create(materialAPI.class);
         api.getMaterials(String.valueOf(categoryId), new Callback <List<materialClass>>() {
             @Override
             public void success(List<materialClass> list, Response response) {

                 materials = list;
                 showList();
                 customAdapter customAdapter = new customAdapter();
                 listView.setAdapter(customAdapter);
             }

             @Override
             public void failure(RetrofitError error) {
                 Log.i(TAG, "Error here"+error.toString()); 
             }
         });

     }





public class materialClass{
        private String courseTitle;
        private String courseAuthor;
        private String fileName;
        private String fileSize;
        private String description;
        private long Id;
        public String getMaterialName(){return courseTitle;}
        public String getAuthorName(){return  courseAuthor;}
        public String getFileName(){return fileName;}
        public String getFileSize(){return fileSize;}
        public String getFileDescription(){return description;}
        public long getId(){return  Id;}
        public void setMaterialName(String resourceName){this.courseTitle =resourceName;}
        public void setAuthorName(String authorName){this.courseAuthor = authorName;}
        public void setFileName(String fileName){this.fileName = fileName;}
        public void setFileSize(String fileSize){this.fileSize = fileSize;}
        public void setFileDescription(String fileDescription){this.description = fileDescription;}
        public  void setFileContent(byte[] resourceFile){this.resourceFile = resourceFile;}
        public void setId(long Id){this.Id = Id;}
}





public interface materialAPI {

    @GET("/Course/{Id}")
    public void getMaterials(@Path("Id") String Id,  Callback<List<materialClass>> response);

}

I have included all the code i'm implementimg. Below is an low the json response is supposed to look it. This is a test data by the way.

{
"Id": 1,
"courseTitle": "Testing",
"courseAuthor": "Just Testing",
"dateCreated": "2018-12-07T00:00:00",
"description": "Just to see if it's working",
"categoryName": "programming",
"fileName": "Android_Programming_Succinctly.pdf",
"fileSize": "3038893"
}

The Json is not in an array format and i guess that;s why retrofit is throwing the error.How to i pass this data to retrofit

Mcbaloo
  • 157
  • 5
  • 18

2 Answers2

1

There are 2 options you have.

The first one is you modify your Android code to accept a single JSON object instead of a list. You can do this by replacing all the List[materialClass] with just materialClass.

The other way is to modify your server code to return a JSON array instead of just a JSON object.

JSON objects begin with curly braces, like so {...} and a JSON array begins with a square bracket, like so [...]

BennyHawk
  • 172
  • 2
  • 11
1

Based on the test data provided

Change this

java

public interface materialAPI {
@GET("/Course/{Id}")
 public void getMaterials(@Path("Id") String Id, Callback<List<materialClass>> response);
}

To

public interface materialAPI {
    @GET("/Course/{Id}")
    public void getMaterials(@Path("Id") String Id,  Callback<materialClass> response);
}
Kishan Viramgama
  • 893
  • 1
  • 11
  • 23
Paul Okeke
  • 1,384
  • 1
  • 13
  • 19