如何解决android studio中cowin公共api的错误代码403?

soat7uwm  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(455)

以下是cowin公共api的链接:
https://apisetu.gov.in/public/marketplace/api/cowin#/
https://cdn-api.co-vin.in/api/v2/admin/location/states
我一直在尝试使用改型库从android studio的第二个链接获取状态详细信息。但每次我运行应用程序时,它都会显示错误403。任何帮助都将不胜感激!!在下面添加我的代码:
关于创建活动:

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();
            }
        });

api接口

@GET("v2/admin/location/states")
    Call<StateMainModel> getAllIndiaStates();

StateMain模型类

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;
    }
mrphzbgm

mrphzbgm1#

您必须在请求中设置用户代理才能获得成功的响应。请参阅以下kotlin代码:

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()

相关问题