Ksoap2 Request Wrong Formed-
This is my request:
privateclassrunTaskextendsAsyncTask<String, String, String> {
privateString response;
Stringstring = "your string parameter"StringSOAP_ACTION = "your soap action here";
String stringUrl = "http://your_url_here";
//if you experience a problem with url remove the '?wsdl' ending@OverrideprotectedStringdoInBackground(String... params) {
try {
//paste your request structure here as the String body.String body = "<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">"+"<soap:Body>"+
"<insertBeacons xmlns="http://tempuri.org/">"+"<MAC_ADDRESS>"+string+"</MAC_ADDRESS>"+
"<UUID>"+string+"</UUID>"+
"<MAJOR>"+string+"</MAJOR>"+
"<MINOR>"+string+"</MINOR>"+
"<MEASURED_POWER>"+string+"</MEASURED_POWER>"+
"<RSSI>"+string+"</RSSI>"+
"</insertBeacons>"+
"</soap:Body>"+
"</soap:Envelope>";
try {
URL url = newURL(stringUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDefaultUseCaches(false);
conn.setRequestProperty("Accept", "text/xml");
conn.setRequestProperty("SOAPAction", SOAP_ACTION);
//you can pass all your request parameters here usong .setRequestProperty() method//push the request to the server addressOutputStreamWriter wr = newOutputStreamWriter(conn.getOutputStream());
wr.write(body);
wr.flush();
//get the server responseBufferedReader reader = newBufferedReader(newInputStreamReader(conn.getInputStream()));
StringBuilder builder = newStringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line);
response = builder.toString();//this is the response, parse it in onPostExecute
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
/**
* @see AsyncTask#onPostExecute(Object)
*/@OverrideprotectedvoidonPostExecute(String result) {
try {
Toast.makeText(this,"Response "+ result,Toast.LENGTH_LONG).show();
//Go ahead and parse the response now
} catch (Exception e) {
e.printStackTrace();
}
}
Now in your onCreate just go ahead and execute this class using the following code
runTasktask=newrunTask();
task.execute();
You will get your response in your onPostExecute, format and parse it from here. The main advantages of using this libraryless way is that it is flexible, you can format the request in any way the webservice requires as compared to the libraries which you only use the provided request formats. This solution works seamlessly in my code, feel free to ask for any further clarification.
Post a Comment for "Ksoap2 Request Wrong Formed-"