标签: simple-xml-converter

Android Retrofit 2多个转换器(Gson和SimpleXML)错误

在我的项目中(我正在使用Dagger 2,Retrofit 2和OkHTTP,RxAndroid)我有2个不同的API调用.从一个我收到JSON,从其他 - XML.我研究了 这个链接并在我的Retrofit.Builder()中添加了2个转换器:

@Provides
@Singleton
public Gson providesGson() {
    return new GsonBuilder().create();
}

@Provides
@Singleton
public Retrofit providesRetrofit(@NonNull OkHttpClient okHttpClient, @NonNull Gson gson) {
    return new Retrofit.Builder()
            .baseUrl(ConstantsManager.BASE_URL)
            .client(okHttpClient)
            .addConverterFactory(SimpleXmlConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson))
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();
}
Run Code Online (Sandbox Code Playgroud)

但是,当我收到JSON(XML被正确转换)时,我得到以下内容:

D/OkHttp: {"date":"20.11.2016","bank":"PB","baseCurrency":980,"baseCurrencyLit":"UAH","exchangeRate":[{"baseCurrency":"UAH","currency":"AUD","saleRateNB":19.4452740,"purchaseRateNB":19.4452740},{"baseCurrency":"UAH","currency":"CAD","saleRateNB":19.4047320,"purchaseRateNB":19.4047320},{"baseCurrency":"UAH","currency":"CZK","saleRateNB":1.0322170,"purchaseRateNB":1.0322170,"saleRate":1.0800000,"purchaseRate":0.9800000},{"baseCurrency":"UAH","currency":"DKK","saleRateNB":3.7519280,"purchaseRateNB":3.7519280},{"baseCurrency":"UAH","currency":"HUF","saleRateNB":0.0902556,"purchaseRateNB":0.0902556},{"baseCurrency":"UAH","currency":"ILS","saleRateNB":6.7524710,"purchaseRateNB":6.7524710,"saleRate":7.0000000,"purchaseRate":6.3000000},{"baseCurrency":"UAH","currency":"JPY","saleRateNB":0.2384005,"purchaseRateNB":0.2384005,"saleRate":0.2500000,"purchaseRate":0.2200000},{"baseCurrency":"UAH","currency":"LVL","saleRateNB":0.2384005,"purchaseRateNB":0.2384005},{"baseCurrency":"UAH","currency":"LTL","saleRateNB":0.2384005,"purchaseRateNB":0.2384005},{"baseCurrency":"UAH","currency":"NOK","saleRateNB":3.0724120,"purchaseRateNB":3.0724120,"saleRate":3.2000000,"purchaseRate":2.9000000},{"baseCurrency":"UAH","currency":"SKK","saleRateNB":3.0724120,"purchaseRateNB":3.0724120},{"baseCurrency":"UAH","currency":"SEK","saleRateNB":2.8384710,"purchaseRateNB":2.8384710},{"baseCurrency":"UAH","currency":"CHF","saleRateNB":26.0049080,"purchaseRateNB":26.0049080,"saleRate":27.5000000,"purchaseRate":25.0000000},{"baseCurrency":"UAH","currency":"RUB","saleRateNB":0.4013400,"purchaseRateNB":0.4013400,"saleRate":0.4200000,"purchaseRate":0.4000000},{"baseCurrency":"UAH","currency":"GBP","saleRateNB":32.4460750,"purchaseRateNB":32.4460750,"saleRate":34.0000000,"purchaseRate":31.0000000},{"baseCurrency":"UAH","currency":"USD","saleRateNB":26.0534380,"purchaseRateNB":26.0534380,"saleRate":27.0000000,"purchaseRate":26.6000000},{"baseCurrency":"UAH","currency":"BYR","saleRateNB":26.0534380,"purchaseRateNB":26.0534380},{"baseCurrency":"UAH","currency":"EUR","saleRateNB":27.9214700,"purchaseRateNB":27.9214700,"saleRate":28.6000000,"purchaseRate":28.2000000},{"baseCurrency":"UAH","currency":"GEL","saleRateNB":10.5921530,"purchaseRateNB":10.5921530},{"baseCurrency":"UAH","currency":"PLZ","saleRateNB":6.2818280,"purchaseRateNB":6.2818280,"saleRate":6.6000000,"purchaseRate":5.9000000}]}
D/OkHttp: <-- END HTTP (2377-byte body)
E/DateCurrencyService: Error while loading data occurred!
                   java.lang.RuntimeException: org.xmlpull.v1.XmlPullParserException: Unexpected token (position:TEXT {"date":"20.11.2...@1:2378 in java.io.InputStreamReader@2ec8965) 
                       at retrofit2.converter.simplexml.SimpleXmlResponseBodyConverter.convert(SimpleXmlResponseBodyConverter.java:44)
                       at retrofit2.converter.simplexml.SimpleXmlResponseBodyConverter.convert(SimpleXmlResponseBodyConverter.java:23)
                       at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:117)
                       at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211)
                       at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
                       at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:171)
                       at rx.internal.operators.OperatorSubscribeOn$1$1$1.request(OperatorSubscribeOn.java:80)
                       at rx.Subscriber.setProducer(Subscriber.java:209)
                       at rx.Subscriber.setProducer(Subscriber.java:205)
                       at …
Run Code Online (Sandbox Code Playgroud)

android gson dagger-2 retrofit2 simple-xml-converter

9
推荐指数
1
解决办法
4798
查看次数

在OkHttp工作正常的情况下,Retrofit2响应是错误的请求

(编辑我添加了一个小的演示测试)

我是新手Retrofit.我需要做的是XML从服务器检索文件并将其转换为POJO.问题是我的测试服务器上的所有内容都与静态文件完美配合,但是当我在真实服务器上尝试这个时,我无法使用它Retrofit2,但是当我直接使用时它确实有效OkHttp,所以有些东西是错误的生成Request我猜.

编辑:我切换代码使用String转换器只是简单的事情,因为问题只与响应有关.

这是我的基本实现.

public interface ZedoClient {
@retrofit2.http.Headers({
        "User-Agent: Mozilla/5.0 (Linux; Android 5.1; Quattro_L50_HD LMY47D/Quattro_L50_HD)",
        "Cookie : ZCBC=1;FFcat=3533,3,90;FFad=0;FFMChanCap=5345280B3533,3#2605191|0,1#0,24:"
})
@GET("fns.vast")
Call<Vast> getVast(@QueryMap(encoded=true) Map<String, String> options);
Run Code Online (Sandbox Code Playgroud)

我添加了两个Interceptor以便设置headerscookies: AddCookiesInterceptor.class

class AddCookiesInterceptor implements Interceptor {
    private final Headers headers;

    AddCookiesInterceptor(Headers headers) {
        this.headers = headers;
    }
    @Override
    public okhttp3.Response intercept(Chain chain) throws IOException {
        Request.Builder builder = chain.request().newBuilder();
        builder.headers(headers);
        return chain.proceed(builder.build());
    }
}
Run Code Online (Sandbox Code Playgroud)

ReceivedCookiesInterceptor.class …

java retrofit okhttp retrofit2 simple-xml-converter

9
推荐指数
1
解决办法
801
查看次数

Proguard - PersistenceException:构造函数与类不匹配

retrofit2.0在我的应用程序中使用simpleframework.xml库.

问题是当我没有proguard运行应用程序时它工作正常但是当我运行proguard时我在日志中得到以下错误.

E/ERROR: java.lang.RuntimeException: org.simpleframework.xml.core.PersistenceException: Constructor not matched for class A
Run Code Online (Sandbox Code Playgroud)

A类没有/ default构造函数应该可以工作.我还添加了一个No Argument Constructor.但这并没有纠正这个问题.

@Root(name = "data",strict = false)
public class A {
    @Element(name = "baseurl",required = false)
    private String baseURl;
    @Element(name = "country_code")
    private String country_code;

    //  Setters and getters
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,没有构造函数(添加默认的空构造函数可以解决问题).所以默认的No Argument Constructor应该也能正常工作.但是,我尝试使用以下构造函数,这将删除错误.

public A(@ELement(name = "baseurl") String baseUrl,
         @Element(name = "country_code") String country_code) {    // Add all the elements from the xml in the constructor i.e. …
Run Code Online (Sandbox Code Playgroud)

android proguard xml-deserialization android-proguard simple-xml-converter

7
推荐指数
1
解决办法
669
查看次数

改造和 SimpleXmlConverterFactory

我在改造和 XML 方面遇到问题,我使用 SimpleXmlConverterFactory,并且出现此错误: 原因为:“java.lang.IllegalArgumentException:无法找到 java.util.List 的 ResponseBody 转换器。尝试过:
*retrofit2.BuiltInConverters *retrofit2。转换器.simplexml.SimpleXmlConverterFactory”

等级:

compile 'com.google.code.gson:gson:2.8.1'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.8.0'
compile ('com.squareup.retrofit2:converter-simplexml:2.3.0') {
    exclude group: 'xpp3', module: 'xpp3'
    exclude group: 'stax', module: 'stax-api'
    exclude group: 'stax', module: 'stax'
}
Run Code Online (Sandbox Code Playgroud)

服务 :

        Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(URL)
            .client(buildHttpClient())
            .addConverterFactory(SimpleXmlConverterFactory.create())
            .build();

private OkHttpClient buildHttpClient() {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    return (new OkHttpClient.Builder())
            .addInterceptor(buildDefaultHeadersInterceptor())
            .addNetworkInterceptor(buildLogInterceptor())
            .build();
}
Run Code Online (Sandbox Code Playgroud)

模型 :

    @Root(name = "item")
    public class Item {

    @Element(name = …
Run Code Online (Sandbox Code Playgroud)

java xml android retrofit simple-xml-converter

5
推荐指数
1
解决办法
3991
查看次数

使用Retrofit 2将根单个元素添加到xml

我在xml中有一个服务器响应,该响应格式不正确,也没有根元素:

<option value="stationAValue">stationADescription</option>
<option value="stationBValue">stationBDescription</option>
Run Code Online (Sandbox Code Playgroud)

我试图这样使用SimpleXmlConverterFactory

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(Params.BASE_URL)
    .client(okHttpClient)
    .addConverterFactory(SimpleXmlConverterFactory.create())
    .build();
Run Code Online (Sandbox Code Playgroud)

这是我的类,代表一行:

public class Station {
    @Element(name = "option")
    private String mName;

   @Attribute(required = false)
   private String value;
}
Run Code Online (Sandbox Code Playgroud)

但是,当然,如果没有根元素就无法解析它,是否有一种方法可以在SimpleXmlConverterFactory尝试解析响应之前添加根元素?

还是另一种解决方案?

xml android retrofit2 simple-xml-converter

0
推荐指数
1
解决办法
1005
查看次数

我可以在Android中使用哪种XmlConverter进行改装?

我正在使用Retrofit开发一个Android应用程序。

服务器正在发送XML作为响应。所以我应该转换它。对于Json,我使用了GsonConverter。对于XMl,我想我可以使用SimpleXMLConverter。

我访问了SimpleXMLConverter的GitHub链接:https : //github.com/square/retrofit/tree/master/retrofit-converters/simplexml

我发现此消息:

不推荐使用-请切换到JAXB转换器

所以我访问了JAXBConverter的Github链接:https : //github.com/square/retrofit/tree/master/retrofit-converters/jaxb

但是我发现了以下消息:

请注意,JAXB在Android上不起作用。

嗯...我应该使用什么xml转换器?...

xml android jaxb retrofit simple-xml-converter

0
推荐指数
1
解决办法
551
查看次数