0

I am new to android app development with java and I am trying to fetch data from an external api . However I get the error : Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $.The API returns a result in the form of :

[ {} ,{} , ... ]

where I have defined every object as a single result and in my API interface I want a list of these results .

Each object is in the form of :

{
 area: "athens",
  areaid: 1002,
  dailydose1: 276,
  dailydose2: 305,
  daydiff: -49,
  daytotal: 581,
  referencedate: "2021-05-27T00:00:00",
  totaldistinctpersons: 25446,
  totaldose1: 25446,
  totaldose2: 12098,
  totalvaccinations: 36960
}

My code :

CovidApi.java

public interface CovidApi {
    @GET("/")
    Call<List<CovidSingleResult>> getCovidData();

}

CovidSingleResult.java

public class CovidSingleResult {

    private int totalvaccinations;
    private int daytotal;
    private String referencedate;
    private int totaldose1;
    private int totaldose2;
    private String area;
    private int areaid;
    private int dailydose1;
    private int dailydose2;
    private  int daydiff;

    public String getArea() {
        return area;
    }

    public int getDailydose1() {
        return dailydose1;
    }

    public int getDaytotal() {
        return daytotal;
    }

    public int getDailydose2() {
        return dailydose2;
    }

    public String getReferencedate() {
        return referencedate;
    }


    public int getAreaid() {
        return areaid;
    }

    public int getTotaldose1() {
        return totaldose1;
    }

    public int getTotaldose2() {
        return totaldose2;
    }


    public int getDaydiff() {
        return daydiff;
    }

    public int getTotalvaccinations() {
        return totalvaccinations;
    }


}

Fragment.java where I call the api url

 resultText = root.findViewById(R.id.response); //text to set api result 

                Gson gson = new GsonBuilder()
                        .setLenient()
                        .create();

                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("https://data.gov.gr/api/v1/query/mdg_emvolio/date_from="+binding.inputFrom.getText()+"&date_to="+binding.inputTo.getText()+"/")
                        .addConverterFactory(GsonConverterFactory.create(gson))
                        .build();
                CovidApi covidApi = retrofit.create(CovidApi.class);
                Call<List<CovidSingleResult>> call = covidApi.getCovidData();
                call.enqueue(new Callback<List<CovidSingleResult>>() {
                    @Override
                    public void onResponse(Call<List<CovidSingleResult>> call, retrofit2.Response<List<CovidSingleResult>> response) {
                        if(!response.isSuccessful()){
                            resultText.setText("Code" + response.code());
                            return;
                        }else{
                            List<CovidSingleResult> covidSingleResults = response.body();
                            for(CovidSingleResult p : covidSingleResults){
                                String content = "";
                                content += "total vaccinations :" + p.getTotalvaccinations();

                                resultText.append(content);
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<List<CovidSingleResult>> call, Throwable t) {
                        resultText.setText(t.getMessage());
                    }
                });

I would appreciate your help

  • show me the real json response, `[ {} ,{} , ... ]` is not enough – Krishan Madushanka Jun 03 '21 at 14:56
  • @KrishanMadushanka I will now – Βαρβάρα Ξιάρχου Jun 03 '21 at 14:59
  • @KrishanMadushanka I just inserted it – Βαρβάρα Ξιάρχου Jun 03 '21 at 15:00
  • Isn't there any other parameters outside the object list? anyway the problem is with mapping.use a tool like [this](https://plugins.jetbrains.com/plugin/7834-dto-generator) to generate models which will generate the pojo class without any conflict – Krishan Madushanka Jun 03 '21 at 15:07
  • @KrishanMadushanka Your comment is irrelevant. The OP's mapping **is** fine (except of the field names convention that can be fixed using `@SerializedName`). Recreating DTOs using that tool will not resolve the issue. – terrorrussia-keeps-killing Jun 04 '21 at 04:49
  • @ΒαρβάραΞιάρχου Make sure your response is neither a valid JSON string (e.g. `"foo bar"`, note a string literal), nor an arbitrary non-JSON content Gson _may_ parse as if it were a string (e.g. `foo bar`, note that it is not a string literal hence not a JSON at all). The latter may happen if the server responds with a non-JSON error (say, an HTML document in case of error, some APIs may be designed like that). Inspect the response first: https://stackoverflow.com/questions/32514410/logging-with-retrofit-2 . – terrorrussia-keeps-killing Jun 04 '21 at 04:56
  • @ΒαρβάραΞιάρχου BTW, your question is a duplicate of dozens of questions since `Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $` can be found many many times at S.O. Making a search would allow you resolving your issue earlier. – terrorrussia-keeps-killing Jun 04 '21 at 04:57

1 Answers1

0

Error say`s that your response is object and your model in java is list please replace bellow code in api interface:

 Call<CovidSingleResult> getCovidData();
  • The OP's method declaration is fine (at least from the mapping perspective, not sure about the `Call` though), and the error does not say the response is an object, it clearly tells it is a string. – terrorrussia-keeps-killing Jun 04 '21 at 04:51