我使用改造来连接数据库,但GSON似乎没有解析数据。正在返回JSON,但未创建响应正文,因此我无法与数据交互。有什么想法吗?
我的网址添加
public interface APIPlug {
@FormUrlEncoded
@POST("repair_manager/v1/login")
Call<UserResults>Login(
@Field("email") String email,
@Field("password") String password
);
}
我的改造构建器
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 log
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)
答案 0 :(得分:1)
正如JaythaKing指出的那样,您接收的是JSONObject而不是JSONArray作为响应。所以使用Call而不是Call。
从Retrofit开始,我建议你仔细阅读:
学习JSON基础知识和解析:
答案 1 :(得分:0)
作为选项,您可以收到Jason的回复,然后通过any方法将其解析或反序列化到您的班级。
public interface APIPlug {
@FormUrlEncoded
@POST("repair_manager/v1/login")
Call<JsonElement>Login(
@Field("email") String email,
@Field("password") String password
);
请求:
call.enqueue(new Callback<JsonElement>() {
@Override
public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
if(response.isSuccessful()){
JsonElement jsonElement = response.body();
JsonObject objectWhichYouNeed = jsonElement.getAsJsonObject();
//use any json deserializer to convert to your class.
}
else{
System.out.println(response.message());
}
}
@Override
public void onFailure(Call<JsonElement> call, Throwable t) {
System.out.println("Failed");
}
});