Android - 如何使用Retrofit SimpleXML解析xml?

时间:2015-12-02 22:15:58

标签: android api xml-parsing retrofit rx-android

我刚开始使用Retrofit。我正在开发一个使用SimpleXML的项目。我正在尝试从网站http://www.supermarketapi.com获取XML,但我收到以下错误:

java.lang.IllegalArgumentException: Unable to create call adapter for rx.Observable<com.crosser.brian.shakennotstirred.Models.StoreResultModel> for method SupermarketProvider.getStoreSearchResults

我想要获取的xml看起来像: http://www.supermarketapi.com/api.asmx/StoresByZip

下面我提供了我项目的摘录。不知道我做错了什么。

StoreResultModel文件:

public class StoreResultModel {

  @Root(name = "ArrayOfStore")
  ArrayList<StoreModel> searchStoresResults;

  public ArrayList<StoreModel> getSearchStoresResults(){
      return searchStoresResults;
  } 
}

StoreModel文件:

@Root(name = "Store")
public class StoreModel {

    @Element(name ="Storename")
    public String storename;

    @Element(name ="Address")
    public String address;

    @Element(name ="City")
    public String city;

    @Element(name ="State")
    public String state;

    @Element(name ="Zip")
    public String zip;

    @Element(name ="Phone")
    public String phone;

    @Element(name ="StoreId")
    public int storeId;



    public int getStoreId() {
        return storeId;
    }

    public String getStorename(){
        return storename;
    }

    public String getAddress(){
        return address;
    }

    public String getCity(){
        return city;
    }

    public String getState(){ return state; }

    public String getZip() { return zip; }

    public String getPhone() { return phone; }

    public void setStoreId(int storeID) { this.storeId = storeId; }

    public void setStorename(String storename) { this.storename = storename; }

    public void setAddress(String address) { this.address = address; }

    public void setCity(String city) { this.city = city; }

    public void setState(String state) { this.state = state; }

    public void setZip(String zip) { this.zip = zip; }

    public void setPhone(String phone) {
        this.phone = phone;
    }


}

SuperMarketAPIClient文件:

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(AppDefines.SUPERMARKET_API_URL)
            .addConverterFactory(SimpleXmlConverterFactory.create())
            .build();

我的API调用:

SupermarketAPIClient.getSupermarketProvider()
                    .getStoreSearchResults(AppDefines.SUPERMARKET_API_KEY, ZipCode)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<StoreResultModel>() {

SupermarketProvider文件:

@GET("/api.asmx/StoresByZip")
Observable<StoreResultModel> getStoreSearchResults(@Query("APIKEY") String key,
                                                    @Query("ZipCode") String zip);

AppDefines文件:

public static final String SUPERMARKET_API_URL = "http://www.SupermarketAPI.com";
public static final String SUPERMARKET_API_KEY = "**********";

提前致谢!

1 个答案:

答案 0 :(得分:0)

将此添加到您的build.gradledependencies下):

compile "com.squareup.retrofit:adapter-rxjava:$project.retrofitVersion"

(将$project.retrofitVersion替换为您正在使用的版本,大概是2.0.0-beta1或2.0.0-beta2。

并将此行添加到Retrofit.Builder

.addCallAdapterFactory(RxJavaCallAdapterFactory.create())

如果我没弄错,你的错误与XML无关,但事实上Retrofit 2现在需要RxJava适配器才能返回Observable而不是Call