当前位置: 首页 > 知识库问答 >
问题:

返回null的翻新正文

竺鸿骞
2023-03-14

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

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

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

共有1个答案

司徒翼
2023-03-14

您似乎没有检索User对象列表,在本例中只是检索一个...这就是为什么它没有反序列化到POJO中。您期望UserResults,这是一个列表,但您只检索了一个对象。

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

应成为:

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

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

 类似资料:
  • 我已经发布了一个请求,并在postman上测试它,响应在postman和android logcat上正确返回,但在改型时,它在正文中返回null 下面是响应的模型 当我发送请求时,响应将获得null响应,而errorbody内容将获得以下内容 [size=99 text={\r\n“errors”:{\r\n“driversapi”:[\r\n“order alredy assig…] 下面是预

  • 我有两个活动和两个布局。当我在第一个活动中显示列表时,一切都正常,并在单击时给出列表中项目的编号,但当我尝试在第二个活动中重复相同的内容时,它会告诉我RecycleServiceClickListener侦听器为空。 适配器: 第一项活动: 第二项活动: 错误: 我不明白为什么在第一种情况下,它正常处理单击,而在第二种情况下,它说RecyclerViewClickListener为null

  • 当我尝试用相同的设置在代码中创建OracleDataSource时,我可以正确地检索连接。我的出厂配置有什么问题?

  • 问题内容: 我正在使用Servlets&Struts2的混合情况下工作。 我正在调用HTTPServlet,并且正在处理数据。 然后,我需要调用struts API在DB中插入数据。 在那我也使用HTTPSession。 所以我打电话 要获取HttpRequest,然后在该struts动作类中进行会话。 Servlet正在调用Struts Action类。 但 始终返回NULL。 是因为它没有被网

  • 问题内容: 我使用osgi + cdi,并且有以下代码: 而且我有以下tasklist.fxml 但是parent.lookup(“#testComboBox”)返回null。怎么解释呢?我已经多次检查ID的名称。 问题答案: 您可以将所需的逻辑放入控制器类中,而不必使用仅在渲染场景之后才能使用的查找。您可以通过注释将元素从FXML文件注入到控制器类中。 查询通常不可靠,我建议避免使用它们。如果您

  • 问题内容: 这是我的课程正在测试: 现在我要模拟第1和2行,并返回模拟HttpResponse和代码。 我已经尝试过但是失败了: 始终返回null。我希望它返回的模拟对象。我已经阅读了与此错误相关的其他帖子,但不确定该如何处理。有人可以帮忙吗? 问题答案: 代替 你应该使用 您的测试中还存在一些问题:模拟构造函数不正确,并且根本不需要httpResponse。 更新 此代码对我而言正常工作: