Youtube数据api在android中改装

时间:2016-01-02 14:15:44

标签: android json api youtube retrofit

我在我的Android应用程序中使用Youtube数据API V3。我也在使用Retrofit向API发出请求。我能够与服务器通信,但我得到的响应无法解析它。

public class YoutubeFeedsFragment extends BaseFragment{

private APIService apiService = APIClient.getAPIService(Constants.YOUTUBE_FEEDS_URL);;


@Override
public void onStart() {
    super.onStart();
    Call<JSONObject> callBack = apiService.getYoutbeFeeds(Constants.DEVELOPER_KEY,Constants.CHANNEL_ID,"id","json");
    Log.i("data", "service called");
    Log.i("data", callBack.toString());
    callBack.enqueue(new retrofit.Callback<JSONObject>() {
        @Override
        public void onResponse(Response<JSONObject> response) {
            if(response.body() != null) {
                Log.i("data", "not null");

            }
            Log.i("data",response.data());

        @Override
        public void onFailure(Throwable t) {
            Toast.makeText(getActivity(),"sorry connection problem",Toast.LENGTH_LONG).show();
            }
        });
    }
}

我称之为Youtube API的片段

public class Constants {
    public static final String CHANNEL_ID = "XXXXXXXXXXXXXXXXX";
    public static final String DEVELOPER_KEY = "AAAAAAAAAAAAAAAAAAAAA";

    public static String YOUTUBE_FEEDS_URL = "https://www.googleapis.com";
}

常量类

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

构建网址的API客户端

 res = self._sync_dir()
  File "/var/lib/openshift/56856e180c1e6670500000bb/app-root/runtime/srv/python/lib/python2.7/site-packages/pyftpsync-1.0.3-py2.7.egg/ftpsync/synchronizers.py", line 375, in _sync_dir
    remote_entries = self.remote.get_dir()
  File "/var/lib/openshift/56856e180c1e6670500000bb/app-root/runtime/srv/python/lib/python2.7/site-packages/pyftpsync-1.0.3-py2.7.egg/ftpsync/ftp_target.py", line 270, in get_dir
    self.ftp.retrlines("MLSD", _addline)
  File "/var/lib/openshift/56856e180c1e6670500000bb/app-root/runtime/srv/python/lib/python2.7/ftplib.py", line 443, in retrlines
    callback(line)
  File "/var/lib/openshift/56856e180c1e6670500000bb/app-root/runtime/srv/python/lib/python2.7/site-packages/pyftpsync-1.0.3-py2.7.egg/ftpsync/ftp_target.py", line 263, in _addline
    raise NotImplementedError
NotImplementedError

我的回复为null。当我从邮递员(REST API)发出GET请求时,我收到了JSON。请告诉我如何使用改造在Android中解析Youtube Data API响应。

先谢谢

1 个答案:

答案 0 :(得分:0)

您使用了.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;
  }
}

您的ApiService将如下所示:

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