【问题标题】:How to use the @GET method to make API call?如何使用@GET 方法进行API 调用?
【发布时间】:2019-05-26 18:15:57
【问题描述】:

我正在尝试调用 youtube 数据 API 以检索特定频道的订阅者数量。但我不知道如何实现 API 接口和定义端点,所以我将剩余的整个 URL 粘贴到 @GET 方法中。但是我的应用在启动时崩溃了。

我的完整网址是:https://www.googleapis.com/youtube/v3/channels?part=statistics&id=+UC-lHJZR3Gqxm24_Vd_AJ5Yw&key=AIzaSyAyON6YdgkFrtNHrGGs3IFS4groadJhhts

这是我的界面:

public interface ApiInterface 
{
    @GET("/channels?part=statistics&id=+UC-lHJZR3Gqxm24_Vd_AJ5Yw&key=AIzaSyAyON6YdgkFrtNHrGGs3IFS4groadJhhts")
    Call<Mainjson> getMainJson();
}

主要活动:

public class MainActivity extends AppCompatActivity 
{
    private Statistics statistics;
    private String subscribers;
    private TextView subscribersPreview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        subscribersPreview=(TextView) findViewById(R.id.textView3);

        ApiInterface service = ApiClient.getClient().create(ApiInterface.class);

        Call<Mainjson> call = service.getMainJson();

        call.enqueue(new Callback<Mainjson>() {
            @Override
            public void onResponse(Call<Mainjson> call, Response<Mainjson> response) {
                List<Items> items = response.body().getItems();
                statistics=items.get(0).getStatistics();
                subscribers=statistics.getSubscriberCount();
                subscribersPreview.setText(subscribers);
            }

            @Override
            public void onFailure(Call<Mainjson> call, Throwable t) {
                Toast.makeText(MainActivity.this,"Failed to retrieve data",Toast.LENGTH_SHORT).show();
            }
        });

    }
}

改造实例:

public class ApiClient {

    public static final String BASE_URL = "https://www.googleapis.com/youtube/v3";
    private static Retrofit retrofit = null;


    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

【问题讨论】:

  • 也许你没有在你的改造构建器中添加 httpClient 或 Okhttp(因为你正在使用.client() 发出 httprequest)。
  • 其次,您还应该在将文本应用到任何 textView 之前检查文本的 **nullness **,以便处理任何网络错误或系统错误
  • 使用这个作为baseURL:https://www.googleapis.com而不是https://www.googleapis.com/youtube/v3这个
  • 和终点应该是:/youtube/v3/channels
  • 查询参数为:?part=statistics&amp;id=+UC-lHJZR3Gqxm24_Vd_AJ5Yw&amp;key=AIzaSyAyON6YdgkFrtNHrGGs3IFS4groadJhhts

标签: android youtube-api retrofit


【解决方案1】:

BASE_URL 的问题。 BASE_URL 应以 / 结尾,并在接口方法的开头删除 /

public static final String BASE_URL = "https://www.googleapis.com/youtube/v3/";

@GET("channels?part=statistics&id=+UC-lHJZR3Gqxm24_Vd_AJ5Yw&key=AIzaSyAyON6YdgkFrtNHrGGs3IFS4groadJhhts")
Call<Mainjson> getMainJson();

【讨论】:

    【解决方案2】:

    实现使用 Android Fast 网络的最简单快捷的方法是:-

    Android Fast networking

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-15
      • 1970-01-01
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多