如何使用Retrofit

时间:2015-08-15 04:27:54

标签: java android json retrofit realm

我对android很新,并且使用改造和领域。我很难从url获取这个json字符串来正确解析。 这是我到目前为止所做的。

我的界面:

package com.example.stephen.traveland.Rest;
import android.database.Observable;
import com.example.stephen.traveland.Models.Country;
import java.util.List;
import retrofit.http.GET;

public interface TravelAnDInterface {
    @GET("/index-updated.json/")
    Observable<List<Country>> getAllCountries();
}

这个java类是我使用接口从URL中提取字符串的地方。

package com.example.stephen.traveland.Rest;
import android.content.Context;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.squareup.okhttp.OkHttpClient;

import io.realm.RealmObject;
import retrofit.RestAdapter;
import retrofit.android.AndroidLog;
import retrofit.client.OkClient;
import retrofit.converter.GsonConverter;

public class TravelAnD {
    private static final String URL = "http://data.international.gc.ca/travel-voyage/";
    private final static Gson gson = new GsonBuilder()
        .setExclusionStrategies(new ExclusionStrategy() {
            @Override
            public boolean shouldSkipField(FieldAttributes f) {
                return f.getDeclaringClass().equals(RealmObject.class);
            }

            @Override
            public boolean shouldSkipClass(Class<?> clazz) {
                return false;
            }
        })
        .create();

    private static TravelAnDInterface sTravelAnDSvc;

    public static TravelAnDInterface getCountries() {

        if (sTravelAnDSvc == null) {
            RestAdapter restAdapter = new RestAdapter.Builder()
                    .setEndpoint(URL)
                    .setClient(new OkClient(new OkHttpClient()))
                    .setConverter(new GsonConverter(gson))
                    .setLogLevel(RestAdapter.LogLevel.FULL)
                    .setLog(new AndroidLog("RETROFIT"))
                    .build();

            sTravelAnDSvc = restAdapter.create(TravelAnDInterface.class);
        }

        return sTravelAnDSvc;
    }
}

这是我的一个对象类,所以你可以看到我是如何完成的。

package com.example.stephen.traveland.Models;

import io.realm.RealmList;
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;

public class Country extends RealmObject {

    @PrimaryKey
    private int country_id;
    private String country_iso;
    private String country_in_english;
    private String country_in_french;
    private int advisory_state;
    private RealmList<DatePublished> datePublished;
    private int advisory_warning;
    private int regional_advisory;
    private int content;
    private String recent_update_type;
    private RealmList<English> english;
    private RealmList<French> french;

    public Country() { super(); }

    public Country(int country_id, String country_iso, String     country_in_english, String country_in_french,
               int advisory_state, int advisory_warning, int regional_advisory, int content) {
        this.country_id = country_id;
        this.country_iso = country_iso;
        this.country_in_english = country_in_english;
        this.country_in_french = country_in_french;
        this.advisory_state = advisory_state;
        this.advisory_warning = advisory_warning;
        this.regional_advisory = regional_advisory;
        this.content = content;

    }

    public int getAdvisory_state() { return advisory_state; }

    public void setAdvisory_state(int advisory_state) { this.advisory_state = advisory_state; }

    public int getAdvisory_warning() { return advisory_warning; }

    public void setAdvisory_warning(int advisory_warning) { this.advisory_warning = advisory_warning; }

    public int getContent() { return content; }

    public void setContent(int content) { this.content = content; }

    public int getCountry_id() { return country_id; }

    public void setCountry_id(int country_id) { this.country_id = country_id; }

    public String getCountry_in_english() { return country_in_english; }

    public void setCountry_in_english(String country_in_english) { this.country_in_english = country_in_english; }

    public String getCountry_in_french() { return country_in_french; }

    public void setCountry_in_french(String country_in_french) { this.country_in_french = country_in_french; }

    public String getCountry_iso() { return country_iso; }

    public void setCountry_iso(String country_iso) {this.country_iso = country_iso; }

    public RealmList<DatePublished> getDatePublished() { return datePublished; }

    public void setDatePublished(RealmList<DatePublished> datePublished) { this.datePublished = datePublished; }

    public RealmList<English> getEnglish() { return english; }

    public void setEnglish(RealmList<English> english) {this.english = english;}

    public RealmList<French> getFrench() { return french; }

    public void setFrench(RealmList<French> french) { this.french = french; }

    public String getRecent_update_type() { return recent_update_type; }

    public void setRecent_update_type(String recent_update_type) { this.recent_update_type = recent_update_type; }

    public int getRegional_advisory() { return regional_advisory; }

    public void setRegional_advisory(int regional_advisory) { this.regional_advisory = regional_advisory; }

}

我已经创建了用于存储数据的类,但遇到了问题,因为字符串本身很奇怪且难以使用。我希望得到一些指导我做错了什么。任何帮助将不胜感激。

@EDIT 不再使用Realm用于我的任何对象,并且之前的代码已经被更改了很多,因为数据太大而无法存储在手机上。对于一台设备来说,这将是太多的信息。

但是,在不同情况下出现新问题或同样问题。我的retofit调用正在抓取数据,当我将它们放入对象时,当我想从中提取数据时,对象为null。以下是我的代码的一些小片段。

public void gatherData() {

    final ProgressDialog progress = new ProgressDialog(this);
    progress.setTitle("Please wait...");
    progress.setMessage("Loading data...");
    progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progress.setIndeterminate(false);
    progress.show();

    bossModel1 = new BossModel();

    Gson gson = new GsonBuilder()
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
            .create();

    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint("http://data.international.gc.ca/travel-voyage")
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .build();

    Api service = restAdapter.create(Api.class);

    service.listRepos(new Callback<BossModel>() {
        @Override
        public void success(BossModel bossModel, Response response) {
            bossModel1 = bossModel;

            Log.i("SUCCESS", "" + bossModel1.getData());

            for (Field field : bossModel1.getData().getClass().getDeclaredFields()) {
                if (!Modifier.isStatic(field.getModifiers())) {
                    Log.i("ahlsdkjha: ", field.getName());
                    countryISO.add(field.getName());
                }
            }

            progress.dismiss();
        }

        @Override
        public void failure(RetrofitError error) {

        }
    });
}

public void gatherVPData(final String countryIso) {
    vpModel1 = new VPModel();

    Gson gson = new GsonBuilder()
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
            .create();

    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint("http://data.international.gc.ca/travel-voyage")
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .build();

    Api service = restAdapter.create(Api.class);

    service.getCountry(countryIso, new Callback<VPModel>() {

        @Override
        public void success(VPModel vpModel, Response response) {
            vpModel1 = vpModel;
            Log.d("SUCCESS2: ", "" + vpModel1.getData());
        }

        @Override
        public void failure(RetrofitError error) {

        }
    });
}

API

public interface Api {
    @GET("/index-updated.json")
    void listRepos(Callback<BossModel> callback);

    @GET("/countries/cta-cap-{country}.json")
    void getCountry(@Path("country") String countryIso, Callback<VPModel> callback);
}

这里是我尝试使用gatherVPData提取的数据填充新对象列表的地方。由于某些原因数据为空,因此无法填充。

public void populateCountryList(int id) {

    for (int i = 0; i < countryISO.size() - 1; i++) {
        gatherVPData(countryISO.get(i));
        if (vpModel1.getData().getAdvisoryState() == id) {
            countries.add(new Country(
                    vpModel1.getData().getCountryIso(),
                    vpModel1.getData().getAdvisoryState(),
                    vpModel1.getData().getHasAdvisoryWarning(),
                    vpModel1.getData().getHasRegionalAdvisory(),
                    vpModel1.getData().getHasContent(),
                    vpModel1.getData().getUpdateMetadata(),
                    vpModel1.getData().getEng(),
                    vpModel1.getData().getFra()
            ));
        }
    }
}

2 个答案:

答案 0 :(得分:0)

我会这样:

a /将getAllCountries的结果声明为JsonObject

JsonObject getAllCountries();

b /当您调用方法时,将其转换为获取内容:

JsonObject json = <your adapter here>.getAllCountries();
Map<String, Country> countries = gson.fromJson(jsonResponse.getAsJsonObject("data"),
                    new TypeToken<Map<String, Country>>() {
                    }.getType());

基本上你得到"data"中的对象,它是一个JsonObject,并将其解析为String of Map&gt; Country。

答案 1 :(得分:0)

这对你有用

Retrofit Example