I'm not able to resolve this error:
org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://www.w3.org/2001/12/soap-envelope}Envelope (position:START_TAG #1:6 in java.io.InputStreamReader#43e524c8)
I have an Android app which accesses a .NET web service. It passes an int and receives an int as a response. I'm using ksoap2.
I added the permissions to the manifest file also and moreover I included all jar files. However, I'm stuck and I am unable to view which SOAP request I am sending.
My code is:
public class FirstAppUI extends Activity{
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL =
"http://nautilussoft.biz.whbus12.onlyfordemo.com/staging/litigation/demowebservice.asmx";
private static final String SOAP_ACTION ="http://tempuri.org/GetNumber";
private static final String METHOD_NAME = "GetNumber";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv= (TextView)findViewById(R.id.TextView01);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("maxbelow", "123");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet= true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
//Toast.makeText(getBaseContext(), "received object", Toast.LENGTH_SHORT).show();
androidHttpTransport.call(SOAP_ACTION, envelope);
Object resultsRequestSOAP = (SoapObject)envelope.getResponse();
//SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
//Toast.makeText(getBaseContext(), "received object", Toast.LENGTH_SHORT).show();
tv.setText("Received :" + resultsRequestSOAP.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e("APP", "MalformedURLException while sending\n" + e.getMessage());
tv.setText("Malformedexception"+e);
}
catch(Exception e1)
{
e1.printStackTrace();
tv.setText("exception"+e1);
}
}
}
Thanks in advance.
Give permission to your webserivce folder in IIS or through IIS manager.
please check the weburl you are using.. its case sensitive... even though its working fine in a web browser, you should use the exact name in your code...
Related
I created Java web services and trying to connect with android code.I am getting the service running but android app is showing error in soap object creating for namespace and method name.I did all the changes and everything is correct namespace,method name,URL all is correct.But I don't know what is wrong can anyone help me out...!
My android code is----->
showing error in
-"SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);"
public class RetailerActivity extends Activity {
private static final String SOAP_ACTION = "urn:training/searchCompanyInfo";
private static final String METHOD_NAME = "searchCompanyInfo";
private static final String NAMESPACE = "urn:training/";
private static final String URL = "http://localhost/attest/CompanyInfoService?wsdl";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println(NAMESPACE);
System.out.println(METHOD_NAME);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
try {
ht.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
SoapPrimitive s = response;
String str = s.toString();
String resultArr[] = str.split("&");//Result string will split & store in an array
TextView tv = new TextView(this);
for(int i = 0; i<resultArr.length;i++){
tv.append(resultArr[i]+"\n\n");
}
setContentView(tv);
} catch (Exception e) {
e.printStackTrace();
}
}
}
In Android, You cannot perform network operation on the main thread, You need to execute your soap request in a background thread. Please read up on AsyncTask.
I've got a problem with this webservice. I used to call diffrent ws in my project and it works fine.
private static final String URL2 = "http://46.248.168.51/webservice/soap/endpoint/apikey/cb7f1f308e82ca2be8541d5ba829dc1e/?wsdl";
private static final String METHOD_NAME2 = "getObjectList";
private static final String SOAP_ACTION2 = "http://46.248.168.51/webservice/soap/endpoint/apikey/cb7f1f308e82ca2be8541d5ba829dc1e/getObjectList";
private static final String NAMESPACE2 = "http://46.248.168.51/webservice/soap/endpoint/apikey/cb7f1f308e82ca2be8541d5ba829dc1e/";
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pp);
SoapObject request = new SoapObject(NAMESPACE2, METHOD_NAME2);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL2);
httpTransport.debug = true;
try {
httpTransport.call(SOAP_ACTION2, envelope);
SoapObject result = (SoapObject) envelope.getResponse();
} catch (Exception e) {
Log.d("e.getMessage()", e.getMessage());
e.getMessage();
e.printStackTrace();
}
httpTransport.call returns errors
11-10 15:31:43.421: D/e.getMessage()(2550): expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG #2:486 in java.io.InputStreamReader#4124e8d0)
Thanks for any help
Since your code works with several other web services. I think you have only changed the URL,SOAP Action and Method. So please check weather the response contain a popper SOAP message. To check that you can something like TCPMon in between the communication.
I have the following WCF webservice
I want to consume it in android client.My code so far is
public class SOAP_TestActivity extends Activity {
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://50.19.226.15/Office365Admin/MsOnline.svc";
private final String SOAP_ACTION = "http://tempuri.org/IMsOnline/ValidateUser";
private final String METHOD_NAME = "ValidateUser";
private final String user_id= "khurram#office365trial.com.au";
private final String password= "friday#123";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button signin = (Button) findViewById(R.id.regsubmitbtn);
signin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
boolean auth=doLogin();
System.out.println(auth);
Toast.makeText(getApplicationContext(), String.valueOf(auth), Toast.LENGTH_SHORT).show();
}
});
}
private boolean doLogin() {
boolean result=false;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("userid", user_id);
request.addProperty("password",password);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
Log.d("myApp", request.toString());
System.out.println(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive soapPrimitiveResults = (SoapPrimitive)envelope.getResponse();
Log.d("myApp", soapPrimitiveResults.toString());
System.out.println("myApp" +soapPrimitiveResults);
if(soapPrimitiveResults.toString().equalsIgnoreCase("true"))
{
result = true;
}
}catch(SocketException ex)
{
Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage());
ex.printStackTrace();
}
catch (Exception e) {
Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
e.printStackTrace();
}
return result;
}
}
This code works without any exception but I am not able to login into my application as the method ValidateUser always return false.This same webservice has been consumed in Windows Phone and is working without any problem.Also the username and password are 100% correct.
Looks like there is some problem in the NAMESPACE and SOAP_ACTION. I have gone through some tutorials and they say that the SOAP_ACTION is made by concatenating NAMESPACE with METHOD_NAME.So if I follow the tutorials my SOAP_ACTION should be
"http://tempuri.org/ValidateUser"
but the SOAP_ACTION given in the WSDL linkfor the method "ValidateUser" is
"http://tempuri.org/IMsOnline/ValidateUser".
I am new to WCF Webservices and SOAP.Can anybody please tell me how to implement the methods given in WSDL Document(link is at the top).
Looks like missing something important but don't know what.
If the same web service works fine with windows phone, then you have a NameSpace problem,
and the web service method is not getting the parameters from the SOAP message. And since your ValidateUser gets no parameters, it just returns null/false.
I had the same problem. I solved it by explicitly defining a namespace hence getting rid of the default namespace WCF sets, tempuri.org
Here is my solution for reference:
passing objects to wcf soap service from android using ksoap2; it sends and receives 0
Try this:
...
Object response = envelope.getResponse();
String value = response.toString();
...
I have created a web service to test passing parameters via ksoap2. I thought it seemed like a pretty simple process, but I'm apparently missing something simple. The web service does nothing more than return the integer that is passed to it. When I execute the code below it returns the number 0 instead of 1.
private static String SOAP_ACTION = "http://tempuri.org/TestParams";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "TestParams";
private static String URL = "http://services.lockrem.com/WebService.asmx?WSDL";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("RoundId", 1);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
} catch (Exception e) {
e.printStackTrace();
}
SoapObject result = (SoapObject)envelope.bodyIn;
Note: I have tested this using a .net web page and it returns the number 1 as expected. The web service is not the issue here.
Here is the web service.
[WebMethod]
public int TestParams(int RoundId)
{
return RoundId;
}
Thank you for any help.
Disclaimer: Whenever I try to talk to a .NET webservice using kSoap2, I usually end up doing a lot of fiddling. And I mean that literally: I setup a windows machine with Fiddler2 and use that as a proxy for both the failing request and a working reference request, so I can actually see what's going wrong. I strongly advise doing something similar as soon as you try to do something non-trivial.
That said, in your case, it might be enough to do
envelope.dotNet = true;
Try removing the WSDL at the end
private static String URL = "http://services.lockrem.com/WebService.asmx";
In addition try using this to get the response:
Object response = envelope.getResponse();
instead of SoapObject result = (SoapObject)envelope.bodyIn;
EDITED
This works:
public class MainActivity extends Activity {
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "TestParams";
private static String URL = "http://services.lockrem.com/WebService.asmx";
private TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.tv);
HttpTransportSE transport = new HttpTransportSE(URL);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("RoundId", 1);
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
try {
transport.call(NAMESPACE + METHOD_NAME, envelope);
Object response = envelope.getResponse();
tv.setText(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Hope it helps.
Im getting single data from web service using SOAP in android and displayed well.But when i need to retrieve multiple set of values from web services..How could i do that?For ex.from a webservice.,im getting 10 set of student records with fields stu.Name,stuRegno.,stuAge.After getting response of this.,i need to parse it to stored into local db with corresponding field names.How could i do that?
My code:
public class Main extends Activity {
private static final String METHOD_NAME = "TopGoalScorers";
private static final String SOAP_ACTION = "http://footballpool.dataaccess.eu/data/TopGoalScorers";
private static final String NAMESPACE = "http://footballpool.dataaccess.eu";
private static final String URL = "http://footballpool.dataaccess.eu/data/info.wso?WSDL";
//Called when the activity is first created.
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
//Buttons deaktiviert alles automatisch
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("iTopN", 3);// username von Preferences abgeholt
//request.addProperty("intB", 4);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
Toast.makeText(getApplicationContext(), request.toString(), Toast.LENGTH_LONG).show();
envelope.setOutputSoapObject(request);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport (URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
//Parse Response
SoapObject resultsRequestSOAP = (SoapObject) envelope.getResponse();
String xy = resultsRequestSOAP.getProperty(0).toString();
((TextView)findViewById(R.id.lblStatus)).setText(xy);
} catch(Exception E) {
((TextView)findViewById(R.id.lblStatus)).setText("ERROR:" + E.getClass().getName() + ": " + E.getMessage());
}
}
}
I don't understand what is your problem. You need to convert resultsRequestSOAP to appropriate data format(for example String) and parse it(for example through SAX) and in your parser collect all data in a container(for example List).