LoginSignup
6
6

More than 5 years have passed since last update.

Retrofit2とSimpleXMLを組み合わせて使う

Last updated at Posted at 2016-02-20

Motivation

  • 未だXMLでレスポンスが返ってくるAPIを使っておりまして、XML parserを頑張って実装していましたがだんだん辛くなってきたので、代替案を調べました
  • せっかくなのでRetrofit2を使おうと思い立ち、レスポンスをXMLに変換するクラスも用意されているとのことだったので、雑ですが使い方をまとめておこうと思います

参考にしたサイト

build.gradleへの記述

  • converter-simplexmlを引き込んでいる所、excludeをいくつか指定している点に注意して下さい (これが無いとgradleがエラーを吐く)
dependencies {
    compile 'com.squareup.okhttp3:okhttp:3.1.2'
    compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'

    compile('com.squareup.retrofit2:converter-simplexml:2.0.0-beta4') {
        exclude module: 'stax'
        exclude module: 'stax-api'
        exclude module: 'xpp3'

    }
}

実装例

public interface FetchService {
    @GET("<APIのpath>")
    Call<ResponseClass> fetch();
}

private static List<ResponseClass> doFetch() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://xxxxxx")
            .client(new OkHttpClient())
            .addConverterFactory(SimpleXmlConverterFactory.create())
            .build();

  • 上記以外にXMLのデータ構造をmappingしたmodelクラスが必要です

辛い所

  • SimpleXMLのメソッド数が多すぎて (10000くらい)、65K limitに引っかかりやすくなる
  • Multidexにするとbuild時間伸びるし、proguardよく分からないので、、、とりあえずproguard調べるかという状況です
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6