首页 > 解决方案 > Android Retrofit 响应不等于 Postman 响应

问题描述

我正在尝试使用 Instagram WebService 获取数据,但在 Postman 中我得到了以下回复:

{
   "access_token":"1642576492.8d98317.2072b80503d14218bc5fcdbb26982d95",
   "user":{
      "id":"1642576492",
      "username":"hesamilyaei",
      "profile_picture":"https:\/\/scontent.cdninstagram.com\/vp\/69b6c110a166eb415ff1a10b8dbe9fe3\/5C0B0872\/t51.2885-19\/s150x150\/17818811_292597561174489_2362514411194679296_a.jpg",
      "full_name":"hesam ilyaei",
      "bio":"\ud83d\udcbbAndroid \u2699\ufe0fDeveloper\nIn \u2764\n\u062f\u0644 \u0631\u0627 \u0633\u0631\u0650 \u0634\u0648\u0642\u06cc...\n                \u0627\u06af\u0631\u0645 \u0647\u0633\u062a...\n                            \u062a\u064f\u0648 \u0622\u0646\u06cc...!",
      "website":"",
      "is_business":false
   }
}

但是在改造响应中,我在登录时得到了这个 br HTML 标记response.body

当我用Model类调用服务时,它给了我这个错误:

Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

当我通过String回调调用服务时,它会在响应正文中显示 br 标记。

这是我的模型类:

public class ViewModelInstaData implements Serializable {
    @SerializedName("access_token")
    @Expose
    private String accessToken;
    @SerializedName("user")
    @Expose
    private ViewModelInstaDataUser user;
    private final static long serialVersionUID = 1692990614104533394L;

    /**
     * No args constructor for use in serialization
     *
     */
    public ViewModelInstaData() {
    }

    /**
     *
     * @param accessToken
     * @param user
     */
    public ViewModelInstaData(String accessToken, ViewModelInstaDataUser user) {
        super();
        this.accessToken = accessToken;
        this.user = user;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public ViewModelInstaDataUser getUser() {
        return user;
    }

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

public class ViewModelInstaDataUser implements Serializable {

    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("username")
    @Expose
    private String username;
    @SerializedName("profile_picture")
    @Expose
    private String profilePicture;
    @SerializedName("full_name")
    @Expose
    private String fullName;
    @SerializedName("bio")
    @Expose
    private String bio;
    @SerializedName("website")
    @Expose
    private String website;
    @SerializedName("is_business")
    @Expose
    private Boolean isBusiness;
    private final static long serialVersionUID = 2434734929798479023L;

    /**
     * No args constructor for use in serialization
     *
     */
    public ViewModelInstaDataUser() {
    }

    /**
     *
     * @param id
     * @param username
     * @param bio
     * @param website
     * @param profilePicture
     * @param fullName
     * @param isBusiness
     */
    public ViewModelInstaDataUser(String id, String username, String profilePicture, String fullName, String bio, String website, Boolean isBusiness) {
        super();
        this.id = id;
        this.username = username;
        this.profilePicture = profilePicture;
        this.fullName = fullName;
        this.bio = bio;
        this.website = website;
        this.isBusiness = isBusiness;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getProfilePicture() {
        return profilePicture;
    }

    public void setProfilePicture(String profilePicture) {
        this.profilePicture = profilePicture;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getBio() {
        return bio;
    }

    public void setBio(String bio) {
        this.bio = bio;
    }

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public Boolean getIsBusiness() {
        return isBusiness;
    }

    public void setIsBusiness(Boolean isBusiness) {
        this.isBusiness = isBusiness;
    }
}

这是我的改造电话:

ApiInterface apiInterface = Api.getClient().create(ApiInterface.class);
Call<ViewModelInstaData> call = apiInterface.getAccountData(url);
call.enqueue(new Callback<ViewModelInstaData>() {
    @Override
    public void onResponse(@NonNull Call<ViewModelInstaData> call, @NonNull Response<ViewModelInstaData> response) {

         Log.i("AccountResponse", String.valueOf(response.code()));

         if (response.code() == 200) {

         Log.i("token",response.body().getAccessToken());

         } else {

         }
    }

    @Override
    public void onFailure(@NonNull Call<ViewModelInstaData> call, @NonNull Throwable t) {
        loginPresenterInterface.onFailedAccountData(1000, t.getMessage());

        Log.i("AccountFailed", String.valueOf("Failed"));
        Log.i("AccountFailed", String.valueOf(t.getMessage()));

    }
});

这是我的改造界面:

@GET("{url}")
Call<ViewModelInstaData>getAccountData(@Path("url") String url);

标签: javaandroidretrofit

解决方案


推荐阅读