问题描述
我正在尝试使用改造来解析 json 数据,但没有收到数据帮助我解析数据.
I am trying to work with retrofit to parse the json data but there is no data received help me to parse the data.
下面是我创建的构建器和界面:
Below is the builder and interface i created :
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
文件:
Below is jsonschema2pojo
files to be used in app :
-----------------------------------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;
}
}
这就是我尝试获取数据的方式:
and this is how i tried to get the data :
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.
I really don't know what i am doing wrong and how to solve it.I really want to know how to parse the json.
谢谢.
编辑这是我将从服务器收到的响应:
EDITThis is the responce i will recieve from the server :
{
"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"
}
推荐答案
你的改造客户端方法如下
Your retrofit client method will be as follows
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 模型如下:
Your User Pojo Model Will be as follows :
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;
}
}
你的改造interface
如下
public interface UserFetchData {
@GET("/api/v1/users/2055338526/info") //here is your api end points
Call<UserModel> getFullName();
}
在你的 getData()
方法将如下:-
In your getData()
Method will be as follows :-
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();
}
});
}
这篇关于用retrofit-gson解析JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!