0

Here's the link to the Cowin Public API:

  1. https://apisetu.gov.in/public/marketplace/api/cowin#/
  2. https://cdn-api.co-vin.in/api/v2/admin/location/states

I have been trying to get the state details from the 2nd link in android studio using retrofit library. But every time I run the app it's showing error 403. Any help would be appreciated!! Adding my code below:

  1. On create activity:
    Retrofit retrofit = new Retrofit.Builder().baseUrl("https://cdn-api.co-vin.in/api/").addConverterFactory(GsonConverterFactory.create()).build();

        CovidAPI covidAPI = retrofit.create(CovidAPI.class);

        Call<StateMainModel> call = covidAPI.getAllIndiaStates();

        call.enqueue(new Callback<StateMainModel>() {
            @Override
            public void onResponse(Call<StateMainModel> call, Response<StateMainModel> response) {
                if(!response.isSuccessful())
                {
                    Toast.makeText(getApplicationContext(),"Error!!Code: "+response.code()+response.toString(),Toast.LENGTH_SHORT).show();
                    tv.setText(response.toString());
                    return;
                }
                else
                {
                    StateMainModel stateMainModel = response.body();

                    int size = stateMainModel.getStates().size();

                    for(int i=0;i<size;i++)
                    {
                        stateList.add(stateMainModel.getStates().get(i).getState_name());
                    }

                    stateAutoCompleteTextView.setAdapter(stateAdapter);
                }

            }

            @Override
            public void onFailure(Call<StateMainModel> call, Throwable t) {
                Toast.makeText(getApplicationContext(),"Error!! Response: "+t.getMessage(),Toast.LENGTH_LONG).show();
            }
        });
  1. API Interface
@GET("v2/admin/location/states")
    Call<StateMainModel> getAllIndiaStates();
  1. StateMainModel Model Class
private ArrayList<StateIdNameModel> states;
    private Integer ttl;

    public StateMainModel() {
    }

    public StateMainModel(ArrayList<StateIdNameModel> states,Integer ttl) {
        this.states = states;
        this.ttl = ttl;
    }

    public ArrayList<StateIdNameModel> getStates() {
        return states;
    }

    public void setStates(ArrayList<StateIdNameModel> states) {
        this.states = states;
    }

    public Integer getTtl() {
        return ttl;
    }

    public void setTtl(Integer ttl) {
        this.ttl = ttl;
    }
Glitch07
  • 87
  • 11

3 Answers3

1

You first need to create an object of class OkHttpClient and then you will have to add interceptor to it since it requires key to access the api

samheist
  • 21
  • 2
0

You have to set the User-Agent in your request to get a successful response. See the below Kotlin code:

val okHttpClientBuilder = OkHttpClient.Builder()
val logger = HttpLoggingInterceptor().apply { level = Level.BODY}
okHttpClientBuilder.addNetworkInterceptor { chain ->
                    chain.proceed(chain.request()
                        .newBuilder()
                        .header("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36")
                        .build())
}.addNetworkInterceptor(logger)
val retrofit = Retrofit.Builder()
                .baseUrl("https://cdn-api.co-vin.in/api/v2/")
                .client(okHttpClientBuilder.build())
                .addConverterFactory(MoshiConverterFactory.create())
                .build()
Alpha 1
  • 4,118
  • 2
  • 17
  • 23
0

Here's the Java Code that worked for me:

OkHttpClient client = new OkHttpClient();

        client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
            @Override
            public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {

                Request originalRequest = chain.request();
                Request requestWithUserAgent = originalRequest.newBuilder().header("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36").build();
                return  chain.proceed(requestWithUserAgent);


            }
        }).build();
Glitch07
  • 87
  • 11