Android 检索JSON数据的有效方法/正确方法

Android 检索JSON数据的有效方法/正确方法,android,json,api,github,Android,Json,Api,Github,我是Android Studio的新手,我正在尝试从Github API(例如)请求API数据并在我的应用程序中显示 我不知何故让应用程序通过以下操作从API检索JSON: public class MainActivity extends AppCompatActivity { private TextView tvData; private static final String TAG = "MainActivity"; @Override protec

我是Android Studio的新手,我正在尝试从Github API(例如)请求API数据并在我的应用程序中显示

我不知何故让应用程序通过以下操作从API检索JSON:

public class MainActivity extends AppCompatActivity {

    private TextView tvData;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.d(TAG, "onCreate: Starting.");


        // Set up the ViewPager with the sections adapter


        // Github tab

        Button buttonHit = (Button) findViewById(R.id.buttonHit);
        tvData = (TextView) findViewById(R.id.tvJsonItem);

        buttonHit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new JSONTask().execute("https://api.github.com/users/froala");

            }
        });

    }


    public class JSONTask extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... params) {

            HttpURLConnection connection = null;
            BufferedReader reader = null;

            try {
                // Pass in a String and convert to URL
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();

                InputStream stream = connection.getInputStream();

                // for reading data line by line
                reader = new BufferedReader(new InputStreamReader(stream));
                StringBuffer strBuffer = new StringBuffer();


                String line = "";
                while ((line = reader.readLine()) != null) {
                    strBuffer.append(line);
                }

                // If we are able to get the data

                String retreivedJson = strBuffer.toString();
                JSONObject parentObject = new JSONObject(retreivedJson);
                JsonReader jsonReader = new JsonReader(responseBody);

                return retreivedJson;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } finally {
                //cant close null

                if (connection != null) {
                    // close both connection and the reader
                    connection.disconnect();
                }
                try {
                    if (reader != null) {
                        reader.close();
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return null;

        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            tvData.setText(result);
        }
    }
}
public类MainActivity扩展了AppCompatActivity{
私有文本视图数据;
私有静态最终字符串TAG=“MainActivity”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(标记“onCreate:Starting”);
//使用分区适配器设置ViewPager
//Github选项卡
Button buttonHit=(Button)findViewById(R.id.buttonHit);
tvData=(TextView)findviewbyd(R.id.tvJsonItem);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
新建JSONTask()。执行(“https://api.github.com/users/froala");
}
});
}
公共类JSONTask扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
HttpURLConnection=null;
BufferedReader reader=null;
试一试{
//传入字符串并转换为URL
URL=新URL(参数[0]);
connection=(HttpURLConnection)url.openConnection();
connection.connect();
InputStream=connection.getInputStream();
//用于逐行读取数据
reader=新的BufferedReader(新的InputStreamReader(流));
StringBuffer strBuffer=新的StringBuffer();
字符串行=”;
而((line=reader.readLine())!=null){
strBuffer.append(行);
}
//如果我们能得到数据
字符串retreivedJson=strBuffer.toString();
JSONObject parentObject=新的JSONObject(retreivedJson);
JsonReader JsonReader=新的JsonReader(ResponseBy);
返回RetrievedJSON;
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}捕获(JSONException e){
e、 printStackTrace();
}最后{
//不能关闭空值
if(连接!=null){
//关闭连接和读卡器
连接断开();
}
试一试{
if(读卡器!=null){
reader.close();
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
tvData.setText(结果);
}
}
}
在它看来:

现在我只需要使用它来解析JSON。但问题是,当我在类似JSON的应用程序中使用不同的URL时,检索JSON数据不起作用,当单击按钮时,应用程序不会显示任何内容。这很奇怪,因为/users/id页面和/users/id/repos页面都是JSON,看起来没有什么不同。我不知道为什么另一个不起作用

两个问题:

  • 我是否使用正确的方法从API检索JSON
  • 为什么另一个链接()不能与我的代码实现一起工作
  • 请帮忙!我真的很困惑

  • 您可以使用改装并从URL获取数据:

    你可以参考:

  • 您可以使用上面的对象

  • 使用JSON字符串,我使用并创建对象:

    -----------------------------------com.example.Example.java-----------------------------------
    
    package com.example;
    
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Example {
    
    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("full_name")
    @Expose
    private String fullName;
    @SerializedName("owner")
    @Expose
    private Owner owner;
    @SerializedName("private")
    @Expose
    private Boolean _private;
    @SerializedName("html_url")
    @Expose
    private String htmlUrl;
    @SerializedName("description")
    @Expose
    private String description;
    @SerializedName("fork")
    @Expose
    private Boolean fork;
    @SerializedName("url")
    @Expose
    private String url;
    @SerializedName("forks_url")
    @Expose
    private String forksUrl;
    @SerializedName("keys_url")
    @Expose
    private String keysUrl;
    @SerializedName("collaborators_url")
    @Expose
    private String collaboratorsUrl;
    @SerializedName("teams_url")
    @Expose
    private String teamsUrl;
    @SerializedName("hooks_url")
    @Expose
    private String hooksUrl;
    @SerializedName("issue_events_url")
    @Expose
    private String issueEventsUrl;
    @SerializedName("events_url")
    @Expose
    private String eventsUrl;
    @SerializedName("assignees_url")
    @Expose
    private String assigneesUrl;
    @SerializedName("branches_url")
    @Expose
    private String branchesUrl;
    @SerializedName("tags_url")
    @Expose
    private String tagsUrl;
    @SerializedName("blobs_url")
    @Expose
    private String blobsUrl;
    @SerializedName("git_tags_url")
    @Expose
    private String gitTagsUrl;
    @SerializedName("git_refs_url")
    @Expose
    private String gitRefsUrl;
    @SerializedName("trees_url")
    @Expose
    private String treesUrl;
    @SerializedName("statuses_url")
    @Expose
    private String statusesUrl;
    @SerializedName("languages_url")
    @Expose
    private String languagesUrl;
    @SerializedName("stargazers_url")
    @Expose
    private String stargazersUrl;
    @SerializedName("contributors_url")
    @Expose
    private String contributorsUrl;
    @SerializedName("subscribers_url")
    @Expose
    private String subscribersUrl;
    @SerializedName("subscription_url")
    @Expose
    private String subscriptionUrl;
    @SerializedName("commits_url")
    @Expose
    private String commitsUrl;
    @SerializedName("git_commits_url")
    @Expose
    private String gitCommitsUrl;
    @SerializedName("comments_url")
    @Expose
    private String commentsUrl;
    @SerializedName("issue_comment_url")
    @Expose
    private String issueCommentUrl;
    @SerializedName("contents_url")
    @Expose
    private String contentsUrl;
    @SerializedName("compare_url")
    @Expose
    private String compareUrl;
    @SerializedName("merges_url")
    @Expose
    private String mergesUrl;
    @SerializedName("archive_url")
    @Expose
    private String archiveUrl;
    @SerializedName("downloads_url")
    @Expose
    private String downloadsUrl;
    @SerializedName("issues_url")
    @Expose
    private String issuesUrl;
    @SerializedName("pulls_url")
    @Expose
    private String pullsUrl;
    @SerializedName("milestones_url")
    @Expose
    private String milestonesUrl;
    @SerializedName("notifications_url")
    @Expose
    private String notificationsUrl;
    @SerializedName("labels_url")
    @Expose
    private String labelsUrl;
    @SerializedName("releases_url")
    @Expose
    private String releasesUrl;
    @SerializedName("deployments_url")
    @Expose
    private String deploymentsUrl;
    @SerializedName("created_at")
    @Expose
    private String createdAt;
    @SerializedName("updated_at")
    @Expose
    private String updatedAt;
    @SerializedName("pushed_at")
    @Expose
    private String pushedAt;
    @SerializedName("git_url")
    @Expose
    private String gitUrl;
    @SerializedName("ssh_url")
    @Expose
    private String sshUrl;
    @SerializedName("clone_url")
    @Expose
    private String cloneUrl;
    @SerializedName("svn_url")
    @Expose
    private String svnUrl;
    @SerializedName("homepage")
    @Expose
    private String homepage;
    @SerializedName("size")
    @Expose
    private Integer size;
    @SerializedName("stargazers_count")
    @Expose
    private Integer stargazersCount;
    @SerializedName("watchers_count")
    @Expose
    private Integer watchersCount;
    @SerializedName("language")
    @Expose
    private String language;
    @SerializedName("has_issues")
    @Expose
    private Boolean hasIssues;
    @SerializedName("has_projects")
    @Expose
    private Boolean hasProjects;
    @SerializedName("has_downloads")
    @Expose
    private Boolean hasDownloads;
    @SerializedName("has_wiki")
    @Expose
    private Boolean hasWiki;
    @SerializedName("has_pages")
    @Expose
    private Boolean hasPages;
    @SerializedName("forks_count")
    @Expose
    private Integer forksCount;
    @SerializedName("mirror_url")
    @Expose
    private Object mirrorUrl;
    @SerializedName("archived")
    @Expose
    private Boolean archived;
    @SerializedName("open_issues_count")
    @Expose
    private Integer openIssuesCount;
    @SerializedName("forks")
    @Expose
    private Integer forks;
    @SerializedName("open_issues")
    @Expose
    private Integer openIssues;
    @SerializedName("watchers")
    @Expose
    private Integer watchers;
    @SerializedName("default_branch")
    @Expose
    private String defaultBranch;
    
    public Integer getId() {
    return id;
    }
    
    public void setId(Integer id) {
    this.id = id;
    }
    
    public String getName() {
    return name;
    }
    
    public void setName(String name) {
    this.name = name;
    }
    
    public String getFullName() {
    return fullName;
    }
    
    public void setFullName(String fullName) {
    this.fullName = fullName;
    }
    
    public Owner getOwner() {
    return owner;
    }
    
    public void setOwner(Owner owner) {
    this.owner = owner;
    }
    
    public Boolean getPrivate() {
    return _private;
    }
    
    public void setPrivate(Boolean _private) {
    this._private = _private;
    }
    
    public String getHtmlUrl() {
    return htmlUrl;
    }
    
    public void setHtmlUrl(String htmlUrl) {
    this.htmlUrl = htmlUrl;
    }
    
    public String getDescription() {
    return description;
    }
    
    public void setDescription(String description) {
    this.description = description;
    }
    
    public Boolean getFork() {
    return fork;
    }
    
    public void setFork(Boolean fork) {
    this.fork = fork;
    }
    
    public String getUrl() {
    return url;
    }
    
    public void setUrl(String url) {
    this.url = url;
    }
    
    public String getForksUrl() {
    return forksUrl;
    }
    
    public void setForksUrl(String forksUrl) {
    this.forksUrl = forksUrl;
    }
    
    public String getKeysUrl() {
    return keysUrl;
    }
    
    public void setKeysUrl(String keysUrl) {
    this.keysUrl = keysUrl;
    }
    
    public String getCollaboratorsUrl() {
    return collaboratorsUrl;
    }
    
    public void setCollaboratorsUrl(String collaboratorsUrl) {
    this.collaboratorsUrl = collaboratorsUrl;
    }
    
    public String getTeamsUrl() {
    return teamsUrl;
    }
    
    public void setTeamsUrl(String teamsUrl) {
    this.teamsUrl = teamsUrl;
    }
    
    public String getHooksUrl() {
    return hooksUrl;
    }
    
    public void setHooksUrl(String hooksUrl) {
    this.hooksUrl = hooksUrl;
    }
    
    public String getIssueEventsUrl() {
    return issueEventsUrl;
    }
    
    public void setIssueEventsUrl(String issueEventsUrl) {
    this.issueEventsUrl = issueEventsUrl;
    }
    
    public String getEventsUrl() {
    return eventsUrl;
    }
    
    public void setEventsUrl(String eventsUrl) {
    this.eventsUrl = eventsUrl;
    }
    
    public String getAssigneesUrl() {
    return assigneesUrl;
    }
    
    public void setAssigneesUrl(String assigneesUrl) {
    this.assigneesUrl = assigneesUrl;
    }
    
    public String getBranchesUrl() {
    return branchesUrl;
    }
    
    public void setBranchesUrl(String branchesUrl) {
    this.branchesUrl = branchesUrl;
    }
    
    public String getTagsUrl() {
    return tagsUrl;
    }
    
    public void setTagsUrl(String tagsUrl) {
    this.tagsUrl = tagsUrl;
    }
    
    public String getBlobsUrl() {
    return blobsUrl;
    }
    
    public void setBlobsUrl(String blobsUrl) {
    this.blobsUrl = blobsUrl;
    }
    
    public String getGitTagsUrl() {
    return gitTagsUrl;
    }
    
    public void setGitTagsUrl(String gitTagsUrl) {
    this.gitTagsUrl = gitTagsUrl;
    }
    
    public String getGitRefsUrl() {
    return gitRefsUrl;
    }
    
    public void setGitRefsUrl(String gitRefsUrl) {
    this.gitRefsUrl = gitRefsUrl;
    }
    
    public String getTreesUrl() {
    return treesUrl;
    }
    
    public void setTreesUrl(String treesUrl) {
    this.treesUrl = treesUrl;
    }
    
    public String getStatusesUrl() {
    return statusesUrl;
    }
    
    public void setStatusesUrl(String statusesUrl) {
    this.statusesUrl = statusesUrl;
    }
    
    public String getLanguagesUrl() {
    return languagesUrl;
    }
    
    public void setLanguagesUrl(String languagesUrl) {
    this.languagesUrl = languagesUrl;
    }
    
    public String getStargazersUrl() {
    return stargazersUrl;
    }
    
    public void setStargazersUrl(String stargazersUrl) {
    this.stargazersUrl = stargazersUrl;
    }
    
    public String getContributorsUrl() {
    return contributorsUrl;
    }
    
    public void setContributorsUrl(String contributorsUrl) {
    this.contributorsUrl = contributorsUrl;
    }
    
    public String getSubscribersUrl() {
    return subscribersUrl;
    }
    
    public void setSubscribersUrl(String subscribersUrl) {
    this.subscribersUrl = subscribersUrl;
    }
    
    public String getSubscriptionUrl() {
    return subscriptionUrl;
    }
    
    public void setSubscriptionUrl(String subscriptionUrl) {
    this.subscriptionUrl = subscriptionUrl;
    }
    
    public String getCommitsUrl() {
    return commitsUrl;
    }
    
    public void setCommitsUrl(String commitsUrl) {
    this.commitsUrl = commitsUrl;
    }
    
    public String getGitCommitsUrl() {
    return gitCommitsUrl;
    }
    
    public void setGitCommitsUrl(String gitCommitsUrl) {
    this.gitCommitsUrl = gitCommitsUrl;
    }
    
    public String getCommentsUrl() {
    return commentsUrl;
    }
    
    public void setCommentsUrl(String commentsUrl) {
    this.commentsUrl = commentsUrl;
    }
    
    public String getIssueCommentUrl() {
    return issueCommentUrl;
    }
    
    public void setIssueCommentUrl(String issueCommentUrl) {
    this.issueCommentUrl = issueCommentUrl;
    }
    
    public String getContentsUrl() {
    return contentsUrl;
    }
    
    public void setContentsUrl(String contentsUrl) {
    this.contentsUrl = contentsUrl;
    }
    
    public String getCompareUrl() {
    return compareUrl;
    }
    
    public void setCompareUrl(String compareUrl) {
    this.compareUrl = compareUrl;
    }
    
    public String getMergesUrl() {
    return mergesUrl;
    }
    
    public void setMergesUrl(String mergesUrl) {
    this.mergesUrl = mergesUrl;
    }
    
    public String getArchiveUrl() {
    return archiveUrl;
    }
    
    public void setArchiveUrl(String archiveUrl) {
    this.archiveUrl = archiveUrl;
    }
    
    public String getDownloadsUrl() {
    return downloadsUrl;
    }
    
    public void setDownloadsUrl(String downloadsUrl) {
    this.downloadsUrl = downloadsUrl;
    }
    
    public String getIssuesUrl() {
    return issuesUrl;
    }
    
    public void setIssuesUrl(String issuesUrl) {
    this.issuesUrl = issuesUrl;
    }
    
    public String getPullsUrl() {
    return pullsUrl;
    }
    
    public void setPullsUrl(String pullsUrl) {
    this.pullsUrl = pullsUrl;
    }
    
    public String getMilestonesUrl() {
    return milestonesUrl;
    }
    
    public void setMilestonesUrl(String milestonesUrl) {
    this.milestonesUrl = milestonesUrl;
    }
    
    public String getNotificationsUrl() {
    return notificationsUrl;
    }
    
    public void setNotificationsUrl(String notificationsUrl) {
    this.notificationsUrl = notificationsUrl;
    }
    
    public String getLabelsUrl() {
    return labelsUrl;
    }
    
    public void setLabelsUrl(String labelsUrl) {
    this.labelsUrl = labelsUrl;
    }
    
    public String getReleasesUrl() {
    return releasesUrl;
    }
    
    public void setReleasesUrl(String releasesUrl) {
    this.releasesUrl = releasesUrl;
    }
    
    public String getDeploymentsUrl() {
    return deploymentsUrl;
    }
    
    public void setDeploymentsUrl(String deploymentsUrl) {
    this.deploymentsUrl = deploymentsUrl;
    }
    
    public String getCreatedAt() {
    return createdAt;
    }
    
    public void setCreatedAt(String createdAt) {
    this.createdAt = createdAt;
    }
    
    public String getUpdatedAt() {
    return updatedAt;
    }
    
    public void setUpdatedAt(String updatedAt) {
    this.updatedAt = updatedAt;
    }
    
    public String getPushedAt() {
    return pushedAt;
    }
    
    public void setPushedAt(String pushedAt) {
    this.pushedAt = pushedAt;
    }
    
    public String getGitUrl() {
    return gitUrl;
    }
    
    public void setGitUrl(String gitUrl) {
    this.gitUrl = gitUrl;
    }
    
    public String getSshUrl() {
    return sshUrl;
    }
    
    public void setSshUrl(String sshUrl) {
    this.sshUrl = sshUrl;
    }
    
    public String getCloneUrl() {
    return cloneUrl;
    }
    
    public void setCloneUrl(String cloneUrl) {
    this.cloneUrl = cloneUrl;
    }
    
    public String getSvnUrl() {
    return svnUrl;
    }
    
    public void setSvnUrl(String svnUrl) {
    this.svnUrl = svnUrl;
    }
    
    public String getHomepage() {
    return homepage;
    }
    
    public void setHomepage(String homepage) {
    this.homepage = homepage;
    }
    
    public Integer getSize() {
    return size;
    }
    
    public void setSize(Integer size) {
    this.size = size;
    }
    
    public Integer getStargazersCount() {
    return stargazersCount;
    }
    
    public void setStargazersCount(Integer stargazersCount) {
    this.stargazersCount = stargazersCount;
    }
    
    public Integer getWatchersCount() {
    return watchersCount;
    }
    
    public void setWatchersCount(Integer watchersCount) {
    this.watchersCount = watchersCount;
    }
    
    public String getLanguage() {
    return language;
    }
    
    public void setLanguage(String language) {
    this.language = language;
    }
    
    public Boolean getHasIssues() {
    return hasIssues;
    }
    
    public void setHasIssues(Boolean hasIssues) {
    this.hasIssues = hasIssues;
    }
    
    public Boolean getHasProjects() {
    return hasProjects;
    }
    
    public void setHasProjects(Boolean hasProjects) {
    this.hasProjects = hasProjects;
    }
    
    public Boolean getHasDownloads() {
    return hasDownloads;
    }
    
    public void setHasDownloads(Boolean hasDownloads) {
    this.hasDownloads = hasDownloads;
    }
    
    public Boolean getHasWiki() {
    return hasWiki;
    }
    
    public void setHasWiki(Boolean hasWiki) {
    this.hasWiki = hasWiki;
    }
    
    public Boolean getHasPages() {
    return hasPages;
    }
    
    public void setHasPages(Boolean hasPages) {
    this.hasPages = hasPages;
    }
    
    public Integer getForksCount() {
    return forksCount;
    }
    
    public void setForksCount(Integer forksCount) {
    this.forksCount = forksCount;
    }
    
    public Object getMirrorUrl() {
    return mirrorUrl;
    }
    
    public void setMirrorUrl(Object mirrorUrl) {
    this.mirrorUrl = mirrorUrl;
    }
    
    public Boolean getArchived() {
    return archived;
    }
    
    public void setArchived(Boolean archived) {
    this.archived = archived;
    }
    
    public Integer getOpenIssuesCount() {
    return openIssuesCount;
    }
    
    public void setOpenIssuesCount(Integer openIssuesCount) {
    this.openIssuesCount = openIssuesCount;
    }
    
    public Integer getForks() {
    return forks;
    }
    
    public void setForks(Integer forks) {
    this.forks = forks;
    }
    
    public Integer getOpenIssues() {
    return openIssues;
    }
    
    public void setOpenIssues(Integer openIssues) {
    this.openIssues = openIssues;
    }
    
    public Integer getWatchers() {
    return watchers;
    }
    
    public void setWatchers(Integer watchers) {
    this.watchers = watchers;
    }
    
    public String getDefaultBranch() {
    return defaultBranch;
    }
    
    public void setDefaultBranch(String defaultBranch) {
    this.defaultBranch = defaultBranch;
    }
    
    }
    
    -----------------------------------com.example.Owner.java-----------------------------------

    package com.example;
    
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Owner {
    
    @SerializedName("login")
    @Expose
    private String login;
    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("avatar_url")
    @Expose
    private String avatarUrl;
    @SerializedName("gravatar_id")
    @Expose
    private String gravatarId;
    @SerializedName("url")
    @Expose
    private String url;
    @SerializedName("html_url")
    @Expose
    private String htmlUrl;
    @SerializedName("followers_url")
    @Expose
    private String followersUrl;
    @SerializedName("following_url")
    @Expose
    private String followingUrl;
    @SerializedName("gists_url")
    @Expose
    private String gistsUrl;
    @SerializedName("starred_url")
    @Expose
    private String starredUrl;
    @SerializedName("subscriptions_url")
    @Expose
    private String subscriptionsUrl;
    @SerializedName("organizations_url")
    @Expose
    private String organizationsUrl;
    @SerializedName("repos_url")
    @Expose
    private String reposUrl;
    @SerializedName("events_url")
    @Expose
    private String eventsUrl;
    @SerializedName("received_events_url")
    @Expose
    private String receivedEventsUrl;
    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("site_admin")
    @Expose
    private Boolean siteAdmin;
    
    public String getLogin() {
    return login;
    }
    
    public void setLogin(String login) {
    this.login = login;
    }
    
    public Integer getId() {
    return id;
    }
    
    public void setId(Integer id) {
    this.id = id;
    }
    
    public String getAvatarUrl() {
    return avatarUrl;
    }
    
    public void setAvatarUrl(String avatarUrl) {
    this.avatarUrl = avatarUrl;
    }
    
    public String getGravatarId() {
    return gravatarId;
    }
    
    public void setGravatarId(String gravatarId) {
    this.gravatarId = gravatarId;
    }
    
    public String getUrl() {
    return url;
    }
    
    public void setUrl(String url) {
    this.url = url;
    }
    
    public String getHtmlUrl() {
    return htmlUrl;
    }
    
    public void setHtmlUrl(String htmlUrl) {
    this.htmlUrl = htmlUrl;
    }
    
    public String getFollowersUrl() {
    return followersUrl;
    }
    
    public void setFollowersUrl(String followersUrl) {
    this.followersUrl = followersUrl;
    }
    
    public String getFollowingUrl() {
    return followingUrl;
    }
    
    public void setFollowingUrl(String followingUrl) {
    this.followingUrl = followingUrl;
    }
    
    public String getGistsUrl() {
    return gistsUrl;
    }
    
    public void setGistsUrl(String gistsUrl) {
    this.gistsUrl = gistsUrl;
    }
    
    public String getStarredUrl() {
    return starredUrl;
    }
    
    public void setStarredUrl(String starredUrl) {
    this.starredUrl = starredUrl;
    }
    
    public String getSubscriptionsUrl() {
    return subscriptionsUrl;
    }
    
    public void setSubscriptionsUrl(String subscriptionsUrl) {
    this.subscriptionsUrl = subscriptionsUrl;
    }
    
    public String getOrganizationsUrl() {
    return organizationsUrl;
    }
    
    public void setOrganizationsUrl(String organizationsUrl) {
    this.organizationsUrl = organizationsUrl;
    }
    
    public String getReposUrl() {
    return reposUrl;
    }
    
    public void setReposUrl(String reposUrl) {
    this.reposUrl = reposUrl;
    }
    
    public String getEventsUrl() {
    return eventsUrl;
    }
    
    public void setEventsUrl(String eventsUrl) {
    this.eventsUrl = eventsUrl;
    }
    
    public String getReceivedEventsUrl() {
    return receivedEventsUrl;
    }
    
    public void setReceivedEventsUrl(String receivedEventsUrl) {
    this.receivedEventsUrl = receivedEventsUrl;
    }
    
    public String getType() {
    return type;
    }
    
    public void setType(String type) {
    this.type = type;
    }
    
    public Boolean getSiteAdmin() {
    return siteAdmin;
    }
    
    public void setSiteAdmin(Boolean siteAdmin) {
    this.siteAdmin = siteAdmin;
    }
    
    }
    
    以及使用改装,例如:

     Call<Example> call = apiService.getData(URL);
    
    
        call.enqueue(new Callback<Example>() {
            @Override
            public void onResponse(Call<Example> call, Response<Example> response) {
    
                Log.d(TAG, "response.body(): " + response.body().getExample());
    
    
            }
    
            @Override
            public void onFailure(Call<Example> call, Throwable t) {
                // Log error here since request failed
    
                Log.e(TAG, t.toString());
            }
        });
    
    Call Call=apiService.getData(URL);
    call.enqueue(新回调(){
    @凌驾
    公共void onResponse(调用、响应){
    Log.d(标记,“response.body():”+response.body().getExample());
    }
    @凌驾
    失败时公共无效(调用调用,可丢弃的t){
    //由于请求失败,此处记录错误
    Log.e(TAG,t.toString());
    }
    });
    
    我希望它能帮助你解决问题

  • 您可以使用改装并从URL获取数据:

    你可以参考:

  • 您可以使用上面的对象

  • 使用JSON字符串,我使用并创建对象:

    -----------------------------------com.example.Example.java-----------------------------------
    
    package com.example;
    
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Example {
    
    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("full_name")
    @Expose
    private String fullName;
    @SerializedName("owner")
    @Expose
    private Owner owner;
    @SerializedName("private")
    @Expose
    private Boolean _private;
    @SerializedName("html_url")
    @Expose
    private String htmlUrl;
    @SerializedName("description")
    @Expose
    private String description;
    @SerializedName("fork")
    @Expose
    private Boolean fork;
    @SerializedName("url")
    @Expose
    private String url;
    @SerializedName("forks_url")
    @Expose
    private String forksUrl;
    @SerializedName("keys_url")
    @Expose
    private String keysUrl;
    @SerializedName("collaborators_url")
    @Expose
    private String collaboratorsUrl;
    @SerializedName("teams_url")
    @Expose
    private String teamsUrl;
    @SerializedName("hooks_url")
    @Expose
    private String hooksUrl;
    @SerializedName("issue_events_url")
    @Expose
    private String issueEventsUrl;
    @SerializedName("events_url")
    @Expose
    private String eventsUrl;
    @SerializedName("assignees_url")
    @Expose
    private String assigneesUrl;
    @SerializedName("branches_url")
    @Expose
    private String branchesUrl;
    @SerializedName("tags_url")
    @Expose
    private String tagsUrl;
    @SerializedName("blobs_url")
    @Expose
    private String blobsUrl;
    @SerializedName("git_tags_url")
    @Expose
    private String gitTagsUrl;
    @SerializedName("git_refs_url")
    @Expose
    private String gitRefsUrl;
    @SerializedName("trees_url")
    @Expose
    private String treesUrl;
    @SerializedName("statuses_url")
    @Expose
    private String statusesUrl;
    @SerializedName("languages_url")
    @Expose
    private String languagesUrl;
    @SerializedName("stargazers_url")
    @Expose
    private String stargazersUrl;
    @SerializedName("contributors_url")
    @Expose
    private String contributorsUrl;
    @SerializedName("subscribers_url")
    @Expose
    private String subscribersUrl;
    @SerializedName("subscription_url")
    @Expose
    private String subscriptionUrl;
    @SerializedName("commits_url")
    @Expose
    private String commitsUrl;
    @SerializedName("git_commits_url")
    @Expose
    private String gitCommitsUrl;
    @SerializedName("comments_url")
    @Expose
    private String commentsUrl;
    @SerializedName("issue_comment_url")
    @Expose
    private String issueCommentUrl;
    @SerializedName("contents_url")
    @Expose
    private String contentsUrl;
    @SerializedName("compare_url")
    @Expose
    private String compareUrl;
    @SerializedName("merges_url")
    @Expose
    private String mergesUrl;
    @SerializedName("archive_url")
    @Expose
    private String archiveUrl;
    @SerializedName("downloads_url")
    @Expose
    private String downloadsUrl;
    @SerializedName("issues_url")
    @Expose
    private String issuesUrl;
    @SerializedName("pulls_url")
    @Expose
    private String pullsUrl;
    @SerializedName("milestones_url")
    @Expose
    private String milestonesUrl;
    @SerializedName("notifications_url")
    @Expose
    private String notificationsUrl;
    @SerializedName("labels_url")
    @Expose
    private String labelsUrl;
    @SerializedName("releases_url")
    @Expose
    private String releasesUrl;
    @SerializedName("deployments_url")
    @Expose
    private String deploymentsUrl;
    @SerializedName("created_at")
    @Expose
    private String createdAt;
    @SerializedName("updated_at")
    @Expose
    private String updatedAt;
    @SerializedName("pushed_at")
    @Expose
    private String pushedAt;
    @SerializedName("git_url")
    @Expose
    private String gitUrl;
    @SerializedName("ssh_url")
    @Expose
    private String sshUrl;
    @SerializedName("clone_url")
    @Expose
    private String cloneUrl;
    @SerializedName("svn_url")
    @Expose
    private String svnUrl;
    @SerializedName("homepage")
    @Expose
    private String homepage;
    @SerializedName("size")
    @Expose
    private Integer size;
    @SerializedName("stargazers_count")
    @Expose
    private Integer stargazersCount;
    @SerializedName("watchers_count")
    @Expose
    private Integer watchersCount;
    @SerializedName("language")
    @Expose
    private String language;
    @SerializedName("has_issues")
    @Expose
    private Boolean hasIssues;
    @SerializedName("has_projects")
    @Expose
    private Boolean hasProjects;
    @SerializedName("has_downloads")
    @Expose
    private Boolean hasDownloads;
    @SerializedName("has_wiki")
    @Expose
    private Boolean hasWiki;
    @SerializedName("has_pages")
    @Expose
    private Boolean hasPages;
    @SerializedName("forks_count")
    @Expose
    private Integer forksCount;
    @SerializedName("mirror_url")
    @Expose
    private Object mirrorUrl;
    @SerializedName("archived")
    @Expose
    private Boolean archived;
    @SerializedName("open_issues_count")
    @Expose
    private Integer openIssuesCount;
    @SerializedName("forks")
    @Expose
    private Integer forks;
    @SerializedName("open_issues")
    @Expose
    private Integer openIssues;
    @SerializedName("watchers")
    @Expose
    private Integer watchers;
    @SerializedName("default_branch")
    @Expose
    private String defaultBranch;
    
    public Integer getId() {
    return id;
    }
    
    public void setId(Integer id) {
    this.id = id;
    }
    
    public String getName() {
    return name;
    }
    
    public void setName(String name) {
    this.name = name;
    }
    
    public String getFullName() {
    return fullName;
    }
    
    public void setFullName(String fullName) {
    this.fullName = fullName;
    }
    
    public Owner getOwner() {
    return owner;
    }
    
    public void setOwner(Owner owner) {
    this.owner = owner;
    }
    
    public Boolean getPrivate() {
    return _private;
    }
    
    public void setPrivate(Boolean _private) {
    this._private = _private;
    }
    
    public String getHtmlUrl() {
    return htmlUrl;
    }
    
    public void setHtmlUrl(String htmlUrl) {
    this.htmlUrl = htmlUrl;
    }
    
    public String getDescription() {
    return description;
    }
    
    public void setDescription(String description) {
    this.description = description;
    }
    
    public Boolean getFork() {
    return fork;
    }
    
    public void setFork(Boolean fork) {
    this.fork = fork;
    }
    
    public String getUrl() {
    return url;
    }
    
    public void setUrl(String url) {
    this.url = url;
    }
    
    public String getForksUrl() {
    return forksUrl;
    }
    
    public void setForksUrl(String forksUrl) {
    this.forksUrl = forksUrl;
    }
    
    public String getKeysUrl() {
    return keysUrl;
    }
    
    public void setKeysUrl(String keysUrl) {
    this.keysUrl = keysUrl;
    }
    
    public String getCollaboratorsUrl() {
    return collaboratorsUrl;
    }
    
    public void setCollaboratorsUrl(String collaboratorsUrl) {
    this.collaboratorsUrl = collaboratorsUrl;
    }
    
    public String getTeamsUrl() {
    return teamsUrl;
    }
    
    public void setTeamsUrl(String teamsUrl) {
    this.teamsUrl = teamsUrl;
    }
    
    public String getHooksUrl() {
    return hooksUrl;
    }
    
    public void setHooksUrl(String hooksUrl) {
    this.hooksUrl = hooksUrl;
    }
    
    public String getIssueEventsUrl() {
    return issueEventsUrl;
    }
    
    public void setIssueEventsUrl(String issueEventsUrl) {
    this.issueEventsUrl = issueEventsUrl;
    }
    
    public String getEventsUrl() {
    return eventsUrl;
    }
    
    public void setEventsUrl(String eventsUrl) {
    this.eventsUrl = eventsUrl;
    }
    
    public String getAssigneesUrl() {
    return assigneesUrl;
    }
    
    public void setAssigneesUrl(String assigneesUrl) {
    this.assigneesUrl = assigneesUrl;
    }
    
    public String getBranchesUrl() {
    return branchesUrl;
    }
    
    public void setBranchesUrl(String branchesUrl) {
    this.branchesUrl = branchesUrl;
    }
    
    public String getTagsUrl() {
    return tagsUrl;
    }
    
    public void setTagsUrl(String tagsUrl) {
    this.tagsUrl = tagsUrl;
    }
    
    public String getBlobsUrl() {
    return blobsUrl;
    }
    
    public void setBlobsUrl(String blobsUrl) {
    this.blobsUrl = blobsUrl;
    }
    
    public String getGitTagsUrl() {
    return gitTagsUrl;
    }
    
    public void setGitTagsUrl(String gitTagsUrl) {
    this.gitTagsUrl = gitTagsUrl;
    }
    
    public String getGitRefsUrl() {
    return gitRefsUrl;
    }
    
    public void setGitRefsUrl(String gitRefsUrl) {
    this.gitRefsUrl = gitRefsUrl;
    }
    
    public String getTreesUrl() {
    return treesUrl;
    }
    
    public void setTreesUrl(String treesUrl) {
    this.treesUrl = treesUrl;
    }
    
    public String getStatusesUrl() {
    return statusesUrl;
    }
    
    public void setStatusesUrl(String statusesUrl) {
    this.statusesUrl = statusesUrl;
    }
    
    public String getLanguagesUrl() {
    return languagesUrl;
    }
    
    public void setLanguagesUrl(String languagesUrl) {
    this.languagesUrl = languagesUrl;
    }
    
    public String getStargazersUrl() {
    return stargazersUrl;
    }
    
    public void setStargazersUrl(String stargazersUrl) {
    this.stargazersUrl = stargazersUrl;
    }
    
    public String getContributorsUrl() {
    return contributorsUrl;
    }
    
    public void setContributorsUrl(String contributorsUrl) {
    this.contributorsUrl = contributorsUrl;
    }
    
    public String getSubscribersUrl() {
    return subscribersUrl;
    }
    
    public void setSubscribersUrl(String subscribersUrl) {
    this.subscribersUrl = subscribersUrl;
    }
    
    public String getSubscriptionUrl() {
    return subscriptionUrl;
    }
    
    public void setSubscriptionUrl(String subscriptionUrl) {
    this.subscriptionUrl = subscriptionUrl;
    }
    
    public String getCommitsUrl() {
    return commitsUrl;
    }
    
    public void setCommitsUrl(String commitsUrl) {
    this.commitsUrl = commitsUrl;
    }
    
    public String getGitCommitsUrl() {
    return gitCommitsUrl;
    }
    
    public void setGitCommitsUrl(String gitCommitsUrl) {
    this.gitCommitsUrl = gitCommitsUrl;
    }
    
    public String getCommentsUrl() {
    return commentsUrl;
    }
    
    public void setCommentsUrl(String commentsUrl) {
    this.commentsUrl = commentsUrl;
    }
    
    public String getIssueCommentUrl() {
    return issueCommentUrl;
    }
    
    public void setIssueCommentUrl(String issueCommentUrl) {
    this.issueCommentUrl = issueCommentUrl;
    }
    
    public String getContentsUrl() {
    return contentsUrl;
    }
    
    public void setContentsUrl(String contentsUrl) {
    this.contentsUrl = contentsUrl;
    }
    
    public String getCompareUrl() {
    return compareUrl;
    }
    
    public void setCompareUrl(String compareUrl) {
    this.compareUrl = compareUrl;
    }
    
    public String getMergesUrl() {
    return mergesUrl;
    }
    
    public void setMergesUrl(String mergesUrl) {
    this.mergesUrl = mergesUrl;
    }
    
    public String getArchiveUrl() {
    return archiveUrl;
    }
    
    public void setArchiveUrl(String archiveUrl) {
    this.archiveUrl = archiveUrl;
    }
    
    public String getDownloadsUrl() {
    return downloadsUrl;
    }
    
    public void setDownloadsUrl(String downloadsUrl) {
    this.downloadsUrl = downloadsUrl;
    }
    
    public String getIssuesUrl() {
    return issuesUrl;
    }
    
    public void setIssuesUrl(String issuesUrl) {
    this.issuesUrl = issuesUrl;
    }
    
    public String getPullsUrl() {
    return pullsUrl;
    }
    
    public void setPullsUrl(String pullsUrl) {
    this.pullsUrl = pullsUrl;
    }
    
    public String getMilestonesUrl() {
    return milestonesUrl;
    }
    
    public void setMilestonesUrl(String milestonesUrl) {
    this.milestonesUrl = milestonesUrl;
    }
    
    public String getNotificationsUrl() {
    return notificationsUrl;
    }
    
    public void setNotificationsUrl(String notificationsUrl) {
    this.notificationsUrl = notificationsUrl;
    }
    
    public String getLabelsUrl() {
    return labelsUrl;
    }
    
    public void setLabelsUrl(String labelsUrl) {
    this.labelsUrl = labelsUrl;
    }
    
    public String getReleasesUrl() {
    return releasesUrl;
    }
    
    public void setReleasesUrl(String releasesUrl) {
    this.releasesUrl = releasesUrl;
    }
    
    public String getDeploymentsUrl() {
    return deploymentsUrl;
    }
    
    public void setDeploymentsUrl(String deploymentsUrl) {
    this.deploymentsUrl = deploymentsUrl;
    }
    
    public String getCreatedAt() {
    return createdAt;
    }
    
    public void setCreatedAt(String createdAt) {
    this.createdAt = createdAt;
    }
    
    public String getUpdatedAt() {
    return updatedAt;
    }
    
    public void setUpdatedAt(String updatedAt) {
    this.updatedAt = updatedAt;
    }
    
    public String getPushedAt() {
    return pushedAt;
    }
    
    public void setPushedAt(String pushedAt) {
    this.pushedAt = pushedAt;
    }
    
    public String getGitUrl() {
    return gitUrl;
    }
    
    public void setGitUrl(String gitUrl) {
    this.gitUrl = gitUrl;
    }
    
    public String getSshUrl() {
    return sshUrl;
    }
    
    public void setSshUrl(String sshUrl) {
    this.sshUrl = sshUrl;
    }
    
    public String getCloneUrl() {
    return cloneUrl;
    }
    
    public void setCloneUrl(String cloneUrl) {
    this.cloneUrl = cloneUrl;
    }
    
    public String getSvnUrl() {
    return svnUrl;
    }
    
    public void setSvnUrl(String svnUrl) {
    this.svnUrl = svnUrl;
    }
    
    public String getHomepage() {
    return homepage;
    }
    
    public void setHomepage(String homepage) {
    this.homepage = homepage;
    }
    
    public Integer getSize() {
    return size;
    }
    
    public void setSize(Integer size) {
    this.size = size;
    }
    
    public Integer getStargazersCount() {
    return stargazersCount;
    }
    
    public void setStargazersCount(Integer stargazersCount) {
    this.stargazersCount = stargazersCount;
    }
    
    public Integer getWatchersCount() {
    return watchersCount;
    }
    
    public void setWatchersCount(Integer watchersCount) {
    this.watchersCount = watchersCount;
    }
    
    public String getLanguage() {
    return language;
    }
    
    public void setLanguage(String language) {
    this.language = language;
    }
    
    public Boolean getHasIssues() {
    return hasIssues;
    }
    
    public void setHasIssues(Boolean hasIssues) {
    this.hasIssues = hasIssues;
    }
    
    public Boolean getHasProjects() {
    return hasProjects;
    }
    
    public void setHasProjects(Boolean hasProjects) {
    this.hasProjects = hasProjects;
    }
    
    public Boolean getHasDownloads() {
    return hasDownloads;
    }
    
    public void setHasDownloads(Boolean hasDownloads) {
    this.hasDownloads = hasDownloads;
    }
    
    public Boolean getHasWiki() {
    return hasWiki;
    }
    
    public void setHasWiki(Boolean hasWiki) {
    this.hasWiki = hasWiki;
    }
    
    public Boolean getHasPages() {
    return hasPages;
    }
    
    public void setHasPages(Boolean hasPages) {
    this.hasPages = hasPages;
    }
    
    public Integer getForksCount() {
    return forksCount;
    }
    
    public void setForksCount(Integer forksCount) {
    this.forksCount = forksCount;
    }
    
    public Object getMirrorUrl() {
    return mirrorUrl;
    }
    
    public void setMirrorUrl(Object mirrorUrl) {
    this.mirrorUrl = mirrorUrl;
    }
    
    public Boolean getArchived() {
    return archived;
    }
    
    public void setArchived(Boolean archived) {
    this.archived = archived;
    }
    
    public Integer getOpenIssuesCount() {
    return openIssuesCount;
    }
    
    public void setOpenIssuesCount(Integer openIssuesCount) {
    this.openIssuesCount = openIssuesCount;
    }
    
    public Integer getForks() {
    return forks;
    }
    
    public void setForks(Integer forks) {
    this.forks = forks;
    }
    
    public Integer getOpenIssues() {
    return openIssues;
    }
    
    public void setOpenIssues(Integer openIssues) {
    this.openIssues = openIssues;
    }
    
    public Integer getWatchers() {
    return watchers;
    }
    
    public void setWatchers(Integer watchers) {
    this.watchers = watchers;
    }
    
    public String getDefaultBranch() {
    return defaultBranch;
    }
    
    public void setDefaultBranch(String defaultBranch) {
    this.defaultBranch = defaultBranch;
    }
    
    }
    
    -----------------------------------com.example.Owner.java-----------------------------------

    package com.example;
    
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Owner {
    
    @SerializedName("login")
    @Expose
    private String login;
    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("avatar_url")
    @Expose
    private String avatarUrl;
    @SerializedName("gravatar_id")
    @Expose
    private String gravatarId;
    @SerializedName("url")
    @Expose
    private String url;
    @SerializedName("html_url")
    @Expose
    private String htmlUrl;
    @SerializedName("followers_url")
    @Expose
    private String followersUrl;
    @SerializedName("following_url")
    @Expose
    private String followingUrl;
    @SerializedName("gists_url")
    @Expose
    private String gistsUrl;
    @SerializedName("starred_url")
    @Expose
    private String starredUrl;
    @SerializedName("subscriptions_url")
    @Expose
    private String subscriptionsUrl;
    @SerializedName("organizations_url")
    @Expose
    private String organizationsUrl;
    @SerializedName("repos_url")
    @Expose
    private String reposUrl;
    @SerializedName("events_url")
    @Expose
    private String eventsUrl;
    @SerializedName("received_events_url")
    @Expose
    private String receivedEventsUrl;
    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("site_admin")
    @Expose
    private Boolean siteAdmin;
    
    public String getLogin() {
    return login;
    }
    
    public void setLogin(String login) {
    this.login = login;
    }
    
    public Integer getId() {
    return id;
    }
    
    public void setId(Integer id) {
    this.id = id;
    }
    
    public String getAvatarUrl() {
    return avatarUrl;
    }
    
    public void setAvatarUrl(String avatarUrl) {
    this.avatarUrl = avatarUrl;
    }
    
    public String getGravatarId() {
    return gravatarId;
    }
    
    public void setGravatarId(String gravatarId) {
    this.gravatarId = gravatarId;
    }
    
    public String getUrl() {
    return url;
    }
    
    public void setUrl(String url) {
    this.url = url;
    }
    
    public String getHtmlUrl() {
    return htmlUrl;
    }
    
    public void setHtmlUrl(String htmlUrl) {
    this.htmlUrl = htmlUrl;
    }
    
    public String getFollowersUrl() {
    return followersUrl;
    }
    
    public void setFollowersUrl(String followersUrl) {
    this.followersUrl = followersUrl;
    }
    
    public String getFollowingUrl() {
    return followingUrl;
    }
    
    public void setFollowingUrl(String followingUrl) {
    this.followingUrl = followingUrl;
    }
    
    public String getGistsUrl() {
    return gistsUrl;
    }
    
    public void setGistsUrl(String gistsUrl) {
    this.gistsUrl = gistsUrl;
    }
    
    public String getStarredUrl() {
    return starredUrl;
    }
    
    public void setStarredUrl(String starredUrl) {
    this.starredUrl = starredUrl;
    }
    
    public String getSubscriptionsUrl() {
    return subscriptionsUrl;
    }
    
    public void setSubscriptionsUrl(String subscriptionsUrl) {
    this.subscriptionsUrl = subscriptionsUrl;
    }
    
    public String getOrganizationsUrl() {
    return organizationsUrl;
    }
    
    public void setOrganizationsUrl(String organizationsUrl) {
    this.organizationsUrl = organizationsUrl;
    }
    
    public String getReposUrl() {
    return reposUrl;
    }
    
    public void setReposUrl(String reposUrl) {
    this.reposUrl = reposUrl;
    }
    
    public String getEventsUrl() {
    return eventsUrl;
    }
    
    public void setEventsUrl(String eventsUrl) {
    this.eventsUrl = eventsUrl;
    }
    
    public String getReceivedEventsUrl() {
    return receivedEventsUrl;
    }
    
    public void setReceivedEventsUrl(String receivedEventsUrl) {
    this.receivedEventsUrl = receivedEventsUrl;
    }
    
    public String getType() {
    return type;
    }
    
    public void setType(String type) {
    this.type = type;
    }
    
    public Boolean getSiteAdmin() {
    return siteAdmin;
    }
    
    public void setSiteAdmin(Boolean siteAdmin) {
    this.siteAdmin = siteAdmin;
    }
    
    }
    
    以及使用改装,例如:

     Call<Example> call = apiService.getData(URL);
    
    
        call.enqueue(new Callback<Example>() {
            @Override
            public void onResponse(Call<Example> call, Response<Example> response) {
    
                Log.d(TAG, "response.body(): " + response.body().getExample());
    
    
            }
    
            @Override
            public void onFailure(Call<Example> call, Throwable t) {
                // Log error here since request failed
    
                Log.e(TAG, t.toString());
            }
        });
    
    Call Call=apiService.getData(URL);
    call.enqueue(新回调(){
    @凌驾
    公共void onResponse(调用、响应){
    Log.d(标记,“response.body():”+response.body().getExample());
    }
    @凌驾
    失败时公共无效(调用调用,可丢弃的t){
    //由于请求失败,此处记录错误
    Log.e(TAG,t.toString());
    }
    });
    

    我希望它能帮助你解决问题

    第三方库(如Reformation)当然比手动遍历数据效果更好,但如果您不愿意这样做,如果生成的数据是JSONObject,您可以按照以下步骤检索结果:

    public void formatJSONObjectData(String results){
        try {
            JSONObject jsonObject = new JSONObject(results);
            if(jsonObject.optString("login")!=null)
                tvData.append(jsonObject.getString("login"));
            if (jsonObject.optInt("id")!=null)
                tvData.append(jsonObject.getString("id"));
            if(jsonObject.optString("avatar_url")!=null)
                tvData.append(jsonObject.getString("avatar_url"));
    
            //you could accordingly retrieve the results from the jsonObject for a specfic key and append it to the TextView
        }catch (JSONException jsonException){
            Log.e("Error",jsonException.getMessage());
        }
    }
    
    如果生成的数据是JSONArray,则需要以以下方式对其进行解析:

    public void formatJSONArray(String results){
        try {
            JSONArray jsonArray = new JSONArray(results);
            for(int i=0;i<jsonArray.length();i++){
                JSONObject jsonObject=jsonArray.getJSONObject(i);
                if(jsonObject.optString("name")!=null)
                    tvData.append(jsonObject.getString("name"));
                if (jsonObject.optInt("id")!=null)
                    tvData.append(jsonObject.getString("id"));
                if(jsonObject.optString("full_name")!=null)
                    tvData.append(jsonObject.getString("full_name"));
    
                if(jsonObject.optJSONObject("owner")!=null){
                    JSONObject ownerObject=jsonObject.getJSONObject("owner");
                    if(ownerObject.optString("login")!=null)
                        tvData.append(ownerObject.getString("login"));
                    if (ownerObject.optInt("id")!=null)
                        tvData.append(ownerObject.getString("id"));
                    if(jsonObject.optString("avatar_url")!=null)
                        tvData.append(ownerObject.getString("avatar_url"));
                }
            }
        }catch (JSONException jsonException){
    
        }
    }
    

    第三方库(如Reformation)当然比手动遍历数据效果更好,但如果您不愿意这样做,那么如果生成的数据是JSONObject,您可以按照以下步骤检索结果:

    public void formatJSONObjectData(String results){
        try {
            JSONObject jsonObject = new JSONObject(results);
            if(jsonObject.optString("login")!=null)
                tvData.append(jsonObject.getString("login"));
            if (jsonObject.optInt("id")!=null)
                tvData.append(jsonObject.getString("id"));
            if(jsonObject.optString("avatar_url")!=null)
                tvData.append(jsonObject.getString("avatar_url"));
    
            //you could accordingly retrieve the results from the jsonObject for a specfic key and append it to the TextView
        }catch (JSONException jsonException){
            Log.e("Error",jsonException.getMessage());
        }
    }
    
    如果生成的数据是JSONArray,则需要以以下方式对其进行解析:

    public void formatJSONArray(String results){
        try {
            JSONArray jsonArray = new JSONArray(results);
            for(int i=0;i<jsonArray.length();i++){
                JSONObject jsonObject=jsonArray.getJSONObject(i);
                if(jsonObject.optString("name")!=null)
                    tvData.append(jsonObject.getString("name"));
                if (jsonObject.optInt("id")!=null)
                    tvData.append(jsonObject.getString("id"));
                if(jsonObject.optString("full_name")!=null)
                    tvData.append(jsonObject.getString("full_name"));
    
                if(jsonObject.optJSONObject("owner")!=null){
                    JSONObject ownerObject=jsonObject.getJSONObject("owner");
                    if(ownerObject.optString("login")!=null)
                        tvData.append(ownerObject.getString("login"));
                    if (ownerObject.optInt("id")!=null)
                        tvData.append(ownerObject.getString("id"));
                    if(jsonObject.optString("avatar_url")!=null)
                        tvData.append(ownerObject.getString("avatar_url"));
                }
            }
        }catch (JSONException jsonException){
    
        }
    }
    
    您可以使用json来创建j