【问题标题】:Retrofit2 + SimpleXML + SOAP request in KotlinKotlin 中的 Retrofit2 + SimpleXML + SOAP 请求
【发布时间】:2020-06-30 23:36:22
【问题描述】:

我正在尝试在使用 SOAP API 的 Android 应用中创建服务。发送值和返回值都是 XML。 以前我在另一个 API 中使用了 FormUrlEncoded + JSON 并工作,但使用 XML 我正在努力,因为 API 似乎没有被调用(HttpLoggingInterceptor 不显示,Mockup 服务也不显示任何请求)。

如果我更改为 FormUrlEncoded 我的服务,我可以看到请求已完成(我使用 HttpLoggingInterceptor 检查了它,但如果我删除 FormUrlEncoded 似乎永远不会调用服务。

我的 NetModule 在哪里创建retrofir、解析器等:

@Module
class NetModule {

    @Provides
    @Singleton
    fun provideRetrofit(): Retrofit {
        val client =
            OkHttpClient.Builder()
                .addInterceptor(HttpLoggingInterceptor().apply {
                    level = HttpLoggingInterceptor.Level.BODY
                })
                .build()

        val strategy = AnnotationStrategy()
        val serializer = Persister(strategy)

        return Retrofit.Builder()
            .baseUrl(BuildConfig.API_URL)
            .client(client)
            .addConverterFactory(SimpleXmlConverterFactory.create(serializer))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()

    }

    @Provides
    @Singleton
    fun provideFilesService(retrofit: Retrofit): FilesService =
        retrofit.create(FilesService::class.java)
}

定义接口的我的 FilesService.kt 是:

import com.liderasoluciones.enviotest.data.model.FileSendResponse
import com.liderasoluciones.enviotest.data.model.FileSendEnvelope
import io.reactivex.Flowable
import retrofit2.http.*

interface FilesService {
    @Headers(
        "Content-Type: application/soap+xml",
        "Accept-Charset: utf-8"
    )
    @POST("mockWSSMTSoap")
    fun sendFile(@Body body: FileSendEnvelope): Flowable<FileSendResponse>

}

我的 Body、Request 和 data 模型是 FileSendEnvelope.kt,并且是:

import org.simpleframework.xml.Element
import org.simpleframework.xml.Root
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.NamespaceList;

@Root(name = "GetInfoByState", strict = false)
@Namespace(reference = "http://www.webservicetest.net")

class FileSendData {

    @Element(name = "FileName", required = false)
    var name: String? = null

}

@Root(name = "soap12:Body", strict = false)
class FileSendBody {

    @Element(name = "GetInfoByFile", required = false)
    var fileSendData: FileSendData? = null

}

@Root(name = "soap12:Envelope")
@NamespaceList(
    Namespace(prefix = "xsi", reference = "http://www.w3.org/2001/XMLSchema-instance"),
    Namespace(prefix = "xsd", reference = "http://www.w3.org/2001/XMLSchema"),
    Namespace(prefix = "soap12", reference = "http://www.w3.org/2003/05/soap-envelope")
)
class FileSendEnvelope {

    @Element(name = "soap12:Body", required = false)
    var body: FileSendBody? = null
}

从 RemoteDataSource 类是我调用 api 的地方:

class RemoteFilesDataSource(private val filesService: FilesService,
                            private val genericResponseEntityMapper: GenericResponseEntityMapper):
    FilesDataSource {

    override fun sendFile(userToken: String): Flowable<GenericResponseEntity> {
        var petitionEnvelope = FileSendEnvelope()
        var petitionBody = FileSendBody()
        var petitionData = FileSendData()
        petitionData.name = "test.png"

        petitionBody.fileSendData = petitionData
        petitionEnvelope.body =

        return filesService.sendFile(petitionEnvelope)
            .map { it.result }
            .map { genericResponseEntityMapper.transform(it) }
    }


}

目前我不太关心发送的 XML 或解析响应,我只是“想检查”API 是否被调用。

我尝试关注此信息: https://github.com/asanchezyu/RetrofitSoapSample http://geekcalledk.blogspot.com/2014/08/use-simple-xml-with-retrofit-for-making.html

甚至是 java 示例,我正在使用 Kotlin,但没有运气。

感谢任何帮助。 提前致谢。

【问题讨论】:

    标签: soap kotlin rx-java retrofit2 simple-framework


    【解决方案1】:

    您是否尝试在标题中使用text/xml 作为您的Content-type? (也可以尝试不使用 Accept-Charset 标头)

    @Headers(
        "Content-type: text/xml"
    )
    

    【讨论】:

      猜你喜欢
      • 2021-10-05
      • 2016-06-25
      • 2018-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-07
      • 2021-12-19
      相关资源
      最近更新 更多