JAVA web service receiving null parameters from Android Ksoap - java

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.

Related

Getting Exception While Invoking Web Service From Android

I am a beginner in this, i am facing an issue while invoking asp.net web service method (SignUp) from android. It works fine when i try it from browser but when i do it from the android it gives me some exception like
"
SoapFault - faultcode: 'soap:Client' faultstring: 'System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: SignUp.
at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)' faultactor: 'null' detail: org.kxml2.kdom."
This is my Java Signup class
String SOAP_ACTION1 = "http://tempuri.org/SignUp";
String METHOD_NAME1 = "SignUp";
btn = (Button)findViewById(R.id.btn_signup);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new WebService().execute(METHOD_NAME1,SOAP_ACTION1,txt_name.getText().toString(), txt_email.getText().toString(),txt_password.getText().toString(),
txt_address.getText().toString(),
txt_dob.getText().toString(),
txt_account.getText().toString(),
txt_cnic.getText().toString(),txt_pobox.getText().toString(),
txt_city.getText().toString(),txt_status.getText().toString());
This is My Java WebService Class
private static String NAMESPACE = "http://tempuri.org/";
private static String URL = "http://192.168.0.102/MyWebService/WebService1.asmx?WSDL";
protected String doInBackground(String... params) {
String s="";
String whichmethodcall = params[0];
switch(whichmethodcall){
case "SignUp":
s = signup(params);
break;
case "SignIn":
s = signin(params);
break;
}
return s;
}
public String signup(String[] arr){
String s = "";
SoapObject request = new SoapObject(NAMESPACE, arr[1]);
//Use this to add parameters
request.addProperty("cname",arr[2]);
request.addProperty("cemail",arr[3]);
request.addProperty("cpwd",arr[4]);
request.addProperty("caddress",arr[5]);
request.addProperty("cdob",arr[6]);
request.addProperty("caccount",arr[7]);
request.addProperty("cCnic",arr[8]);
request.addProperty("cpobox",arr[9]);
request.addProperty("cCity",arr[10]);
request.addProperty("cstatus",arr[11]);
//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new
SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
//this is the actual part that will call the webservice
androidHttpTransport.call(arr[0], envelope);
// Get the SoapResult from the envelope body.
// SoapObject result = (SoapObject)envelope.bodyIn;
// SoapObject result = (SoapObject) envelope.getResponse();
if (envelope.bodyIn instanceof SoapFault) {
final SoapFault result = (SoapFault) envelope.bodyIn;
if (result != null) {
//Get the first property and change the label text
s = result.toString();
} else
s = "No response";
}
} catch (Exception e) {
e.printStackTrace();
s=e.getMessage().toString();
}
return s;
}
And This is My asp.net WebService method
public string SignUp(string cname, string caddress, string cdob, long caccount, long cCnic, int cpobox, string cCity, string cstatus,string cemail, string cpass)
{
bool check = ConnectDB(); // Opens the Connection to Insert The new User
string msg = "";
//DateTime dob = DateTime.ParseExact(cdob, "dd/mm/yyyy", null);
string query = "INSERT INTO Customers values('" + cname + "','" + caddress + "','" + cdob + "','" + caccount + "','" + cCnic + "','" + cpobox + "','" + cCity + "','" + cstatus + "','"+ cemail + "','"+ cpass + "')";
SqlCommand comm = new SqlCommand(query, con);
try{
if (check == true){
if (comm.ExecuteNonQuery() > 0)
msg = "you've signed up !";
else
msg = "sign up error";
}
else
msg = "Database not Connected";
}catch (SqlException ex){
msg = ex.Message.ToString();
con.Close();
}finally{
con.Close();
}
return msg;
}
Can you try this
instead of SoapObject request = new SoapObject(NAMESPACE, arr[1]);
use
SoapObject request = new SoapObject(NAMESPACE, arr[0]);
In the soap object you should pass just method name, without http://...

Change form of webservice response to array in java

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;
}
}

WCF WebService on Android project - How to get byte[] type via SoapObjects?

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

How do I call a .NET webmethod?

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();

How to pass parameter to a webservice using ksoap2?

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);
}
}

Categories