【问题标题】:Retrofit2: retrieving a JSONObject is not workingRetrofit2:检索 JSONObject 不起作用
【发布时间】:2015-12-21 04:16:53
【问题描述】:

Retrofit2 可以用来检索 JSONObject 吗?我在 Movie.java 中做错了什么?

Logcat

 [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;
    }
}

Movie.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();
    }
}

【问题讨论】:

  • for line // date = this.release_dates.getJSONObject("theater").toString();,我猜应该是“release_dates”而不是“theater”
  • @Nisarg 我试过了,但结果还是一样。
  • 我猜 date = this.release_dates.getJSONObject("theater").toString();应该是 this.release_dates.getString("theater");此外,您可能会在 logcat 中遇到异常。请保持改造日志开启。
  • 您在使用 GSON 吗?你用的是什么转换器?
  • 还有抛出异常吗?

标签: android json http jsonobject retrofit2


【解决方案1】:

我建议在这种情况下您应该避免使用内部 JSONObject 并使用内部类来表示数据。例如,使用theateronlinedvd 属性创建一个内部ReleaseDates 类,如下所示:

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;
        }
    }
}

【讨论】:

    猜你喜欢
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多