1

Retrofit2 を使用して JSONObject を取得できますか? Movie.java で何が間違っていますか?

ログキャット

 [Movie] title: Star Wars: The Force Awakens, wiki: https://en.wikipedia.org/wiki/Star_Wars:_The_Force_Awakens
    rel: 

test.json

{
  "title":"Star Wars: The Force Awakens",
  "wiki":"https://en.wikipedia.org/wiki/Star_Wars:_The_Force_Awakens",
  "release_dates": { "theater":"2015.12.17", "online":"2016.2.17", "dvd":"2016.01.17" }
}

ServiceAPI.java

public interface ServiceAPI {
        @GET("/txt2lrn/bollywood/test.json")
        Observable<Movie> getTestRx();
}

MyApplicationB.java

public class MyApplication extends Application {

    private static ServiceAPI mService;

    @Override
    public void onCreate() {
        super.onCreate();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://goanuj.freeshell.org")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        mService = retrofit.create(ServiceAPI.class);
    }

    public static ServiceAPI getServiceAPI() {
        return mService;
    }
}

ムービー.java

public class Movie {
    private final String TAG = getClass().getSimpleName();

    private String title, wiki;
    private JSONObject release_dates;

    public String getTitle() {
        return title;

    }

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

    public String getWiki() {
        return wiki;
    }

    public void setWiki(String wiki) {
        this.wiki = wiki;
    }

    public void setRelease_dates(JSONObject j) {
        Log.d(TAG, "j: " + j.toString());
        this.release_dates = j;
    }

    public String getRelease_dates() {
        String date = "";
        try {
            date = this.release_dates.getJSONObject("theater").toString();
        } catch (JSONException e) {
            Log.d(TAG, e.getMessage());
        }
        return date;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("[Movie] title: ");
        sb.append(this.title);
        sb.append(", wiki: ").append(this.wiki);
        sb.append("\n\trel: ").append(this.getRelease_dates());
        return sb.toString();
    }
}
4

1 に答える 1

0

この場合、内部 JSONObject を避け、内部クラスを使用してデータを表現することをお勧めします。たとえば、次のように、、、およびプロパティを持つ内部クラスを作成ReleaseDatesします。theateronlinedvd

public class Movie {
    private final String TAG = getClass().getSimpleName();

    private String title, wiki;
    private ReleaseDates release_dates;

    public String getTitle() {
        return title;
    }

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

    public String getWiki() {
        return wiki;
    }

    public void setWiki(String wiki) {
        this.wiki = wiki;
    }

    public void setReleaseDates(ReleaseDates releaseDates) {
        this.release_dates = releaseDates;
    }

    public ReleaseDates getReleaseDates() {
        return this.release_dates;
    }

    public static class ReleaseDates {
        private String theater;
        private String online;
        private String dvd;

        public String getTheaterReleaseDate() {
            return theater;
        }

        public String getOnlineReleaseDate() {
            return online;
        }

        public String getDvdReleaseDate() {
            return dvd;
        }
    }
}
于 2015-12-21T17:27:13.367 に答える