try {
// prepare POST body
String body = methodCall(method, params);
// set POST body
HttpEntity entity = new StringEntity(body);
postMethod.setEntity(entity);
// This code slightly tweaked from the code by erickok in issue #6
// Force preemptive authentication
// This makes sure there is an 'Authentication: ' header being send before trying and failing and retrying
// by the basic authentication mechanism of DefaultHttpClient
if(this.httpPreAuth == true) {
String auth = this.username + ":" + this.password;
postMethod.addHeader("Authorization", "Basic " + Base64Coder.encode(auth.getBytes()).toString());
}
//Log.d(Tag.LOG, "ros HTTP POST");
// execute HTTP POST request
HttpResponse response = client.execute(postMethod);
//Log.d(Tag.LOG, "ros HTTP POSTed");
// check status code
int statusCode = response.getStatusLine().getStatusCode();
//Log.d(Tag.LOG, "ros status code:" + statusCode);
if (statusCode != HttpStatus.SC_OK) {
throw new XMLRPCException("HTTP status code: " + statusCode + " != " + HttpStatus.SC_OK);
}
// parse response stuff
//
// setup pull parser
XmlPullParser pullParser = XmlPullParserFactory.newInstance().newPullParser();
entity = response.getEntity();
Reader reader = new InputStreamReader(new BufferedInputStream(entity.getContent()));
// for testing purposes only
// reader = new StringReader("<?xml version='1.0'?><methodResponse><params><param><value>\n\n\n</value></param></params></methodResponse>");
pullParser.setInput(reader);
// lets start pulling...
pullParser.nextTag();
pullParser.require(XmlPullParser.START_TAG, null, Tag.METHOD_RESPONSE);
pullParser.nextTag(); // either Tag.PARAMS (<params>) or Tag.FAULT (<fault>)
String tag = pullParser.getName();
if (tag.equals(Tag.PARAMS)) {
// normal response
pullParser.nextTag(); // Tag.PARAM (<param>)
pullParser.require(XmlPullParser.START_TAG, null, Tag.PARAM);
pullParser.nextTag(); // Tag.VALUE (<value>)
// no parser.require() here since its called in XMLRPCSerializer.deserialize() below
// deserialize result
Object obj = iXMLRPCSerializer.deserialize(pullParser);
entity.consumeContent();
return obj;
} else
if (tag.equals(Tag.FAULT)) {
// fault response
pullParser.nextTag(); // Tag.VALUE (<value>)
// no parser.require() here since its called in XMLRPCSerializer.deserialize() below
// deserialize fault result
Map<String, Object> map = (Map<String, Object>) iXMLRPCSerializer.deserialize(pullParser);
String faultString = (String) map.get(Tag.FAULT_STRING);
int faultCode = (Integer) map.get(Tag.FAULT_CODE);
entity.consumeContent();
throw new XMLRPCFault(faultString, faultCode);
} else {
entity.consumeContent();
throw new XMLRPCException("Bad tag <" + tag + "> in XMLRPC response - neither <params> nor <fault>");
}
} catch (XMLRPCException e) {
// catch & propagate XMLRPCException/XMLRPCFault
throw e;