私はretrofitを使ってデータベースに接続しますが、GSONはデータを解析していないようです。 JSONが返されていますが、レスポンスボディが作成されていないため、データと対話できません。何か案は?改造体がnullを返す
私のURLのほか
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;}
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":"[email protected]","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)
UserクラスとUserResultsは異なっていますか? – fernandospr
ユーザーの結果:public class UserResults { public list結果; } –
Mugzwell
'' 'onResponse'''の中で、' '' response.isSuccess() '' 'を使って' '' 'if'''文を実行する必要があります。これは本当ですか? – fernandospr