reformapi调用(java)返回空对象

bybem2ql  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(418)

这是我的第一个java应用程序。我做了一个应用程序与离子/Angular ,我调用这个api和一切工作正常。现在,我正试图为智能手表(watch os)做同样的事情,我使用了带有gson 2.4.0的改进版2.0版本2.4.0

implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'

我真的没有找到任何帮助我解决问题的教程/文档。
这是我的密码
mainactivity.java:

Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("https://new.scoresaber.com/")
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            API api = retrofit.create(API.class);

            Call<JSONObject> call = api.getPosts();

            call.enqueue(new Callback<JSONObject>() {
                @Override
                public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
                    //When i debug then the response.body() is just empty
                    if (response.isSuccessful()) {
                        JSONObject res = response.body();
                        countryRank.setText(res.toString());
                    }
                    else{
                        countryRank.setText("Response was not successful");
                    }
                }

                @Override
                public void onFailure(Call<JSONObject> call, Throwable t) {
                    countryRank.setText("error: " + t.getMessage());
                }
            });

获取请求.java

public class GetRequest {

@SerializedName("playerName")
private String playerName;
private String rank;
private String countryRank;
private String pp;

public String getPlayerName() {
    return playerName;
}

public String getRank() {
    return rank;
}

public String getCountryRank() {
    return countryRank;
}

public String getPp() {
    return pp;
}
}

api.java文件

import org.json.JSONObject;
import retrofit2.Call;
import retrofit2.http.GET;

public interface API {

    @GET("api/player/76561198280372610/basic")
    Call<JSONObject> getPosts();

}

感谢您的帮助!
n

esyap4oy

esyap4oy1#

只需为根对象再添加一个pojo:

public class PlayerResponse {
    private GetRequest playerInfo;

    public GetRequest getPlayerInfo() {
        return playerInfo;
    }
}

把你所有的 JSONObject 与…在一起 PlayerResponse

相关问题