我有一个链接和一个API:
@GET("users/{username}")
Call<String> getStringResponse(@Path("username") String username);
public class APIClientDetail {
public Retrofit getRetrofit(String baseUrl) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
// add other factories here, if needed.
.build();
return retrofit;
}
}
我打电话:
APIClientDetail apiClientDetail= new APIClientDetail();
Retrofit retrofit= apiClientDetail.getRetrofit("https://www.youtube.com/watch?v=");
GitHubService scalarService = retrofit.create(GitHubService.class);
Call<String> stringCall = scalarService.getStringResponse("EMyW7WvhEso");
stringCall.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
String responseString = response.body();
// todo: do something with the response string
modelInterface.dataSuccessful();
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
modelInterface.dataError();
}
});
但它不起作用,而不是功能onResponse
。
答案 0 :(得分:1)
@GET("users/{username}")
如果您的基本网址为https://www.youtube.com/watch?v=
,则通过方法getStringResponse()
的改造创建的网址为https://www.youtube.com/watch?v=users/EMyW7WvhEso
。
您的基本网址应为https://www.youtube.com/
并定义您的api,如下所示:
@GET("watch")
Call<String> getVideo(@Query("v") String videoID);