2015-12-02 105 views
0

我剛開始使用Retrofit。我正在開發一個使用SimpleXML的項目。我試圖從該網站獲取http://www.supermarketapi.com XML,但我得到以下錯誤:Android - 如何使用Retrofit SimpleXML解析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 = "**********"; 

在此先感謝!

回答

0

添加到您的build.gradledependencies下):

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

(更換$project.retrofitVersion與您正在使用的版本,想必2.0.0-β1或2.0.0-β2)。

這行添加到您Retrofit.Builder

.addCallAdapterFactory(RxJavaCallAdapterFactory.create()) 

如果我沒有記錯你的錯誤無關,與XML,但一個事實,即改造2現在需要的RxJava適配器能夠返回Observable s而不是Call s。

+0

我已經在我的build.grade @ david.mihola – Zach

+0

已經有這種依賴關係,那麼你如何將這行添加到你的'Retrofit.Builder'? –

+0

添加了該行,它有幫助!謝謝!現在我收到錯誤:java.lang.RuntimeException:org.simpleframework.xml.core.ElementException:Element'Store'在類中沒有匹配...在第3行的Models.StoreResultModel @ david.mihola – Zach