我正在使用Retrofit与数据库进行连接,但是GSON似乎无法解析数据。JSON已经返回,但响应体未被创建,因此我无法与数据进行交互。有什么想法吗?
我的URL增加
我的URL增加
public interface APIPlug {
@FormUrlEncoded
@POST("repair_manager/v1/login")
Call<UserResults>Login(
@Field("email") String email,
@Field("password") String password
);
}
我的Retrofit构建器
public class ApiClient {
private static APIPlug REST_CLIENT;
private static final String API_URL = "http://192.168.1.85/";
static {
setupRestClient();
}
private ApiClient() {}
public static APIPlug get() {
return REST_CLIENT;
}
private static void setupRestClient() {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
//Uncomment these lines below to start logging each request.
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(logging);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
REST_CLIENT = retrofit.create(APIPlug.class);
}}
User.java
public class User {
private Boolean error;
private String name;
private String email;
private String apiKey;
private String createdAt;
public Boolean getError() {
return error;
}
public void setError(Boolean error) {
this.error = error;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getApiKey() {
return apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
}
使用GSON的活动
private void getUser() {
final String email = inputEmail.getText().toString().trim();
final String password = inputPassword.getText().toString().trim();
Call<UserResults> call = ApiClient.get().Login(email,password);
call.enqueue(new Callback<UserResults>() {
public void onFailure(Call<UserResults> call, Throwable t) {
Log.d("APIPlug", "Error Occured: " + t.getMessage());
pDialog.dismiss();
}
public void onResponse(Call<UserResults> call, Response<UserResults> response) {
Log.d("APIPlug", "Successfully response fetched" );
pDialog.dismiss();
user = response.body().results;
Log.d("APIPlug", String.valueOf(response.body().results));
session.setLogin(true);}});
用户结果
public class UserResults {
public List<User> results;}
OkHttp日志
08-15 21:18:32.753 13629-14006/com.example.maxwell.nfc_app D/OkHttp: Date: Mon, 15 Aug 2016 20:18:26 GMT
08-15 21:18:32.753 13629-14006/com.example.maxwell.nfc_app D/OkHttp: Server: Apache/2.4.18 (Win64) PHP/5.6.19
08-15 21:18:32.753 13629-14006/com.example.maxwell.nfc_app D/OkHttp: X-Powered-By: PHP/5.6.19
08-15 21:18:32.753 13629-14006/com.example.maxwell.nfc_app D/OkHttp: Content-Length: 148
08-15 21:18:32.753 13629-14006/com.example.maxwell.nfc_app D/OkHttp: Keep-Alive: timeout=5, max=100
08-15 21:18:32.753 13629-14006/com.example.maxwell.nfc_app D/OkHttp: Connection: Keep-Alive
08-15 21:18:32.753 13629-14006/com.example.maxwell.nfc_app D/OkHttp: Content-Type: application/json
08-15 21:18:32.753 13629-14006/com.example.maxwell.nfc_app D/OkHttp: {"error":false,"name":"Rachel Simon","email":"RachelSimon2@gmail.com","apiKey":"c5996a2076d7338987d8743effc56d58","createdAt":"2016-08-13 12:41:51"}
08-15 21:18:32.753 13629-14006/com.example.maxwell.nfc_app D/OkHttp: <-- END HTTP (148-byte body)
onResponse
中,你应该首先使用response.isSuccess()
进行一个if
语句。这个返回值是 true 吗? - fernandospr