android中的Youtube数据api及其改进

android中的Youtube数据api及其改进,android,json,api,youtube,retrofit,Android,Json,Api,Youtube,Retrofit,我正在android应用程序中使用Youtube数据API V3。我还使用改装来请求API。我能够与服务器通信,但是我得到的响应无法解析它 public interface APIService { //////////////////////// Auth /////////////////// @GET("/youtube/v3/search") Call<JSONObject> getYoutbeFeeds(@Query("key") String d

我正在android应用程序中使用Youtube数据API V3。我还使用改装来请求API。我能够与服务器通信,但是我得到的响应无法解析它

public interface APIService {

    //////////////////////// Auth ///////////////////
    @GET("/youtube/v3/search")
    Call<JSONObject> getYoutbeFeeds(@Query("key") String developerKey, @Query("channelId") String channelId,@Query("part") String id,@Query("alt") String alt);
    /////////////////////////////////////////////
}
构建URL的API客户端

public class APIClient {
    public static APIService getAPIService(String baseUrl) {

    OkHttpClient client = new OkHttpClient();
    client.interceptors().add(new LoggingInterceptor());

    if (apiService == null) {
        Retrofit restAdapter = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();

        apiService = restAdapter.create(APIService.class);
    }
    return apiService;
}
我得到的响应为空。当我从Postman(RESTAPI)发出GET请求时,我得到的是JSON。请告诉我如何使用改型在Android中解析Youtube数据API响应


提前感谢您使用了
.addConverterFactory(GsonConverterFactory.create())
,它不会返回
JSONObject
,而是返回已解析的json(通过Gson转换器)。 例如,如果json必须返回如下内容:

{
  result: "searchResult"
}
然后您将创建一个类
Result
,如下所示:

public class Result {

  @SerializedName("result")
  private String result;

  public String getResult(){
    return result;
  }

  public void setResult(String result) {
    this.result = result;
  }
}
public interface APIService {

    //////////////////////// Auth ///////////////////
    @GET("/youtube/v3/search")
    Call<Result> getYoutbeFeeds(@Query("key") String developerKey, @Query("channelId") String channelId,@Query("part") String id,@Query("alt") String alt);
    /////////////////////////////////////////////
}
您的
ApiService
将如下所示:

public class Result {

  @SerializedName("result")
  private String result;

  public String getResult(){
    return result;
  }

  public void setResult(String result) {
    this.result = result;
  }
}
public interface APIService {

    //////////////////////// Auth ///////////////////
    @GET("/youtube/v3/search")
    Call<Result> getYoutbeFeeds(@Query("key") String developerKey, @Query("channelId") String channelId,@Query("part") String id,@Query("alt") String alt);
    /////////////////////////////////////////////
}
公共接口服务{
////////////////////////认证///////////////////
@获取(“/youtube/v3/search”)
调用getyoutbeeds(@Query(“key”)String developerKey、@Query(“channelId”)String channelId、@Query(“part”)String id、@Query(“alt”)String alt);
/////////////////////////////////////////////
}

谢谢E-Kami,但我仍然无法解析响应。作为回应,我想要一个频道相关的所有视频链接,但我无法得到,真正的问题在于你编写请求方法的方式。当您在改装初始化中使用
addConverterFactory(GsonConverterFactory.create())
时,不能期望它返回
JSONObject
。至少您可以尝试获取一个
JsonObject
(从gson包中)。本教程将简单明了地介绍如何使用改型2.0。我希望它能有所帮助