问题描述
我用我的Android应用程序YouTube数据API V3。我还使用了改造提出请求的API。我能够与服务器,但我得到的响应进行沟通是不是能解析它。
I am using Youtube data API V3 in my android application. I am also using Retrofit for making requests to the API. I am able to communicate with the server but the response I am getting is not able to parse it.
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);
/////////////////////////////////////////////
}
从那里我打电话的Youtube API片段
Fragment from where I am calling Youtube 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();
}
});
}
}
常量类
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";
}
API客户端构建网址
API Client building the URL
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;
}
我得到的响应为空。当我提出从邮差GET请求(REST API)我得到的JSON。请告诉我如何解析Android中使用的改装YouTube数据API响应。
I am getting response as null. When I am making the GET request from the Postman(REST API) I am getting the JSON. Please tell me how to parse the Youtube Data API response in Android using retrofit.
由于提前
推荐答案
您使用 .addConverterFactory(GsonConverterFactory.create())
这将不返回的JSONObject
,但已经被解析JSON(通过GSON转换器)。
例如,如果你的JSON必须返回是这样的:
You used .addConverterFactory(GsonConverterFactory.create())
which won't return a JSONObject
but an already parsed json (via the Gson converter).For example if your json must return something like:
{
result: "searchResult"
}
然后,您将创建一个类结果
是这样的:
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);
/////////////////////////////////////////////
}
这篇关于在改造Android的YouTube数据API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!