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

时间:2016-11-14 03:51:24

标签: java android xml-rpc

我想创建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");

        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());
    }

我收到了错误&#34; onServerError APPLICATION_NAME必须记录不存在。&#34;
我使用的是XML文件库https://github.com/gturri/aXMLRPC 你推荐哪个图书馆?
我可以使用Retrofit来发出xml-rpc请求吗? 谢谢你的帮助

1 个答案:

答案 0 :(得分:2)

你只需使用这样的改造:

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

您必须添加SimpleXmlConverter:

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