0
votes

I am new to Retrofit . i am trying to call api using base url,but i getting error while loading function.there is a object and inside that there is two different arrays name animal and birds.i need to call this array from the object and need to load in gridview adapter. this is how i calling the function in my fragment.

 My error.

java.lang.IllegalArgumentException: Non-body HTTP method cannot contain @Body.
    for method GetDataService.getAll
  1. This is my JSON response

    {
       "animals": [
       {
          "animalName": "String",
          "animalID": "integer",
    
    }
    
     ],
        "birds": [
      {
        "birdsId": "integer",
        "birdsname": "string
     }
    
    ],
    
     "ID": "integer"
    }
    
  2. Calling GET in fragment

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://demoapp.in/")
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    
    GetDataService service = retrofit.create(GetDataService .class);
    Call<TableResponse> call= service.getAll(new TableResponse());
    call.enqueue(new Callback<TableResponse>() {
        @Override
        public void onResponse(Call<TableResponse> call, Response<TableResponse> response) {
    
            String passKey = response.body().getPassKey();
            List<Table> TableName = response.body().getTables();
            String[] TableNameArray = new String[TableName.size()];
            for (int i = 0; i < TableName.size(); i++) {
    
                TableNameArray[i] = TableName.get(i).getTableName();
    
            }
    
            gridView.setAdapter(new TableViewAdapter(getContext(), TableNameArray));
    
        }
    
        @Override
        public void onFailure(Call<TableResponse> call, Throwable t) {
    
        }
    });
    

3.TableResponse,java class in this fie is generated using jsonschema2pojo

@SerializedName("animal")
@Expose
private List<Table> animal = null;
   ......
  public TableResponse(){
    this.animal= animal;
    this.birds= birds;
    this.ID = ID;
   }


public List<Table> getTables() {
    return tables;
}

  -------

4.GetDataService.java

@GET("Animal.ashx")
Call<TableResponse> getAll(@Body TableResponse tableResponse);
1
did you try change Call<TableResponse> getAll(@Body TableResponse tableResponse) to Call<TableResponse> getAll()?Công Hải
when you pass parameters as @Body it not template of GETCông Hải
@CôngHải while calling this fumction Call<TableResponse> getAll() error is gone but it didnt fetching datasherin
Is your get api need parammeter?Công Hải
@sherin you can't have body payload when using get request, body is only used in Post/Put requestmangkool

1 Answers

0
votes

@Headers("Content-Type: application/json")

@GET("helper-url")

fun getHelperUrl(

@Query("api_token") apiToken: String,

@Query("authtype") authType: String,

@Query("channel") channel: String

): Call<ResponseHelperUrl>