how to parse XML response using retrofit 2.0?

后端 未结 1 503
攒了一身酷
攒了一身酷 2021-01-24 19:17

Hi am new to android programming i have gone through some links for how to parse the XML response using retrofit and i tried for below XML as shown.

Is there any necessa

相关标签:
1条回答
  • 2021-01-24 19:32

    For example you have an XML string:

    <Response>
        <Field1>string</Field1>
        <Field2>string</Field2>
        <Field3>string</Field3>
        <Field4>string</Field4>
    </Response>
    

    First, add to your gradle file:

    implementation "com.squareup.retrofit2:converter-simplexml:2.6.1" 
    

    Next, add to your retrofit builder:

    .addConverterFactory(SimpleXmlConverterFactory.create())
    

    Note: SimpleXmlConverterFactory is deprecated now, but I've not found a better solution

    And your model class:

    @Root(name = "Response")
    data class Response @JvmOverloads constructor(
    @field:Element(name = "Field1")
    var bucket: String = "",
    @field:Element(name = "Field2")
    var key: String = "",
    @field:Element(name = "Field3")
    var etag: String = "",
    @field:Element(name = "Field4")
    var location: String = ""
    )
    

    Note: properties need to be predefined

    Your ApiService file will be the same as you are parsing json

    0 讨论(0)
提交回复
热议问题