【问题标题】:Trying to process a JSON array that is received through POST method (Retrofit2)尝试处理通过 POST 方法接收的 JSON 数组 (Retrofit2)
【发布时间】:2023-03-31 09:20:01
【问题描述】:

我在尝试解析通过 POST 方法检索到的 JSON 数组时遇到了困难。我对此非常陌生,请原谅我的任何无知。

RequestInterface.java

public interface RequestInterface {

    @POST("index.php/")
    Call<Classes> queryclass(@Body ServerRequest request);

}

类.java

public class Classes {

    @SerializedName("classname")
    private String classname;
    @SerializedName("subject")
    private String subject;
    @SerializedName("classday")
    private String classday;
    @SerializedName("timestart")
    private String timestart;
    @SerializedName("timeend")
    private String timeend;

    public Classes(String classname, String subject, String classday, String timestart, String timeend){

        this.classname = classname;
        this.subject = subject;
        this.classday = classday;
        this.timestart = timestart;
        this.timeend = timeend;

    }

    public String getClassname() {
        return classname;
    }
}

ServerRequest.java

public class ServerRequest {

private String queryclass;
private User user;

public void setQueryClass(String queryclass) { this.queryclass = queryclass; }

public void setUser(User user) {
    this.user = user;
}

}

活动(改造)

User user = new User();
user.setEmail(email);
ServerRequest request = new ServerRequest();
request.setQueryClass(Constants.QUERYCLASSES_OPERATION);
request.setUser(user);
Call<Classes> response = requestInterface.queryclass(request);

response.enqueue(new Callback<Classes>() {

@Override
public void onResponse(Call<Classes> call, Response<Classes> response) {

        if(!response.isSuccessful()){
            tvData.setText(response.code());
            return;
        }

        Classes resp = response.body();

        String content = "";

        content += "code: " + response.code() + "\n";
        content += "classname: " + resp.getClassname() + "\n";

        tvData.setText(content);

    }

@Override
public void onFailure(Call<Classes> call, Throwable t) {

            Toast.makeText(getApplicationContext(),t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            Log.d("TAG",t.getLocalizedMessage());
        }

    });

我要显示的 JSON 数组(邮递员)

[
    {
        "classname": "PHYS1201",
        "subject": "PHYS12",
        "classday": "THU",
        "timestart": "18:00:00",
        "timeend": "20:00:00"
    },
    {
        "classname": "CHEM1202",
        "subject": "CHEM12",
        "classday": "FRI",
        "timestart": "12:00:00",
        "timeend": "14:00:00"
    }
]

它只是在应用程序运行时显示“classname: null”。对这么长且令人困惑的帖子感到抱歉,我们将不胜感激。


澄清: POST 请求正在发送一个电子邮件(id),它返回该特定 id 的类数组。这在邮递员中有效,给了我上面显示的数组,但由于某种原因,Retrofit 没有将响应识别为数组。我曾尝试使用 List,但它似乎将响应识别为对象,而不是数组。

【问题讨论】:

    标签: android arrays json post retrofit2


    【解决方案1】:

    如果要获取 JSON 数组值,请使用列表。

    像这样:

    public interface RequestInterface {
        @POST("index.php/")
        Call<List<Classes>> queryclass(@Body ServerRequest request);
    }
    

    【讨论】:

    • 当我使用列表时,改造无法识别响应并显示“预期 BEGIN_ARRAY 但找到 BEGIN_OBJECT”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多