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();
...
Related
I'm writing an Android App that communicates with an web service using KSOAP. The connection between web service and Android app is working as I can call the webservice and get a return value (hello). But if I try to give a name from the App to the web service via .addProperty the webservice returns a null object.
Here is my code:
MainActivity:
private final String NAMESPACE_Local = "http://test.com/";
private final String URL_Local = "http://168.185.226.21:7001/myTest/myTestWebServiceService";
private final String SOAP_ACTION_Local = "Hello_Action_Extend";
private final String METHOD_NAME_Local = "hello_extend";
public void LocalServer(View view)
{
TextView text = (TextView) findViewById(R.id.update_text);
SoapObject request = new SoapObject(NAMESPACE_Local, METHOD_NAME_Local);
request.addProperty("name", "Christian");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_Local);
try {
androidHttpTransport.call(SOAP_ACTION_Local, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
Log.i("myApp", response.toString());
text.setText(response.toString());
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this,"Device or service offline",Toast.LENGTH_LONG).show();
}
}
WebServer:
package com.test;
import javax.jws.*;
#WebService
public class myTestWebService {
#WebMethod(action="Hello_Action") //that method works
public String hello() {
return "hello";
}
#WebMethod(action="Hello_Action_Extend")
public String hello_extend(String name) //that works also, but it is giving back "hello null"
{
return "hello "+name;
}
}
I hope you can help me!
Try replacing:
request.addProperty("name", "Christian");
for:
request.addProperty("name",ElementType.STRING_CLASS, "Christian");
and the response for:
SoapObject reponse=(SoapObject)envelope.getResponse();
response.getProperty("name");
API SoapObject
Not Able To Connect To Webservice With Android
it not acess the webservice code
i have tried to make thread and then run my code there
i test the webservice link from my phone , its reachable
here are the code both .net and android :-
Webservice Code .net :strong text
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
<WebService(Namespace:="http://tempuri.org/")> _
Public Class Service
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function HelloWorld() As Integer
Return 0
End Function
End Class
Here Is the Code
public class MainActivity extends Activity implements OnClickListener {
ImageButton btn1;
ImageButton Save;
private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
private static final String MethodName = "HelloWorld";
private static final String NameSpace = "http://tempuri.org/";
private static final String URL = "http://172.20.10.2/WebSite9/Service.asmx";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Thread webser = new Thread() {
public void run() {
try {
// String project = titles.get(position - 1);
// CallWebService();
SoapObject request = new SoapObject(NameSpace, MethodName);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
// request.addProperty("TextToDisplay", "This is coming from android");
envelope.setOutputSoapObject(request);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject)envelope.getResponse();
// Toast.makeText(MainActivity.this,result.toString(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
};
webser.start();
please help!
Your Code Seems to be ok
please make sure that : -
1- use ksoap2-android-2.5.2.jar
2- go to buildpath>add external jars and select it
3- copy the ksoap2-android-2.5.2.jar and paste it under libs file
restart the application and try ?
put this code:
SoapObject result = (SoapObject)envelope.bodyIn;
instead of this
SoapObject result = (SoapObject)envelope.getResponse();
and it will work with you as expected.
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 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.
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...