简体   繁体   English

Android:如何使用自定义http标头和内容类型创建xml-rpc请求

[英]Android: how to create post xml-rpc request with custom http header & content type

I want to creat xml-rpc POST request, and pass 2 parameters "application_name" & "key", and change content type to "application/x-www-form-urlencoded" like my code below. 我想创建xml-rpc POST请求,并传递2个参数“ application_name”和“ key”,然后将内容类型更改为“ application / x-www-form-urlencoded”,如下所示。

try {
        XMLRPCClient oneTimeKeyClient = new XMLRPCClient(new URL(URL_REQUEST_SAMPLE), XMLRPCClient.FLAGS_DEFAULT_TYPE_STRING);
        oneTimeKeyClient.setCustomHttpHeader("X-HTTP-METHOD-OVERRIDE", "POST");

// oneTimeKeyClient.setCustomHttpHeader("Content-Type", "application/x-www-form-urlencoded"); // oneTimeKeyClient.setCustomHttpHeader(“ Content-Type”,“ application / x-www-form-urlencoded”);

        HashMap<String, String> oneTimeKeyParam = new HashMap<>();
        oneTimeKeyParam.put("application_name", "hello_app");
        oneTimeKeyParam.put("key", "bb5eb953d3b41dcf59f4669d98f8e14782ed83133be772956b");
        Vector<Object> params = new Vector<Object>();
        params.add(oneTimeKeyParam);

        oneTimeKeyClient.callAsync(new XMLRPCCallback() {
            @Override
            public void onResponse(long id, Object response) {
                try {
                    result = ((Map) response).get(NAME_ONE_TIME_KEY).toString();
                } catch (Exception e) {
                    Timber.e("onParseError %s", e.getMessage());
                    DialogUtil.showLoginErrorDialog(getSupportFragmentManager());
                }
            }

            @Override
            public void onError(long id, XMLRPCException error) {
                Timber.e("onError %s", error.getMessage());
                DialogUtil.showLoginErrorDialog(getSupportFragmentManager());
            }

            @Override
            public void onServerError(long id, XMLRPCServerException error) {
                Timber.e("onServerError %s", error.getMessage());
                DialogUtil.showLoginErrorDialog(getSupportFragmentManager());
            }
        }, "", params);
    } catch (Exception e) {
        Timber.e("onError %s", e.getMessage());
        DialogUtil.showLoginErrorDialog(getSupportFragmentManager());
    }

I got error " onServerError APPLICATION_NAME must record not exists." 我收到错误消息“ onServerError APPLICATION_NAME必须记录不存在。”
I using aXMLRPC library https://github.com/gturri/aXMLRPC . 我使用XMLRPC库https://github.com/gturri/aXMLRPC
Which library do you recommend ? 您推荐哪个图书馆?
Can I use Retrofit to make xml-rpc request ? 我可以使用Retrofit发出xml-rpc请求吗?
Thanks for any help 谢谢你的帮助

You just use retrofit like this: 您只需要像这样使用改造:

@FormUrlEncoded
@POST("onetime_key")
Observable<OneTimeKeyRes> requestOneTimeKey(@Field("application_name") String applicationName, @Field("key") String key);

You must add SimpleXmlConverter: 您必须添加SimpleXmlConverter:

Retrofit retrofit = new Retrofit.Builder()
            .client(builder.build())
            .baseUrl(YOUR_BASE_URL)
            .addConverterFactory(SimpleXmlConverterFactory.create())
            .build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM