Retrofit android 不工作并且没有错误
Retrofit android not working and has no error
我刚刚为 rest api 调用实现了改造 android 库,但它没有工作并且没有错误。我的代码是
ApiInterface.java
public interface ApiInterface {
@POST("url")
void getLoginResponse(@Field("username") String username , @Field("password") String password,
@Field("clientId") String clientId , Callback<LoginResponse> cb);
}
RestClient.java
public class RestClient {
private static ApiInterface REST_CLIENT;
private static String BASE_URL = "base_url";
static {
setupRestClient();
}
private RestClient() {}
public static ApiInterface get() {
return REST_CLIENT;
}
private static void setupRestClient() {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(BASE_URL)
.build();
REST_CLIENT = restAdapter.create(ApiInterface.class);
}
}
在activity我打电话给
RestClient.get().getLoginResponse(usernameText, passwordText, clientId, new Callback<LoginResponse>() {
@Override
public void success(LoginResponse loginResponse, Response response) {
Toast.makeText(getApplicationContext(), loginResponse.getToken(), Toast.LENGTH_SHORT).show();
}
@Override
public void failure(RetrofitError error) {
}
});
并且在 AndroidManifest 中我设置了互联网权限。
如何将 RestClient 设为单例:
public class RestClient {
private static ApiInterface REST_CLIENT;
private static String BASE_URL = "base_url";
public RestClient() {}
public static ApiInterface getInstance() {
//if REST_CLIENT is null then set-up again.
if (REST_CLIENT == null) {
setupRestClient();
}
return REST_CLIENT;
}
private static void setupRestClient() {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(BASE_URL)
.build();
REST_CLIENT = restAdapter.create(ApiInterface.class);
}
}
那么当你想调用 api 时,你应该总是调用:
ApiInterface api = RestClient.getInstance();
api.callWhatApiYouWant
我回答晚了,但它对其他人有用,我更喜欢使用改造 2。
// Retrofit
compile 'com.squareup.retrofit2:retrofit:2.1.0'
// JSON Parsing
compile 'com.google.code.gson:gson:2.7'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
退出简单创建实例。
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
这里是关于改造 2 的详细解释 android 最好的例子,退出简单易懂。
http://al-burraq.com/retrofit-android-get-and-post-api-request-tutorial/
我刚刚为 rest api 调用实现了改造 android 库,但它没有工作并且没有错误。我的代码是
ApiInterface.java
public interface ApiInterface {
@POST("url")
void getLoginResponse(@Field("username") String username , @Field("password") String password,
@Field("clientId") String clientId , Callback<LoginResponse> cb);
}
RestClient.java
public class RestClient {
private static ApiInterface REST_CLIENT;
private static String BASE_URL = "base_url";
static {
setupRestClient();
}
private RestClient() {}
public static ApiInterface get() {
return REST_CLIENT;
}
private static void setupRestClient() {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(BASE_URL)
.build();
REST_CLIENT = restAdapter.create(ApiInterface.class);
}
}
在activity我打电话给
RestClient.get().getLoginResponse(usernameText, passwordText, clientId, new Callback<LoginResponse>() {
@Override
public void success(LoginResponse loginResponse, Response response) {
Toast.makeText(getApplicationContext(), loginResponse.getToken(), Toast.LENGTH_SHORT).show();
}
@Override
public void failure(RetrofitError error) {
}
});
并且在 AndroidManifest 中我设置了互联网权限。
如何将 RestClient 设为单例:
public class RestClient {
private static ApiInterface REST_CLIENT;
private static String BASE_URL = "base_url";
public RestClient() {}
public static ApiInterface getInstance() {
//if REST_CLIENT is null then set-up again.
if (REST_CLIENT == null) {
setupRestClient();
}
return REST_CLIENT;
}
private static void setupRestClient() {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(BASE_URL)
.build();
REST_CLIENT = restAdapter.create(ApiInterface.class);
}
}
那么当你想调用 api 时,你应该总是调用:
ApiInterface api = RestClient.getInstance();
api.callWhatApiYouWant
我回答晚了,但它对其他人有用,我更喜欢使用改造 2。
// Retrofit
compile 'com.squareup.retrofit2:retrofit:2.1.0'
// JSON Parsing
compile 'com.google.code.gson:gson:2.7'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
退出简单创建实例。
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
这里是关于改造 2 的详细解释 android 最好的例子,退出简单易懂。 http://al-burraq.com/retrofit-android-get-and-post-api-request-tutorial/