android - 使用 retrofit-gson 解析 JSON?

标签 android json retrofit2

我正在尝试使用改造来解析 json 数据,但没有收到任何数据来帮助我解析数据。

下面是我创建的构建器和界面:

public class fetchdata {
    private static UserFetchData userdatafetch = null;

    public static UserFetchData getService() {
        if (userdatafetch==null) {
            String url = "the_instagram_link";
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(url)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            userdatafetch = retrofit.create(UserFetchData.class);
        }
        return userdatafetch;
    }

    public interface UserFetchData {
        @GET("full_name")
        Call<User> getFullName();
    }
}

下面是在应用程序中使用的 jsonschema2pojo 文件:

-----------------------------------com.google.app.HdProfilePicUrlInfo.java-----------------------------------

package com.google.app;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class HdProfilePicUrlInfo {

@SerializedName("height")
@Expose
private Integer height;
@SerializedName("url")
@Expose
private String url;
@SerializedName("width")
@Expose
private Integer width;

public Integer getHeight() {
return height;
}

public void setHeight(Integer height) {
this.height = height;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public Integer getWidth() {
return width;
}

public void setWidth(Integer width) {
this.width = width;
}

}
-----------------------------------com.google.app.User.java-----------------------------------

package com.google.app;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class User {

@SerializedName("is_verified")
@Expose
private Boolean isVerified;
@SerializedName("biography")
@Expose
private String biography;
@SerializedName("follower_count")
@Expose
private Integer followerCount;
@SerializedName("media_count")
@Expose
private Integer mediaCount;
@SerializedName("hd_profile_pic_url_info")
@Expose
private HdProfilePicUrlInfo hdProfilePicUrlInfo;
@SerializedName("external_lynx_url")
@Expose
private String externalLynxUrl;
@SerializedName("following_count")
@Expose
private Integer followingCount;

public Boolean getIsVerified() {
return isVerified;
}

public void setIsVerified(Boolean isVerified) {
this.isVerified = isVerified;
}

public String getBiography() {
return biography;
}

public void setBiography(String biography) {
this.biography = biography;
}

public Integer getFollowerCount() {
return followerCount;
}

public void setFollowerCount(Integer followerCount) {
this.followerCount = followerCount;
}

public Integer getMediaCount() {
return mediaCount;
}

public void setMediaCount(Integer mediaCount) {
this.mediaCount = mediaCount;
}

public HdProfilePicUrlInfo getHdProfilePicUrlInfo() {
return hdProfilePicUrlInfo;
}

public void setHdProfilePicUrlInfo(HdProfilePicUrlInfo hdProfilePicUrlInfo) {
this.hdProfilePicUrlInfo = hdProfilePicUrlInfo;
}

public String getExternalLynxUrl() {
return externalLynxUrl;
}

public void setExternalLynxUrl(String externalLynxUrl) {
this.externalLynxUrl = externalLynxUrl;
}

public Integer getFollowingCount() {
return followingCount;
}

public void setFollowingCount(Integer followingCount) {
this.followingCount = followingCount;
}

}
-----------------------------------com.google.app.Userdata.java-----------------------------------

package com.google.app;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Userdata {

@SerializedName("user")
@Expose
private User user;

public User getUser() {
return user;
}

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

} 

这就是我试图获取数据的方式:

public void GetData {
        Call<User> jsondata = fetchdata.getService().getFullName();
        jsondata.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                User user = response.body();
                Toast.makeText(getContext(), "Yeeeeep !", Toast.LENGTH_SHORT).show();//added the break point but no data recived but it toasted
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                Toast.makeText(getContext(), "Error!", Toast.LENGTH_SHORT).show();
            }
        });

    } 

滑动依赖:

实现 'com.squareup.retrofit2:retrofit:2.4.0'

实现 'com.squareup.retrofit2:converter-gson:2.4.0'

我真的不知道我做错了什么以及如何解决它。 我真的很想知道如何解析 json。

谢谢。

编辑 这是我将从服务器收到的响应:

{
  "user": {
    "external_lynx_url": "https://l.instagram.com/?u=http%3A%2F%2Fgoogle.com%2F\u0026e=ATPCIx1ldIEQpnqvlYYf-W9bvjDwTqw4XGRSCjt5mr5VhqwlaEsaHpdkMUiU2BfQumhKNM3IJEQri5UX8Qsz8p2G",
    "username": "google",
    "has_anonymous_profile_picture": false,
    "external_url": "http://google.com/",
    "full_name": "Google",
    "profile_pic_id": "1825015012270132630_1067259270",
    "media_count": 830,
    "pk": 1067259270,
    "hd_profile_pic_versions": [
      {
        "height": 320,
        "url": "https://instagram.fjai1-1.fna.fbcdn.net/vp/1df66b7b71efa2a6dba025f38a47495a/5BE04111/t51.2885-19/s320x320/36616551_641374529567850_6798278096731504640_n.jpg",
        "width": 320
      },
      {
        "height": 640,
        "url": "https://instagram.fjai1-1.fna.fbcdn.net/vp/e453300e2a8170426349c370afe2c8f7/5BD45B7E/t51.2885-19/s640x640/36616551_641374529567850_6798278096731504640_n.jpg",
        "width": 640
      }
    ],
    "is_private": false,
    "is_verified": true,
    "reel_auto_archive": "on",
    "biography": "Google unfiltered\u2014sometimes with filters.",
    "follower_count": 7035887,
    "has_highlight_reels": true,
    "usertags_count": 311,
    "following_count": 12,
    "profile_pic_url": "https://instagram.fjai1-1.fna.fbcdn.net/vp/e4e4a1a1c41950738442218cf892669e/5C0D19E1/t51.2885-19/s150x150/36616551_641374529567850_6798278096731504640_n.jpg",
    "hd_profile_pic_url_info": {
      "height": 1080,
      "url": "https://instagram.fjai1-1.fna.fbcdn.net/vp/10891608f8e9f072ac5dcb4e9554b7a6/5BE33A1B/t51.2885-19/36616551_641374529567850_6798278096731504640_n.jpg",
      "width": 1080
    },
    "auto_expand_chaining": false
  },
  "status": "ok"
} 

最佳答案

你的改造客户端方法如下

public static UserFetchData getService() {
        if (userdatafetch==null) {
            String url = "https://i.instagram.com/";  //this ur base url
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(url)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            userdatafetch = retrofit.create(UserFetchData.class);
        }
        return userdatafetch;
    }

您的用户 Pojo 模型如下:

public class UserModel {

    @Expose
    @SerializedName("status")
    public String status;
    @Expose
    @SerializedName("user")
    public User user;

    public String getStatus() {
       return status;
    }

    public User getUser() {
        return user;
    } 

    public static class User {
        @Expose
        @SerializedName("auto_expand_chaining")
        public boolean auto_expand_chaining;
        @Expose
        @SerializedName("hd_profile_pic_url_info")
        public Hd_profile_pic_url_info hd_profile_pic_url_info;
        @Expose
        @SerializedName("profile_pic_url")
        public String profile_pic_url;
        @Expose
        @SerializedName("following_count")
        public int following_count;
        @Expose
        @SerializedName("usertags_count")
        public int usertags_count;
        @Expose
        @SerializedName("has_highlight_reels")
        public boolean has_highlight_reels;
        @Expose
        @SerializedName("follower_count")
        public int follower_count;
        @Expose
        @SerializedName("biography")
        public String biography;
        @Expose
        @SerializedName("reel_auto_archive")
        public String reel_auto_archive;
        @Expose
        @SerializedName("is_verified")
        public boolean is_verified;
        @Expose
        @SerializedName("is_private")
        public boolean is_private;
        @Expose
        @SerializedName("hd_profile_pic_versions")
        public List<Hd_profile_pic_versions> hd_profile_pic_versions;
        @Expose
        @SerializedName("pk")
        public int pk;
        @Expose
        @SerializedName("media_count")
        public int media_count;
        @Expose
        @SerializedName("profile_pic_id")
        public String profile_pic_id;
        @Expose
        @SerializedName("full_name")
        public String full_name;
        @Expose
        @SerializedName("external_url")
        public String external_url;
        @Expose
        @SerializedName("has_anonymous_profile_picture")
        public boolean has_anonymous_profile_picture;
        @Expose
        @SerializedName("username")
        public String username;
        @Expose
        @SerializedName("external_lynx_url")
        public String external_lynx_url;
    }

    public static class Hd_profile_pic_url_info {
        @Expose
        @SerializedName("width")
        public int width;
        @Expose
        @SerializedName("url")
        public String url;
        @Expose
        @SerializedName("height")
        public int height;
    }

    public static class Hd_profile_pic_versions {
        @Expose
        @SerializedName("width")
        public int width;
        @Expose
        @SerializedName("url")
        public String url;
        @Expose
        @SerializedName("height")
        public int height;
    }


}

你改造后的界面如下

public interface UserFetchData {
        @GET("/api/v1/users/2055338526/info")  //here is your api end points
        Call<UserModel> getFullName();
    }

在您的 getData() 方法中将如下所示:-

public void GetData {
        Call<UserModel> jsondata = fetchdata.getService().getFullName();
        jsondata.enqueue(new Callback<UserModel>() {
            @Override
            public void onResponse(Call<UserModel> call, Response<UserModel> response) {
                User user = response.body().getUser();
                Toast.makeText(getContext(), "Yeeeeep !", Toast.LENGTH_SHORT).show();//added the break point but no data recived but it toasted
            }

            @Override
            public void onFailure(Call<UserModel> call, Throwable t) {
                Toast.makeText(getContext(), "Error!", Toast.LENGTH_SHORT).show();
            }
        });

    }

关于android - 使用 retrofit-gson 解析 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51503557/

相关文章:

android - 通过电子邮件发送图像,Android?

java - Android + Retrofit 2 + GSON = 无法为接口(interface)调用无参数构造函数

ios - 如何为JSON字典添加值?

android - 使用 Retrofit 2.0 POST 方法获取请求正文内容

android - 在 multipart retrofit2 中上传图像数组

android - 使用手机的唯一设备标识号而不是帐户

java - 如何将图像按钮作为参数传递给 android studio 中的函数?

c++ - Android NDK r5 不能包含 STL 头文件

ruby-on-rails - Ruby on Rails 高级 JSON 序列化

javascript - 如何仅使用 javascript 在 html 选择选项中列出 JSON 文件中的对象?