【问题标题】:Android: how to create post xml-rpc request with custom http header & content typeAndroid:如何使用自定义 http 标头和内容类型创建 post xml-rpc 请求
【发布时间】:2017-03-27 16:05:31
【问题描述】:

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

我收到错误“onServerError APPLICATION_NAME 必须记录不存在。”
我使用 XMLRPC 库 https://github.com/gturri/aXMLRPC .
你推荐哪个图书馆?
我可以使用 Retrofit 来发出 xml-rpc 请求吗?
感谢您的帮助

【问题讨论】:

    标签: java android xml-rpc


    【解决方案1】:

    你只需像这样使用改造:

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

    【讨论】:

      猜你喜欢
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2016-01-01
      • 2012-01-20
      相关资源
      最近更新 更多