Hello I am trying to call a web service from android. The code is working fine with no errors but I get no output. I am new to android. Please help me out.
The whole tutorial is here ....
There are three buttons clear button do well but both convert to celsius and convert to fahrenheit don't work.
Actually there is the statement in both of them in the try block
SoapObject result = (SoapObject)envelope.bodyIn;
I guess at this line the application get stuck because I put the builder message after each statement it prompted but after this statement it did not .
Please tell me what is the problem, I am really worried..
try to use this class to call a web service:
public class WSRequest {
public HttpTransportSE androidHttpTransport;
public SoapSerializationEnvelope envelope;
public String methodName;
public SoapObject request;
public WSRequest(String methodName)
{
this.methodName = methodName;
this.request = new SoapObject(SRWebServer.NAMESPACE, methodName);
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.implicitTypes = true;
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
androidHttpTransport = new HttpTransportSE(SRWebServer.URL);
}
public void RegisterMarshal()
{
MarshalBase64 marshal = new MarshalBase64();
marshal.register(envelope);
}
public SoapObject Send() throws IOException, XmlPullParserException
{
System.setProperty("http.keepAlive", "false");
new MarshalDate().register(envelope);
this.androidHttpTransport.call(SRWebServer.NAMESPACE + this.methodName, envelope);
return (SoapObject) this.envelope.getResponse();
}
public void AddProperties(String name, Object value)
{
this.request.addProperty(name, value);
}
//
}
and use this way:
WSRequest request = new WSRequest("method name here");
request.addProperties("property1Name",property1);
request.Send();
requestSend() will return a SoapObject containing the object received from the web service.
Related
i need to send a multiple values to a webservice -asp.net- via android application
this is the webservice method
<WebMethod()> _
Public Function AddTheNums(ByVal nums() As String,) As String
For i = 0 To nums.Length - 1
--some process
Next
Return status
End Function
and i use this code to in android
public class Login extends AsyncTask<String, Void, String>
{
public Login(String MethodName)
{
}
public void onPreExecute()
{
}
#Override
protected String doInBackground(String... params)
{
final SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("nums", params[0]);
request.addProperty("nums", params[1]);
final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try
{
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
response = result.toString();
}
return response;
}
#Override
public void onPostExecute(String res)
{
}
}
can any one help me on this i need to send a multiple nums to the parameter in webserive
i used this code
request.addProperty("nums", params[0]);
request.addProperty("nums", params[1]);
but it doesnt work correctly ...
Best Regards
I'm not really sure that it's possible to use multiple params with same name in soapobject. Isn't it better to pass param like
request.addProperty("nums", params);
And I suppose this discussion can help https://groups.google.com/forum/#!topic/ksoap2-android/pq1V2ZXY3D8
In my WebService, I have simple class:
public class UserName
{
public UserName() { }
public UserName(string loginname, bool logged)
{
this._loginname = loginname;
this._logged = logged;
}
public string Loginname
{
set { this._loginname = value; }
get { return this._loginname; }
}
public bool Logged
{
set { this._logged = value; }
get { return this._logged; }
}
private string _loginname = string.Empty;
private bool _logged = false;
}
I communication with my IIS (ADB Emulator):
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
HttpTransportSE transport= new HttpTransportSE(URL);
transport.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
OK, this works fine.
I don't know how get UserName object in android code.
How to make a response object class?
I am actually working with one of these services right now, and this is what we were forced to do.
With the SOAP Service that I am workingo n, everything is returned as XML. You can use the following code to view the raw response from your service, and then decide what to do from there.
transport.responseDump
That String that is returned will give you the full response from the transport. You need to make sure that debugging is turned on first, this is done with the following.
transport.debug=true;
change your line:
SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
to
SoapObject response=(SoapObject)envelope.bodyIn;
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
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 have a problem while calling a webservice. I have a .NET web service on the server, and I am using KSOAP2 (ksoap2-j2se-full-2.1.2) in Android. While running the program I got an runtime exception like "org.ksoap2.serialization.SoapPrimitive". What should I do?
Here is my code.
package projects.ksoap2sample;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.*;
import android.os.*;
import android.widget.TextView;
public class ksoap2sample extends Activity {
/** Called when the activity is first created. */
private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
private static final String METHOD_NAME = "HelloWorld";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://192.168.1.19/TestWeb/WebService.asmx";
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.text1);
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//request.addProperty("prop1", "myprop");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = (Object)envelope.getResponse();
String[] results = (String[]) result;
tv.setText( ""+results[0]);
}
catch (Exception e) {
tv.setText(e.getMessage());
}
}
}
It's very simple. You are getting the result into an Object which is a primitive one.
Your code:
Object result = (Object)envelope.getResponse();
Correct code:
SoapObject result=(SoapObject)envelope.getResponse();
//To get the data.
String resultData=result.getProperty(0).toString();
// 0 is the first object of data.
I think this should definitely work.
How does your .NET Webservice look like?
I had the same effect using ksoap 2.3 from code.google.com. I followed the tutorial on The Code Project (which is great BTW.)
And everytime I used
Integer result = (Integer)envelope.getResponse();
to get the result of a my webservice (regardless of the type, I tried Object, String, int) I ran into the org.ksoap2.serialization.SoapPrimitive exception.
I found a solution (workaround). The first thing I had to do was to remove the "SoapRpcMethod() attribute from my webservice methods.
[SoapRpcMethod(), WebMethod]
public Object GetInteger1(int i)
{
// android device will throw exception
return 0;
}
[WebMethod]
public Object GetInteger2(int i)
{
// android device will get the value
return 0;
}
Then I changed my Android code to:
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
However, I get a SoapPrimitive object, which has a "value" filed that is private. Luckily the value is passed through the toString() method, so I use Integer.parseInt(result.toString()) to get my value, which is enough for me, because I don't have any complex types that I need to get from my Web service.
Here is the full source:
private static final String SOAP_ACTION = "http://tempuri.org/GetInteger2";
private static final String METHOD_NAME = "GetInteger2";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://10.0.2.2:4711/Service1.asmx";
public int GetInteger2() throws IOException, XmlPullParserException {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi = new PropertyInfo();
pi.setName("i");
pi.setValue(123);
request.addProperty(pi);
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
return Integer.parseInt(result.toString());
}
Typecast the envelope to SoapPrimitive:
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
String strRes = result.toString();
and it will work.
You can Use below code to call the web service and get response .Make sure that your Web Service return the response in Data Table Format..This code help you if you using data from SQL Server database .If you you using MYSQL you need to change one thing just replace word NewDataSet from sentence obj2=(SoapObject) obj1.getProperty("NewDataSet"); by DocumentElement
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://localhost/Web_Service.asmx?"; // you can use IP address instead of localhost
private static final String METHOD_NAME = "Function_Name";
private static final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("parm_name", prm_value); // Parameter for Method
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope); //call the eb service Method
} catch (Exception e) {
e.printStackTrace();
} //Next task is to get Response and format that response
SoapObject obj, obj1, obj2, obj3;
obj = (SoapObject) envelope.getResponse();
obj1 = (SoapObject) obj.getProperty("diffgram");
obj2 = (SoapObject) obj1.getProperty("NewDataSet");
for (int i = 0; i < obj2.getPropertyCount(); i++) //the method getPropertyCount() return the number of rows
{
obj3 = (SoapObject) obj2.getProperty(i);
obj3.getProperty(0).toString(); //value of column 1
obj3.getProperty(1).toString(); //value of column 2
//like that you will get value from each column
}
If you have any problem regarding this you can write me..
If more than one result is expected, then the getResponse() method will return a Vector containing the various responses.
In which case the offending code becomes:
Object result = envelope.getResponse();
// treat result as a vector
String resultText = null;
if (result instanceof Vector)
{
SoapPrimitive element0 = (SoapPrimitive)((Vector) result).elementAt(0);
resultText = element0.toString();
}
tv.setText(resultText);
Answer based on the ksoap2-android (mosabua fork)
I think you can't call
androidHttpTransport.call(SOAP_ACTION, envelope);
on main Thread.
Network operations should be done on different Thread.
Create another Thread or AsyncTask to call the method.