我正在尝试使用retrofit2拨打api电话,这是我的代码
public class MainActivity extends Activity implements Callback<List<Repo>>{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ApiCLient apiCLient = new ApiCLient();
IApiService apiService = apiCLient.getApiService();
Call<List<Repo>> call = apiService.getRepos();
call.enqueue(this);
}
@Override
public void onResponse(Call<List<Repo>> call, Response<List<Repo>>
response) {
if (response.isSuccessful()) {
List<Repo> repos = response.body();
} else {
ResponseBody a = response.errorBody();
Log.d("asd", response.errorBody().toString());
}
}
@Override
public void onFailure(Call<List<Repo>> call, Throwable t) {
Log.d("asd", t.getLocalizedMessage());
}
}
我没有得到任何回复。此外,调试器未输入onResponse或onFailure方法。
ApiClient类
private IApiService mService;
static final String BASE_URL = "https://api.github.com";
public ApiCLient(){
Retrofit retrofit = null;
try {
Gson gson = new GsonBuilder().setLenient().create();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
} catch (Exception e){
e.printStackTrace();
}
mService = retrofit.create(IApiService.class);
}
public IApiService getApiService(){
return mService;
}
最后,IApiService接口
public interface IApiService {
@GET("https://api.github.com/repositories?since=364")
Call<List<Repo>> getRepos();
}
答案 0 :(得分:0)
你应该使用这段代码:
public interface IApiService {
@GET("repositories?since=364")
Call<List<Repo>> getRepos();
}
用于您的界面。 并使用
static final String BASE_URL = "https://api.github.com/";
用于BASE_URL。