android - 改造 body 返回空

标签 android json gson retrofit retrofit2

我正在使用 retrofit 来连接数据库,但 GSON 似乎没有解析数据。正在返回 JSON 但未创建响应主体,因此我无法与数据交互。有什么想法吗?

我的网址添加

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);
}}

用户.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 的 Activity

 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)

最佳答案

在这种情况下,您似乎没有检索到 User 对象的列表,而只是检索其中一个...这就是为什么它没有反序列化到您的 POJO 中的原因。您期望 UserResults 是一个列表,但您只是检索了一个对象。

Call<UserResults> call = ApiClient.get().Login(email,password);

应该变成:

Call<User> call = ApiClient.get().Login(email,password);

或者如果预期的行为是检索一个数组,那么你应该在服务器端处理以始终发送一个数组,即使只有一个元素......我为避免所有错误所做的是创建我的 pojo本网站:http://www.jsonschema2pojo.org/

关于android - 改造 body 返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38962255/

相关文章:

java - GSON 未将新字段保存到文件中

java - 放大位图

android - 如何检索和显示登录用户的数据 - Firestore

ios - 如何在 Json 请求中的参数中传递一个 ID 数组

json - Swift:将 JSON 数组中的每个项目拆分为单个文件

android - 改造 + GSON 解串器

php - JSON 错误 : Unterminated object on valid json text

android - 在Cordova 7、WebView上输入type ="file",Android 7.1只能直接打开文件浏览器,不能选择camera

javascript - 很难将 json 字符串从服务器转换为数组

java - Gson异常——de-gson复杂对象