0

I am using Covid Tracker Rapid API for a recipe app project. The problem occurs when trying to making GET requests to the Rapid API. The error shows expected begin array but was begin object at line 1 column 2 path $

This is my Retrofit API Class

public interface Api {

    String BASE_URL = "https://who-covid-19-data.p.rapidapi.com/api/data/";


    @GET("names")
    Call < List < Model > > modelData( @Header("X-RapidAPI-Host") String api,
                                       @Header("X-RapidAPI-Key") String apiKey );
}

This is my model class

public class Model {

    String names;

    public Model ( String names ) {
        this.names = names;
    }

    public String getNames () {
        return names;
    }

    public void setNames ( String names ) {
        this.names = names;
    }
}

This is my main activity class

public class MainActivity extends AppCompatActivity {
    TextView textView;

    @Override
    protected void onCreate ( Bundle savedInstanceState ) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
        textView=findViewById ( R.id.txtHell );
        
        loadData();
    }

    private void loadData () {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Api.BASE_URL)
                .addConverterFactory( GsonConverterFactory.create())
                .addConverterFactory ( ScalarsConverterFactory.create () )
                .build();

        Api api = retrofit.create(Api.class);

        Call < List < Model > > call=api.modelData ("","");
        call.enqueue ( new Callback < List < Model > > ( ) {
            @Override
            public void onResponse ( Call < List < Model > > call , Response < List < Model > > response ) {

                if(!response.isSuccessful ()){
                    textView.setText ( "Code"+response.code () );
                }else{
                    List<Model> models=response.body ();
                    for(Model model:models){
                        String data="";
                        data +="Names :"+ model.getNames ();

                        textView.append (data  );
                    }
                }
            }

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

please help me

SSK
  • 3,444
  • 6
  • 32
  • 59
  • Your response, must wrapped with [] instead of {}, if you using php, replace json_decode(data) with print_r(data) –  Mar 01 '21 at 07:39

1 Answers1

0

Everything is good with API and RapidAPI. I believe you just need to change the interface like this.

@GET("names")
Call <ListModel> modelData( @Header("X-RapidAPI-Host") String api,
                            @Header("X-RapidAPI-Key") String apiKey );
Pratham
  • 497
  • 3
  • 7