Cuong Nguyen Cuong Nguyen - 23 days ago 13
Android Question

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.


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


I got error " onServerError APPLICATION_NAME must record not exists."

I using aXMLRPC library https://github.com/gturri/aXMLRPC .

Which library do you recommend ?

Can I use Retrofit to make xml-rpc request ?

Thanks for any help

Answer

You just use retrofit like this:

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