I'm using Eclipse IDE to develop an android app. I'm trying to connect to a .net webservice. I'm using ksoap2 version 2.3
When I'm calling a webmethod with no parameters, it works fine. When I come to pass a parameter to the webmethod, I get null (while debugging the webservice I discovered that) and I get a null from the webmethod in the client side code.
Code:
package com.examples.hello;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloActivity extends Activity {
/** Called when the activity is first created. */
private static final String SOAP_ACTION = "http://Innovation/HRService/stringBs";
private static final String METHOD_NAME = "stringBs";
private static final String NAMESPACE = "http://Innovation/HRService/";
private static final String URL = "http://196.205.5.170/mdl/hrservice.asmx";
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.text1);
call();
}
public void call()
{
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//PropertyInfo PI = new PropertyInfo();
//request.addProperty("a", "myprop");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet=true;
envelope.encodingStyle = SoapSerializationEnvelope.XSD;
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = (Object)envelope.getResponse();
String results = result.toString();
tv.setText( ""+results);
} catch (Exception e) {
tv.setText(e.getMessage());
}
}
}
Why do I get the null response, how do I pass a parameter to a webservice using ksoap2?
Instead of
request.addProperty("a", "myprop");
try using
request.addProperty("arg0", "myprop");
I'm not an expect on ksoap2 but i'm pretty sure this sets the value of the first parameter to your web service function. Has worked perfectly for me.
Calling webservice by passing parameters from j2me
SoapObject request = new SoapObject("http://www.webserviceX.NET", "GetCitiesByCountry");
String soapAction = "http://www.webserviceX.NET/GetCitiesByCountry";
request.addProperty("CountryName", "india");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
envelope.dotNet = true;
HttpTransport ht = new HttpTransport("http://www.webservicex.net/globalweather.asmx");
ht.debug = true;
//System.err.println( ht.requestDump );
ht.call(soapAction,envelope);
System.out.println("####################: " +envelope.getResponse());
//SoapObject result = (SoapObject)envelope.getResponse();
I have been working with this for 2 days now and i finally got the solution. I submit my complete code and hope this will help. It Can pass Parameters and get response.
Inside the WebService file in .net C#:
[WebService(Namespace = "http://something/webservice/v1")]
[WebMethod]
public DateTime[] Function(Guid organizationId, Guid categoryId)
{
return ...;
}
Inside the Android code:
private final static String URL = "http://something/WebServices/WebService.asmx";
private final static String NAMESPACE = "http://something/webservice/v1";
public ArrayList<Object> getSoapObject(String METHOD_NAME, String SOAP_ACTION, Map<String, String> parameters){
try {
ArrayList<Object> sol = new ArrayList<Object>();
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
if(parameters != null){
for (Entry<String, String> para : parameters.entrySet()) {
request.addProperty(para.getKey(), para.getValue());
}
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
Log.d("Body", envelope.bodyOut.toString());
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result=(SoapObject)envelope.getResponse();
for(int i = 0; i < result.getPropertyCount(); i++){
sol.add(result.getProperty(i));
}
return sol;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public void getMenuEndDate(String orgId, String categoryId){
Date startDate = null;
Date endDate = null;
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("organizationId", orgId);
parameters.put("categoryId", categoryId);
ArrayList<Object> sol = getSoapObject("Function", "http://something/webservice/v1/Function", parameters);
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
try {
startDate = (Date)dateFormatter.parse(sol.get(0).toString());
endDate = (Date)dateFormatter.parse(sol.get(1).toString());
} catch (ParseException e) {
Log.d(TAG, "Exception i Date-Formatering");
e.printStackTrace();
}
}
Things to check:
Are the parameters named exactly the same as what is expected in the Web Service?
Check if you use Trailing "/" for the Namespace. Have the same in you application.
Try commenting out the line:
envelope.dotNet=true;
I did the same thing you did and when I read about this property being a really ugly hack, I commented it out for testing purposes and my parameter got passed correctly.
You will have to declare parameter type in client code:
SoapObject request = new SoapObject("http://tempuri.org/", "mymethod");
PropertyInfo p = new PropertyInfo();
p.setName("param_name_from_webservice");
p.setValue(true);
p.setType(Boolean.class);
request.addProperty(p);
In here Problem With the Order of Codes You Wrote, Don't Worry Try this, It's Worked for me.
private class ConversionAsyncTask extends AsyncTask<Void,Void,Void> {
private SoapPrimitive response;
protected Void doInBackground(Void... params) {
SoapObject request = new SoapObject(NAMESPACE, METHOD);
request.addProperty("a","5");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.setOutputSoapObject(request);
soapEnvelope.dotNet = true;
soapEnvelope.implicitTypes = true;
try {
HttpTransportSE aht = new HttpTransportSE(URL);
aht.call(SOAP_ACTION, soapEnvelope);
response = (SoapPrimitive) soapEnvelope.getResponse();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
temperatureTxt.setText("Status: " + response);
}
}
Related
I'm unable to figure this out. My code is throwing this error
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Vector
.WebService.parseJourney
public static ArrayList<Journey> parseJourney(Object response) {
ArrayList<Journey> rs = new ArrayList<Journey>();
try {
if (response == null) {
return rs;
}
#SuppressWarnings("unchecked")
Vector<Object> result = (Vector<Object>) response;
if (result.size() < 4) {
return rs;
}
Am sure have used generics with no issues in past.
Wow - that was quick.
The call to parseJourney :
Vector<EntryValue> values = new Vector<EntryValue>();
EntryValue value = new EntryValue();
SoapObject request = new SoapObject(NAMESPACE_ENTRY, METHOD_NAME_ENTRY);
PropertyInfo pi = new PropertyInfo();
pi.setName("values");
pi.setValue(values);
pi.setType(MyArrayList.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
HttpTransportSE aht = new HttpTransportSE(URL_ENTRY);
try {
aht.call(SOAP_ACTION_ENTRY, envelope);
Object response = envelope.getResponse();
resultEntry = parseJourney(response);
return response.toString();
}
catch (Exception e) {
ERROR_EXCEPTION = 1;
return e.toString();
}
}
parseJourney() receives a String and you are trying to cast it to Vector without testing it
if(response instanceof Vector)
{
Vector<Object> result = (Vector<Object>) response;
//...
}
And don't put #SuppressWarnings("unchecked"), it's generally a bad practice
The Object that is being passed into parseJourney() is a String, not a Vector. If you want more information than that, you'll need to include whatever code calls parseJourney().
I develop an android application and i send a class object to a webservice method and i supposed to take an array as response but it returns anyType{}.
Here is a part of my code.
Customer C = new Customer();
C.setProperty(0,"30000001");
PropertyInfo pi =new PropertyInfo();
pi.setName("customer");
pi.setValue(C);
pi.setType(C.getClass());
request.addProperty(pi);
try{
androidHttpTransport.call(SOAP_ACTION1, envelope);
SoapObject response = (SoapObject) envelope.bodyIn;
String[] denemeList;
denemeList = new String[response.getPropertyCount()];
for(int i=0; i<response.getPropertyCount(); i++)
{
denemeList[i] = response.getPropertyAsString(i).toString();
Log.d("This is the response",denemeList[i]);
}
}catch (Exception e) {
TextView01.setText("EXCEPTION");
e.printStackTrace();
}
I find some other codes except from this one but none of them work.
Anyone know what should i do?
Thanks.
NOTE : HERE IS MY CLASS , I THINK IT WILL HELP YOU JUST POST tHIS
CLASS AS IT IS IN UR PROJECT 2. REPLACE THE VARIABLE ACCORDINGLY 3.
CALL METHOD : getDistrictDetials IN SUPPOSE MAIN CLASS...THIS WILL
PRINT THE OUTPUT IN CONSOL WINDOW
public class WebServiceCaller {
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://www.MYSERVICE/Service.asmx";
private final String SOAP_ACTION = "http://tempuri.org/RASHTRWADI_State";
private final String METHOD_NAME = "RASHTRWADI_State";
public boolean getDistrictDetials() {
boolean result = false;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo propInfo1 = new PropertyInfo();
propInfo1.setName("State_Code");
propInfo1.setValue(1);
propInfo1.setType(int.class);
request.addProperty(propInfo1);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true; // put this only if the web service is .NET one
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
// SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
Object response1 = envelope.getResponse();
SoapObject response = (SoapObject) response1;
Log.i("myApp", response1.toString());
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
I am trying to consume a JAVA web service from android.
Here is what I have tried so far:
private void CallWebServiceDummy() {
// TODO Auto-generated method stub
try {
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER10);
soapEnvelope.dotNet = false;
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi = new PropertyInfo();
StringArraySerializer a = new StringArraySerializer();
a.add("hello"); a.add("world"); String n0 = NAMESPACE;
pi = new PropertyInfo(); pi.setName("a"); pi.setValue(a);
pi.setType(a.getClass()); pi.setNamespace(n0);
Request.addProperty(pi);
String b = "my name"; pi = new PropertyInfo(); pi.setName("b");
pi.setValue(b); Request.addProperty(pi);
soapEnvelope.setOutputSoapObject(Request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
Log.d("test", "request: " + androidHttpTransport.requestDump);
Log.d("test", "response: " + androidHttpTransport.responseDump);
SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;
String c = resultsRequestSOAP.toString();
} catch (Exception e) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, e.getMessage(), duration);
toast.show();
toast.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
}
}
My Java web service Code:
package MyPackage;
public class WebServiceClass {
public String addnumbers(String[] a, String b) {
String c = new StringBuilder("This the String1 ").append(a[0]).append(" merged with String2 ").append(b).toString();
return c;
}
}
My Globals:
private static final String NAMESPACE = "http://MyPackage"; private
static final String URL =
"http://10.0.2.2:8080/WebService/services/WebServiceClass?wsdl"; private
static final String SOAP_ACTION = "urn:addnumbers"; private static final
String METHOD_NAME = "addnumbers";
Issue:
The response I receive is:
addnumbersResponse{return=This the String1 merged with String2 my name; }
The first parameter is not being sent to web service. I have tried to remove this line:
soapEnvelope.dotNet = false; but its still not working.
Guys please help me out. I am stuck for two days. Thanks for any help provided.
Your method's first argument's type is String array, not only String. Instead of StringArraySerializer, please try adding strings manually. You should see this page for this: Adding an array of complex objects to the request.
Check if your namespace is right!
I spent three hours with the same problem and only then realized that the namespace that was declared in the WebService class was different from what I put in the creation of SoapObject.
I have Android / Java project with Network threads. It is connected to WCF WebService which provides me method GetAddonsTypes.
GetAddonsTypes return long, int, and string items so I use own Parser (String / regex) which also create object in myDataSource (it's SQLite database). Now I have problem with my second method which is called GetProducts. GetProducts return long, int and Image.
I Would like to store Image as byte[] type. But how I can deal with binary files with SoapObjects? Maybe I should cast this anyType{} to binary file, but how I can do it?
This is how looks SoapObject result from GetProducts (.toString())
anyType{DocumentElement=anyType{Tabela=anyType{ID=701; lg=1;
ProductImage=anyType{}; };
My Network Thread
Thread networkThread1 = new Thread() {
#Override
public void run() {
try {
final String METHOD_NAME = "GetAddonsTypes";
final String SOAP_ACTION = "http://tempuri.org/IService1/GetAddonsTypes";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION, envelope);
final SoapObject result=(SoapObject)envelope.getResponse();
runOnUiThread (new Runnable(){
public void run() {
ParseTable(result.getProperty(1).toString());
}
});
}
catch (Exception e) {
Log.e("WS", e.toString());
}
}
};
public void ParseTable(String input)
{
myDataSource myDatasource;
Pattern p = Pattern.compile("(PID=)(\\d*); (flg=)(\\d*); (Name=)(\\w*);");
Matcher m = p.matcher(input);
myDatasource = new myDatasource(this);
myDatasource.open();
while (m.find()) {
try {
myDatasource.createMyItem(Long.parseLong(m.group(2)), Integer.parseInt(m.group(4)), m.group(6));
}
catch (Exception e) {
Log.e("Parser Error", e.toString());
}
}
myDatasource.close();
}
Maybe this will help:
" In the getProperty method I'm using the following:
Info.type = MarshalBase64.BYTE_ARRAY_CLASS
"
From this post: http://supportforums.blackberry.com/t5/Java-Development/Getting-Image-Over-Using-Web-Service-and-Ksoap2/td-p/491835
Firstly thank you for all answers, this subject may be closed because I made what I want already. I mentioned about how to call a .NET webmethod below.
http://aaarkonusurum.blogspot.com/2011/10/android-ile-dataset-donen-bir-net.html
If you need only a method for calling a webservice method, use this:
public static SoapPrimitive callWebServiceMethod(String url,
String namespace, String methodName,
HashMap<String, Object> parameters, String soapAction)
throws IOException, XmlPullParserException, SoapFault {
SoapObject request = new SoapObject(namespace, methodName);
if (parameters != null) {
String[] keys = new String[0];
keys = (String[]) parameters.keySet().toArray(keys);
Object[] vals = (Object[]) parameters.values().toArray();
for (int i = 0; i < parameters.size(); i++) {
request.addProperty(keys[i], vals[i]);
}
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(url,
60 * 1000);
androidHttpTransport.call(soapAction, envelope);
return (SoapPrimitive) envelope.getResponse();
}
and in your activity:
private static final String NAMESPACE = "http://tempuri.org/";
private static final String METHOD_NAME = "MethodName";
private static final String SOAP_ACTION = "http://tempuri.org/MethodName";
String url = "http://...";
HashMap<String, Object> parameters = new HashMap<String, Object>();
parameters.put("param1", param1);
parameters.put("param2", param2);
//...
SoapPrimitive response = callWebServiceMethod(url,
NAMESPACE, METHOD_NAM, parameters,
SOAP_ACTION);
String webServiceResult = response.toString();