I'm using SOAP service to get ticket. I'm sending user and pass, and I'm getting xml in String. For this I'm using ksoap2.
#Override
protected String doInBackground(String... params) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty(USER, params[0]);
request.addProperty(PASS, params[1]);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.bodyOut = request;
soapEnvelope.setOutputSoapObject(request);
HttpTransportSE HttpTransport = new HttpTransportSE(URL);
try {
HttpTransport.call(SOAP_ACTION, soapEnvelope);
return soapEnvelope.getResponse().toString();
} catch (IOException | XmlPullParserException e) {
e.printStackTrace();
return null;
}
}
#Override
protected void onPostExecute(String XML) {
super.onPostExecute(XML);
if (result != null) {
// Here I need to get data from XML
}
}
My XML String looks like this:
<?xml version="1.0" encoding="UTF-8" ?>
<resp err="0">
<ticket>1234567989</ticket>
</resp>
So I need to get the error number, and the ticket number.
Don't convert your response to String and return it, instead get properties you need with getProperty() method and convert them to String. Here's how i was doing it:
String ticket;
public void getSoap() {
SoapObject request = new SoapObject(NAMESPACE, METHODNAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.implicitTypes = false;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransportSe = new HttpTransportSE(URL);
httpTransportSe.debug = true;
SoapObject response = null;
try {
httpTransportSe.call(SOAPACTION, envelope);
response = (SoapObject) envelope.getResponse();
ticket = response.getProperty("ticket").toString();
} catch (Exception e) {
e.printStackTrace();
}
}
Getting "err" element from attribute might require a bit more research but you can try this where you get ticket:
String err = response.getAttribute("err").toString;
Hope this helps and happy coding !
Use JDom to parse XML:
String xmlString = //get xml string;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try
{
builder = factory.newDocumentBuilder();
Document document = builder.parse( new InputSource( new StringReader( xmlString ) ) );
} catch (Exception e) {
e.printStackTrace();
}
and next get values via XPath from document http://www.w3schools.com/xpath/xpath_examples.asp
Related
I'm trying to connect with a web service.
The sample request for this web service contains this array:
<Details>
<invoiceDetails>
<ItemID>int</ItemID>
<Price>decimal</Price>
<Quantity>decimal</Quantity>
<UOM>int</UOM>
</invoiceDetails>
<invoiceDetails>
<ItemID>int</ItemID>
<Price>decimal</Price>
<Quantity>decimal</Quantity>
<UOM>int</UOM>
</invoiceDetails>
</Details>
I have managed to make a call with SOAP before, but not with arrays,
so how would I add this to my request?
Try this code,
public String getservicecallmethod(String nameSpace, String methodName,
String soapAction, String Url, List<Info> mInfo) {
String mResponse = "";
SoapObject request = new SoapObject(nameSpace, methodName);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
if (mPropertyInfo != null) {
for (Info propertyInfo : mInfo) {
request.addProperty(propertyInfo);
}
}
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(Url);
ht.debug = true;
try {
ht.call(soapAction, envelope);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
try {
mResponse = envelope.getResponse().toString();
} catch (Exception e) {
e.printStackTrace();
}
return mResponse;
}
I get the string of array on responseJSON like this
Result: responseJSON = ["Product1","Product2","Product1","Product2"]
try {
// Invole web service
androidHttpTransport.call(SOAP_ACTION+methName, envelope);
// Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
// Assign it to static variable
responseJSON = response.toString();
} catch (Exception e) {
e.printStackTrace();
}
how can to get each element of the array so that I can use it to display on my ListView?
Thanks
public void invokeJSONWS(String country, String methName) {
// Create request
SoapObject request = new SoapObject(NAMESPACE, methName);
/*
// Property which holds input parameters
PropertyInfo paramPI = new PropertyInfo();
// Set Name
paramPI.setName("country");
// Set Value
paramPI.setValue(country);
// Set dataType
paramPI.setType(String.class);
// Add the property to request object
request.addProperty(paramPI);
*/
// Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
// Set output SOAP object
envelope.setOutputSoapObject(request);
// Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
// Invole web service
androidHttpTransport.call(SOAP_ACTION+methName, envelope);
// Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
// Assign it to static variable
responseJSON = response.toString();
JSONArray responseArr = new JSONArray(responseJSON);
for(int i=0;i < responseArr.length();i++)
{
String temp=responseArr.getString(i);
myProduct.add(new Product(responseJSON,2,R.drawable.user_awake,responseJSON));
}
} catch (Exception e) {
e.printStackTrace();
}
}
this is the method that i used it it correct that i also include the adding an item in the list view??
please help
thanks
pass this string to JSONAarry constructor like this
ArrayList<String> mParsedList = new ArrayList<String>();
JSONAarry responseArr=new JSONArray(responseJSON);
for(int i=0;i<responseArr.length;i++)
{
String temp=responseArr.getString(i);// get one by one element
myProduct.add(new Product(temp,2,R.drawable.user_awake,responseJSON));
}
I'm using HttpsTransportSE for calling webservices. Repeatedly calling web service which needs for reply 1-2s. Then there is a call which requires 30-45s or return timeout(set on 45s). This problem occurs only on 3G signal on WIFI works perfectly.
public SoapObject CallWS(Object object, ArrayList<Tuples> params,
String SOAP_ACTION, String METHOD_NAME, String NAMESPACE, String serviceURL) {
SoapObject response = null;
//try {
// Generate soap object with WS-namespace and WS-method name
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// Parameters for WS method
for (int i = 0; i < params.size(); i++) {
request.addProperty(params.get(i).getLeft().toString(), params
.get(i).getRight().toString());
}
// Create soap envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.headerOut = new Element[1];
envelope.headerOut[0] = buildAuthHeader(NAMESPACE,"trak8","g3s0");
// Add mapping for complex parameter
envelope.addMapping(NAMESPACE, "Object", new Object().getClass());
TrustManagerManipulator.allowAllSSL();
Log.i("NAMESPACE",NAMESPACE);
Log.i("METHOD_NAME",METHOD_NAME);
Log.i("URL2",serviceURL);
//HttpTransportSE aht = new HttpTransportSE(serviceURL, 10000);
String[] serviceURLSplit = serviceURL.split("//");
String[] serviceURLSplitMethod = serviceURLSplit[1].split("/");
HttpsTransportSE aht = new HttpsTransportSE(serviceURLSplitMethod[0], 443,"/"+serviceURLSplitMethod[1], 45000);
List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
headerList.add(new HeaderProperty("Connection", "keep-alive"));
try {
Log.i("StatusCall", "calling ws"+METHOD_NAME); // For debbug only
aht.call(SOAP_ACTION, envelope,headerList);
response = (SoapObject) envelope.getResponse();
Log.i("StatusCall", "response successfully recived"); // For debbug
// only
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
`
I have a webservice with this location:
<wsdlsoap:address location="http://app.example.com:8080/WSTest/services/Hello"/>
And I want to get access to this webservice over my Android app. I do this:
private class ConnectTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
private final String NAMESPACE = "http://ws.audiomobil.com";
private final String URL = "http://app.example.com:8080/WSTest/services/Hello?wsdl";
private final String SOAP_ACTION = "http://ws.audiomobil.com/hello";
private final String METHOD_NAME = "hello";
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
String firstName = "Android";
String lastName = "Program";
/*
// Pass value for fname variable of the web service
PropertyInfo fnameProp = new PropertyInfo();
fnameProp.setName("fname"); // Define the variable name in the web service method
fnameProp.setValue(firstName); // Define value for fname variable
fnameProp.setType(String.class); // Define the type of thevariable
request.addProperty(fnameProp); // Pass properties to thevariable
// Pass value for lname variable of the web service
PropertyInfo lnameProp = new PropertyInfo();
lnameProp.setName("lname");
lnameProp.setValue(lastName);
lnameProp.setType(String.class);
request.addProperty(lnameProp);
*/
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,60000);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
String s = response.toString();
System.out.println("WS: " + s);
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
return null;
}
}
++++++EDIT
My Webservice class is this:
public class Hello {
public String hello(String fname, String lname){
return "Hello";
}
}
+++++++
When I run this on my AVD Emulator Android 4.0.3 then its working fine. Its get access to webservice and I get the response.
But when I run it on my device Version 4.0.3, then it's not working. It stops at this point: androidHttpTransport.call(SOAP_ACTION, envelope);
And it throws a XMLPullParserException. Heres the logcat:
01-23 11:24:36.959: I/System.out(8172): org.xmlpull.v1.XmlPullParserException:
unterminated entity ref (position:TEXT #1:86 in java.io.InputStreamReader#4188c258)
I have really no idea how this can happen. Can anyone help me pls?
I think you are missing the xml version tag. Try to set xml version tag before calling
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
androidHttpTransport.call(SOAP_ACTION, envelope);
Sample Code:
public static String webServiceForScriptTimeLimit() {
String scriptTimeLimitResponse = null;
try {
SoapObject request = new SoapObject(NAMESPACE,
SCRIPT_TIMELIMIT_METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.implicitTypes = true;
envelope.enc = SoapSerializationEnvelope.ENC2003;
envelope.xsd = SoapEnvelope.XSD;
envelope.xsi = SoapEnvelope.XSI;
envelope.setOutputSoapObject(request);
envelope.setAddAdornments(false);
HttpTransportSE ht = new HttpTransportSE(SOAP_ADDRESS);
ht.debug = true;
ht.call(SOAP_ACTION_TIME_LIMIT, envelope);
final SoapPrimitive response = (SoapPrimitive) envelope
.getResponse();
scriptTimeLimitResponse = response.toString();
}
catch (Exception ex) {
FileLog.logInfo(
"Exception ---> WS_ERROR ---> WebServiceUtility: webServiceForScriptTimeLimit() - "
+ ex.toString(), 0);
}
return scriptTimeLimitResponse;
}
I am consuming a .net web service using ksoap2 but getting the following Exception:
org.xmlpull.v1.XmlPullParserException: unexpected type (position:END_DOCUMENT null#1:1 in java.io.InputStreamReader#412caa08)`
My request dump is the same as I needed but I'm not getting response dump and it has come as null. I am trying, but getting the same error.Please some one help me. My code is given below-
public class Login extends Activity {
private final String NAMESPACE = "http://xyz.public/imageupload/1.0/";
private final String URL = "http://public.service.xyz.com/ImageUpload.svc";
private final String SOAP_ACTION = "http://xyz.public/imageupload/1.0/Services/Authentication";
private final String METHOD_NAME = "Authentication";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
SoapSerializationEnvelope envelope = null;
SoapObject request = null;
HttpTransportSE httpTransportSE = null;
try {
request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("userId", "admin");
request.addProperty("password", "password");
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.dotNet = true;
envelope.encodingStyle = SoapSerializationEnvelope.ENC;
/**
* below code for custom header
*/
Element actionElement = new Element().createElement(null,
"d:Action");
actionElement.setAttribute("", "s:mustUnderstand", "1");
actionElement.addChild(Node.TEXT, SOAP_ACTION);
Element toElement = new Element().createElement(null, "d:To");
toElement.setAttribute("", "s:mustUnderstand", "1");
toElement.addChild(Node.TEXT, URL);
Element[] header = { actionElement,toElement };
envelope.headerOut = header;
envelope.env = "http://schemas.xmlsoap.org/soap/envelope/";
/**
* Custom header part end
*/
envelope.setOutputSoapObject(request);
httpTransportSE = new HttpTransportSE(URL);
httpTransportSE.debug = true;
httpTransportSE.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject) envelope.getResponse();
// To get the data.
String textResult = result.toString();
Log.i("textResult", textResult);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
Log.e("Exception:", e.toString());
}
finally {
Log.i(getClass().getSimpleName(), "requestDump : "+ httpTransportSE.requestDump);
Log.i(getClass().getSimpleName(), "responseDump : "+ httpTransportSE.responseDump);
}
}
}