I have the following Android app code below. I'm trying to connect to a web service via HTTP. The web service uses apache axis. However I'm running into the error "Error reading XMLStreamReader" in the response. I'm really stuck and not sure what I can do. Could it be that there are different versions of HTTP client and SOAP being used on the server and client side?? Any help on this would be greatly appreciated. The web service is very simple: the sayHello method displays the argument given in arg0=some_string
public class MainActivity extends Activity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
BufferedReader in = null;
try {
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(
"http://10.0.0.63:8080/archibus/cxf/HelloWorld/sayHello");
request.addHeader("Content-Type", "text/xml");
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("arg0", "testing"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String page = sb.toString();
// Log.i(tag, page);
System.out.println(page);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Your WebService request is not constructed properly. You are actually creating a form request and not an actual SOAP request.
a SOAP request is an XML Document which has an envelope and a body see example here SOAP Message Example on Wikipedia.
What you are actually doing here is a Standard HTTP call that emulates a submit form and not a SOAP call.
You have two solutions here :
1- You can either emulate the behaviour of a SOAP client by manually creating the XML document and submitting it. Besides setting the proper XML document as request body don't forget to set the proper headers : SOAPAction, Content-Type and Content-Length
RequestEntity requestEntity = new StringRequestEntity("<?xml version=\"1.0\"?><soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"><soap:Header></soap:Header><soap:Body><m:GetStockPrice xmlns:m=\"http://www.example.org/stock\"><m:StockName>IBM</m:StockName></m:GetStockPrice></soap:Body></soap:Envelope>");
post.setRequestEntity(requestEntity );
Also do not forget to change the namespace (m) above with the proper namespace your webservice is using. and the operation name (GetStockPrice ) with the operation you are trying to invoke. Also don't forget the parameter names and types.
2- You can use Apache Axis to generate a client and use that client with your application
See this thread for more information and a recommended client How to call a SOAP web service on Android
Shoukry K is right. You have done a HTTP POST request. This is not a soap webservice. If you want to know how to call a webservice follow the link. http://www.youtube.com/watch?v=v9EowBVgwSo. Make sure you download the latest ksoap jar file.
Sample code using K-SOAP for Android.
private void sendSOAPmsg(DamageAssessmentFormPojo pojo) throws IOException, XmlPullParserException, SoapFault {
SoapObject request = new SoapObject(WEBSERVICE.NAMESPACE, WEBSERVICE.METHOD_NAME_SUBMIT_REPORT);
request.addProperty("xmlBytes", Util.getSoapBase64String(pojo));
request.addProperty("fileName", IO.DefaultReportName);
request.addProperty("deviceId", AppConstants.IMEI != null ? AppConstants.IMEI : Util.getIMEI(this));
SoapPrimitive response = sendSOAPEnvelope(request, WEBSERVICE.SOAP_ACTION_SUBMIT_REPORT);
if (response.toString().equalsIgnoreCase("true")) {
Logger.logInfo("REPORT SENT SUCCESSFULLY", "WEB-SERVICE");
pojo.setReportSent(true);
IO.writeObject(pojo.getReportsFolderPath() + IO.DefaultReportName, pojo);
} else {
Logger.logInfo("REPORT SENT FAILED", "WEB-SERVICE - RESONSE " + response.toString());
}
}
private SoapPrimitive sendSOAPEnvelope(SoapObject request, String soapAction) throws IOException, XmlPullParserException, SoapFault {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(WEBSERVICE.URL);
androidHttpTransport.call(soapAction, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
return response;
}
private boolean sendSOAPimage(String strImg, File pFile) throws IOException, XmlPullParserException {
boolean result = false;
if (strImg != null) {
SoapObject request = new SoapObject(WEBSERVICE.NAMESPACE, WEBSERVICE.METHOD_NAME_SUBMIT_IMAGE);
request.addProperty("imageBytes", strImg);
request.addProperty("fileName", pFile.getName());
request.addProperty("deviceId", AppConstants.IMEI != null ? AppConstants.IMEI : Util.getIMEI(this));
SoapPrimitive response = sendSOAPEnvelope(request, WEBSERVICE.SOAP_ACTION_SUBMIT_MAGE);
if (response.toString().equalsIgnoreCase("true")) {
result = true;
Logger.logInfo("IMAGE SENT SUCCESSFULLY", "WEB-SERVICE");
} else {
Logger.logInfo("IMAGE SENT FAILED", "WEB-SERVICE - RESONSE " + response.toString());
}
}
return result;
}
//--------Util Helper Method
public static String getSoapBase64String(DamageAssessmentFormPojo pojo) {
XmlSerializer xmlSerializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
try {
xmlSerializer.setOutput(writer);
xmlSerializer.startDocument("UTF-8", true);
xmlSerializer.startTag("", XMLTags.TAG_ROD);
xmlSerializer.startTag("", XMLTags.TAG_ORDER);
xmlSerializer.startTag("", XMLTags.TAG_SEVERITY);
xmlSerializer.text(pojo.getCheckedSeverity_Complexity());
xmlSerializer.endTag("", XMLTags.TAG_SEVERITY);
xmlSerializer.startTag("", XMLTags.TAG_DAMAGE_TYPE);
StringBuilder builder = new StringBuilder();
for (String str : pojo.getCheckedDamageTypes()) {
builder.append(str + " , ");
}
xmlSerializer.text(builder.toString());
xmlSerializer.endTag("", XMLTags.TAG_DAMAGE_TYPE);
xmlSerializer.startTag("", XMLTags.TAG_ENV_IMPACT);
xmlSerializer.text(pojo.getCheckedEnvImpact());
xmlSerializer.endTag("", XMLTags.TAG_ENV_IMPACT);
xmlSerializer.startTag("", XMLTags.TAG_ENV_COMMENT);
xmlSerializer.text(pojo.getEnvImpactComments());
xmlSerializer.endTag("", XMLTags.TAG_ENV_COMMENT);
xmlSerializer.startTag("", XMLTags.TAG_TRAVEL_CONDITION);
xmlSerializer.text(pojo.getCheckedTravelCond());
xmlSerializer.endTag("", XMLTags.TAG_TRAVEL_CONDITION);
xmlSerializer.startTag("", XMLTags.TAG_TRAVEL_COMMENT);
xmlSerializer.text(pojo.getTravCondComments());
xmlSerializer.endTag("", XMLTags.TAG_TRAVEL_COMMENT);
xmlSerializer.startTag("", XMLTags.TAG_ORDER_DATE);
xmlSerializer.text(pojo.getDateTime());
xmlSerializer.endTag("", XMLTags.TAG_ORDER_DATE);
xmlSerializer.endTag("", "Order");
xmlSerializer.endTag("", "ROD");
xmlSerializer.endDocument();
} catch (IllegalArgumentException e) {
Logger.logException(e);
} catch (IllegalStateException e) {
Logger.logException(e);
} catch (IOException e) {
Logger.logException(e);
}
return Base64.encode(writer.toString().getBytes());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With