【问题标题】:How to resolve error Code 403 from Cowin Public API in android studio?如何解决 android studio 中 Cowin Public API 的错误代码 403?
【发布时间】:2021-10-02 12:31:39
【问题描述】:

这是 Cowin 公共 API 的链接:

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

我一直在尝试使用改造库从 android studio 中的第二个链接获取状态详细信息。但每次我运行该应用程序时,它都会显示错误 403。任何帮助将不胜感激!在下面添加我的代码:

  1. 关于创建活动:
    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 接口
@GET("v2/admin/location/states")
    Call<StateMainModel> getAllIndiaStates();
  1. StateMainModel 模型类
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;
    }

【问题讨论】:

    标签: android retrofit retrofit2 rest


    【解决方案1】:

    您首先需要创建一个 OkHttpClient 类的对象,然后您必须为其添加拦截器,因为它需要密钥才能访问 api

    【讨论】:

      【解决方案2】:

      您必须在请求中设置 User-Agent 才能获得成功的响应。请参阅下面的 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()
      

      【讨论】:

      • 好的,这是我今天遇到的新事物。你知道如何在 Java 中设置用户代理吗?
      • 在这里查看答案:stackoverflow.com/a/63996219/6147653
      • 谢谢哥们!!有效。顺便说一句,我们为什么需要使用它?
      • 他们只限制了浏览器的 API,所以我用浏览器用户代理尝试了它,它起作用了。
      【解决方案3】:

      这是对我有用的 Java 代码:

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

      【讨论】:

        猜你喜欢
        • 2020-12-11
        • 2020-10-03
        • 1970-01-01
        • 1970-01-01
        • 2017-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-05
        相关资源
        最近更新 更多