>我有一个链接和一个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
.
@GET("users/{username}")
如果基本 url https://www.youtube.com/watch?v=
,则通过为方法 getStringResponse()
进行改造创建的 url https://www.youtube.com/watch?v=users/EMyW7WvhEso
。
您的基本 URL 应https://www.youtube.com/
,并按如下方式定义您的 api:
@GET("watch")
Call<String> getVideo(@Query("v") String videoID);