首页 > 解决方案 > 改造 JSON 数组和对象现在工作类

问题描述

我有以下问题,我正在尝试通过改造从 reddit 获取数据,问题是当我尝试访问数组后的最后一个类时,为对象和数组创建类时它不允许我,它只是没有出现,我留下了我的代码,我失去了一天,我没有找到解决方案,如果有人知道,我将不胜感激。

杰森

{
  "kind": "Listing",
  "data": {
  "after": "t3_ouadqs",
  "dist": 1,
  "modhash": "5ogbn4nb68df205978165776cf08b9eaa9b230a92935a3f5b9",
  "geo_filter": "",
  "children": [
    {
      "kind": "t3",
      "data": {
          "subreddit": "Art",
          "title": "Ocean Beach, San Diego - Me, Digital Painting, 2021",
          "url": "https://i.redd.it/ix7w1fjb09e71.png",
          "author": "honeyteaspice",
          "is_video": false         
      }
    }
  ],
  "before": null
  }
}

MainActivity.java

package com.retreddit.retreddit;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.retreddit.retreddit.Interface.MyApiCall;
import com.retreddit.retreddit.Model.Data__1;
import com.retreddit.retreddit.Model.Example;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

TextView text;

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

    text = findViewById(R.id.Texto);


    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://www.reddit.com/r/art/")
        .addConverterFactory(GsonConverterFactory.create())
        .build();

    MyApiCall myApiCall = retrofit.create(MyApiCall.class);

    Call<Example> call = myApiCall.getData();

    call.enqueue(new Callback<Example>() {
        @Override
        public void onResponse(Call<Example> call, Response<Example> response) {

            if(response.code() != 200){
                text.setText("Checa la conexión we, algo salió mal");
                return;
            }else{

                assert response.body() != null;
                String jsony = "Titulo: " + response.body().getData().getChildren();

                text.setText(jsony);
            }



        }

        @Override
        public void onFailure(Call<Example> call, Throwable t) {
            Toast.makeText(MainActivity.this, ""+t, Toast.LENGTH_SHORT).show();
        }
    });
}}

ActivityMain.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/Texto"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</androidx.core.widget.NestedScrollView>

数据.java

package com.retreddit.retreddit.Model;

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

import java.util.List;

public class Data {

@SerializedName("after")
@Expose
private String after;
@SerializedName("dist")
@Expose
private Integer dist;
@SerializedName("modhash")
@Expose
private String modhash;
@SerializedName("geo_filter")
@Expose
private String geoFilter;
@SerializedName("children")
@Expose
private List<Child> children = null;

public String getAfter() {
    return after;
}

public void setAfter(String after) {
    this.after = after;
}

public Integer getDist() {
    return dist;
}

public void setDist(Integer dist) {
    this.dist = dist;
}

public String getModhash() {
    return modhash;
}

public void setModhash(String modhash) {
    this.modhash = modhash;
}

public String getGeoFilter() {
    return geoFilter;
}

public void setGeoFilter(String geoFilter) {
    this.geoFilter = geoFilter;
}

public List<Child> getChildren() {
    return children;
}

public void setChildren(List<Child> children) {
    this.children = children;
}}

Example.java

package com.retreddit.retreddit.Model;

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

public class Example {

@SerializedName("kind")
@Expose
private String kind;
@SerializedName("data")
@Expose
private Data data;

public String getKind() {
    return kind;
}

public void setKind(String kind) {
    this.kind = kind;
}

public Data getData() {
    return data;
}

public void setData(Data data) {
    this.data = data;
}}

孩子

package com.retreddit.retreddit.Model;

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

public class Child {

@SerializedName("kind")
@Expose
private String kind;
@SerializedName("data")
@Expose
private Data__1 data;

public String getKind() {
    return kind;
}

public void setKind(String kind) {
    this.kind = kind;
}

public Data__1 getData() {
    return data;
}

public void setData(Data__1 data) {
    this.data = data;
}}

资料__1

package com.retreddit.retreddit.Model;

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

public class Data__1 {

@SerializedName("is_video")
@Expose
private Boolean isVideo;
@SerializedName("title")
@Expose
private String title;
@SerializedName("url")
@Expose
private String url;
@SerializedName("author")
@Expose
private String author;
@SerializedName("subreddit")
@Expose
private String subreddit;

public Boolean getVideo() {
    return isVideo;
}

public void setVideo(Boolean video) {
    isVideo = video;
}

public String getUrl() {
    return url;
}

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

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}

public String getSubreddit() {
    return subreddit;
}

public void setSubreddit(String subreddit) {
    this.subreddit = subreddit;
}

public Boolean getIsVideo() {
    return isVideo;
}

public void setIsVideo(Boolean isVideo) {
    this.isVideo = isVideo;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}}

MyApiCall.java 这是接口

package com.retreddit.retreddit.Interface;

import com.retreddit.retreddit.Model.Example;

import retrofit2.Call;
import retrofit2.http.GET;

public interface MyApiCall {

@GET("new.json?limit=10")
Call<Example> getData();

}

未找到类 Data__1

标签: javaandroidjsongsonretrofit

解决方案


推荐阅读