This is my web service code.I don't know how to parse soap response.what should i do? i don't even know it is json or Xml.So please tell me which type of response it is?
my response like this
anyType{Product_Details=anyType{P_id=129;p_name=ffff;C_name=gggg;};}
public class MyAsyncTask extends AsyncTask<String, String , String>
{
#Override
protected void onPostExecute(String arrPersons )
{
uid.setText(arrPersons);
}
#Override
protected void onProgressUpdate(String... text)
{
uid.setText(text[0]);
}
#Override
protected String doInBackground(String... arg0)
{
SOAP_ADDRESS="";
request=new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
publishProgress("Loading contents...");
PropertyInfo pi=new PropertyInfo();
pi.setName("PID");
pi.setValue(Integer.parseInt(arg0[1]));
pi.setType(String.class);
request.addProperty(pi);
pi=new PropertyInfo();
envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
httpTransport=new HttpTransportSE(SOAP_ADDRESS);
try
{
httpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject)envelope.getResponse();
for (int i = 0; i < response.getPropertyCount(); i++)
{
Object property = response.getProperty(i);
if (property instanceof SoapObject)
{
SoapObject category_list = (SoapObject) property;
String returnString1 = category_list.getProperty(0).toString();
}
}
}
catch (Exception e)
{
returnString1 = e.getMessage();
}
return returnString1;
}
}
The format might in data table return format. you better show your web services code which you created on asp.net
This is my asp code for Web service
public class qr : System.Web.Services.WebService {
[WebMethod]
public DataTable findData(String PID)
{
String constr = ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Product_Details WHERE P_id = #P_id"))
{
cmd.Parameters.AddWithValue("#P_id", PID);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
dt.TableName = "Product_Details";
sda.Fill(dt);
return dt;
}
}
}
}
}
Related
Im trying to get some response from several SOAP webservices at last i tried to run a code that is a well very known example on the internet. But i realized that even this doesnt run on my project. I hardly tried to understand what the error could be but i dont know why its not working with soap.
I would really appriacate your help.
Downloaded new version of KSOAP2 and also permission for internet is given.
public class WEBSERVİCE extends AppCompatActivity {
Button btn;
EditText et;
TextView txv;
String celcius="21";
String fahren;
private String NAMESPACE = "https://www.w3schools.com/xml/";
private String METHOD_NAME = "CelsiusToFahrenheit";
private String SOAP_ACTİON = "https://www.w3schools.com/xml/CelsiusToFahrenheit";
private String URL = "https://www.w3schools.com/xml/tempconvert.asmx?op=CelsiusToFahrenheit?WSDL";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.button);
txv = findViewById(R.id.textView);
et = findViewById(R.id.editText1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AsyncCallWS task = new AsyncCallWS();
task.execute();
}
});
}
private class AsyncCallWS extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
txv.setText("calculating");
super.onPreExecute();
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
#Override
protected String doInBackground(String... objects) {
return getBolum(celcius);
}
#Override
protected void onPostExecute(String o) {
txv.setText(fahren + "F");
super.onPostExecute(o);
}
}
public String getBolum(String celsius) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi = new PropertyInfo();
pi.setName("Celcius");
pi.setValue(celsius);
pi.setType(double.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new
SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHTTPTransport = new HttpTransportSE(URL);
try {
androidHTTPTransport.call(SOAP_ACTİON, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
fahren = response.toString();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return fahren;
}
}
No Error Messages but the value it turns back is always "null"
EDIT:posted changed code again
There isn't anything wrong with SOAP Api. The problem is your AsyncTask class. Read the documentation for AsyncTask first. Please do proper research before you use any code from internet. Always read about the components that are used snippets on internet otherwise you are going to have hard time figuring out problems.
Your AsyncTask class is declared as:
private class AsyncCallWS extends AsyncTask<String,Void,Void>
Change it to
private class AsyncCallWS extends AsyncTask<String,Void,String>
Third generic parameter in your Void which is supposed to be result type. So in your case your async task won't return any data once it is finished.
//CHANGE TYPE TO STRING
public String getBolum(String celsius) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi=new PropertyInfo();
pi.setName("Celcius");
pi.setValue(celsius);
pi.setType(double.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHTTPTransport = new HttpTransportSE(URL);
try {
androidHTTPTransport.call(SOAP_ACTİON, envelope);
SoapPrimitive response= (SoapPrimitive) envelope.getResponse();
//RETURN RESULT
return response.toString();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
//I forgot this line previously:
return "";
}}
In your async task, you need to change return type of doInBackground and paramter of onPostExecute to String:
#Override
protected String doInBackground(String... objects) {
return getBolum(celcius); //RETURN RESULT
}
#Override
protected void onPostExecute(String result) {
txv.setText(result+"F");
fahren = result;
super.onPostExecute(result);
}
It should work now.
I added the Row_Cursor_Adapter globally and made changes after adding onPostExecute() method in the Service_ivr AsyncTask.This is the updated code.
class Service_ivr extends AsyncTask<String, Void, String>
{
#Override
protected String doInBackground(String... param)
{
SoapObject request = new SoapObject(NAMESPACE ,METHOD_NAME);
request.addProperty("user_id",param[0]);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut=request;
envelope.dotNet =true;
envelope.setOutputSoapObject(request);
try
{
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject obj2 = (SoapObject)envelope.getResponse();
int count = obj2.getPropertyCount();
index = count/7;
final lead_content_IVR [] total_ivr_data = new lead_content_IVR[index];
for(int i=0; i<index ;i++)
{
String call_duration;
String lead_id = obj2.getPropertyAsString(i*7+0);
String lead_call_from = obj2.getPropertyAsString(i*7+1);
String lead_call_to = obj2.getPropertyAsString(i*7+2);
String lead_date=obj2.getPropertyAsString(i*7+3);
String lead_audio=obj2.getPropertyAsString(i*7+4);
String assign_id = obj2.getPropertyAsString(i*7+5);
String time = obj2.getPropertyAsString(i*7+6);
if(lead_call_from.equals("Welcome Sound") || lead_call_from.equals("Call Missed") || lead_call_from.equals("User Disconnected") || lead_call_from.equals("Customer Missed"))
{
call_duration= "5 sec";
}
else
{
call_duration = time.toString().concat(" sec");
}
total_ivr_data[i] = new lead_content_IVR(lead_id,lead_call_from,lead_call_to,lead_date,lead_audio,assign_id,call_duration);
}
adapter = new RowCursorAdapter_IVR(Activity_IVR_Lead.this, R.layout.listview_layout_ivr,total_ivr_data);
}catch(Exception e)
{
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
listView.setAdapter(adapter);
}
}
As u suggest i update the code but error is same all the time.
Change your method like so
class service_ivr extends AsyncTask<String, Void, String>
{
#Override
protected lead_content_IVR[] doInBackground(String... param)
{
SoapObject request = new SoapObject(NAMESPACE ,METHOD_NAME);
request.addProperty("user_id",param[0]);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut=request;
envelope.dotNet =true;
envelope.setOutputSoapObject(request);
lead_content_IVR [] total_ivr_data = null;
try
{
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject obj2 = (SoapObject)envelope.getResponse();
int count = obj2.getPropertyCount();
index = count/7;
total_ivr_data = new lead_content_IVR[index];
for(int i=0; i<index ;i++)
{
String call_duration;
String lead_id = obj2.getPropertyAsString(i*7+0);
String lead_call_from = obj2.getPropertyAsString(i*7+1);
String lead_call_to = obj2.getPropertyAsString(i*7+2);
String lead_date=obj2.getPropertyAsString(i*7+3);
String lead_audio=obj2.getPropertyAsString(i*7+4);
String assign_id = obj2.getPropertyAsString(i*7+5);
String time = obj2.getPropertyAsString(i*7+6);
if(lead_call_from.equals("Welcome Sound") || lead_call_from.equals("Call Missed") || lead_call_from.equals("User Disconnected") || lead_call_from.equals("Customer Missed"))
{
call_duration= "5 sec";
}
else
{
call_duration = time.toString().concat(" sec");
}
total_ivr_data[i] = new lead_content_IVR(lead_id,lead_call_from,lead_call_to,lead_date,lead_audio,assign_id,call_duration);
}
}catch(Exception e)
{
e.printStackTrace();
}
return total_ivr_data;
}
public void onPostExecute(lead_content_IVR [] total_ivr_data ) {
RowCursorAdapter_IVR adapter = new RowCursorAdapter_IVR(Activity_IVR_Lead.this, R.layout.listview_layout_ivr,total_ivr_data);
listView.setAdapter(adapter);
}
}
Please anyone set this code,I have tried several different code to parse soap-envelop how to handle this type of response
public class Main Activity extends Activity
{
TextView res;
String str, he, hh;
SoapSerializationEnvelope envelope;
private final String URL = "http://10.0.2.2:port/Service1.svc?wsdl";
private final String NAMESPACE = "http://tempuri.org/";
private final String SOAP_ACTION = "http://tempuri.org/IService1/GetData";
private final String METHOD_NAME = "GetData";
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Hello", "Fault_Exception_WebService Project Called.........");
myasynctask MAT = new myasynctask();
MAT.execute();
}
private class myasynctask extends AsyncTask<Void, Integer, String>
{
#Override
protected void onPreExecute()
{
}
#Override
protected void onPostExecute(String result)
{
TextView tv= (TextView)findViewById(R.id.heloo);
tv.setText(he);
}
#Override
protected String doInBackground(Void... params)
{
try
{
// Soap Call
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("value", "1");
envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION, envelope);
Log.i("res......", fulatcode.toString());
}
catch (SoapFault fault)
{
Log.v("TAG", "soapfault = "+fault.toString());
String strFault = ((SoapFault) envelope.bodyIn).faultstring;
String strfalutcode =((SoapFault)envelope.bodyIn).faultcode;
String strfaultactor =((SoapFault)envelope.bodyIn).faultactor;
String strmessage = ((SoapFault)envelope.bodyIn).getMessage();
String a= String.valueOf(strmessage);
Log.v("TAG", "Message:"+a);
Log.v("TAG", "Code:"+strfalutcode.toString()+"String:"+strFault.toString());
he="Code:"+strfalutcode.toString()+"String:"+strFault.toString() ;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
}
Here is my soap response:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header />
<s:Body>
<s:Fault>
<faultcode>s:2</faultcode>
<faultstring xml:lang="en-US">sdajkgfuio</faultstring>
<detail>
<custom_fault xmlns="http://schemas.datacontract.org/2004/07/testing_fault" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<error_code1>2</error_code1>
<error_code2>3</error_code2>
<error_message1>There is no employee exist with the emp id</error_message1>
<error_message2>password is empty</error_message2>
</custom_fault>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>
here is my logcat responce:
12-04 11:54:43.839: V/TAG(1154): soapfault = SoapFault - faultcode: 's:2' faultstring: 'sdajkgfuio' faultactor: 'null' detail: org.kxml2.kdom.Node#41024470
12-04 11:54:43.839: V/TAG(1154): Message:sdajkgfuio
12-04 11:54:43.839: V/TAG(1154): Code:s:2String:sdajkgfuio
Try this
SoapObject tabResult = (SoapObject) tabResponse .getProperty(0);
if not worked than try this
for(int i=0;i<elementData.getPropertyCount();i++)
{
SoapObject getAllData = (SoapObject) elementData.getProperty(i);
if (getAllData instanceof SoapObject) {
String data= getAllData.getProperty({property_name}).toString();
}
}
}
else
{
Log.i("No Response","error");
return null;
}
and Explore this one
http://seesharpgears.blogspot.in/2010/10/ksoap-android-web-service-tutorial-with.html
I got the main problem
you are getting the cast errors edit your question to
"How to parse the "detail" node of the Soap Fault Object using the ksoap api"
try manual parsing to do the things using the .getproperties()
and see more
http://docs.oracle.com/cd/E19159-01/819-3669/bnbin/index.html
I have written a program for communicating with a web service and get response value. But when i debug the programme i end with requestDump=null at the line androidHttpTransport.call(SOAP_ACTION, envelope); Can some one tell me the reason for the error and what can i do for this
public class WebService extends Activity {
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
private final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private final String METHOD_NAME = "CelsiusToFahrenheit";
String celsius;
Button b;
TextView tv;
EditText et;
String res,resultval;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_service);
et=(EditText)findViewById(R.id.editText1);
tv=(TextView)findViewById(R.id.Result);
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//String result=getFarenheit(et.getText().toString());
//tv.setText(result+"°F");
new service().execute();
}
});
}
private class service extends AsyncTask<Void, Void, String>{
#Override
protected String doInBackground(Void... arg0) {
celsius=et.getText().toString();
SoapObject request= new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo celsuiusPI= new PropertyInfo();
celsuiusPI.setName("Celsius");
celsuiusPI.setValue(celsius);
celsuiusPI.setType(String.class);
request.addProperty("XMLMarks",celsuiusPI);
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope (SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.implicitTypes = true;
envelope.enc = SoapSerializationEnvelope.ENC2003;
envelope.xsd = SoapEnvelope.XSD;
envelope.xsi = SoapEnvelope.XSI;
envelope.setOutputSoapObject(request);
envelope.setAddAdornments(false);
SoapPrimitive response;
HttpTransportSE androidHttpTransport=new HttpTransportSE(URL);
try{
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, envelope);
String dump= androidHttpTransport.requestDump.toString();
response=(SoapPrimitive)envelope.getResponse();
Toast.makeText(WebService.this, response.toString(), 20).show();
Log.i("WebService output", response.toString());
System.out.println("WebService Response"+response.toString());
Object res= response.toString();
resultval=(String) res;
}
catch(Exception e){
e.printStackTrace();
}
return res;
}
protected void onPostExecute(String h){
String result=h;
tv.setText(result+"°F");
}
}
}
Just replace your service AsyncTask with this new one and see result:
code:
private class service extends AsyncTask<Void, Void, String> {
#Override
protected String doInBackground(Void... arg0) {
System.out.println("In DoIn Background");
// Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// Use this to add parameters
request.addProperty("Celsius", txtCel.getText().toString());
// 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(SOAP_ACTION, envelope);
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject) envelope.bodyIn;
if (result != null) {
// Get the first property and change the label text
// txtFar.setText(result.getProperty(0).toString());
res = result.getProperty(0).toString();
} else {
Toast.makeText(getApplicationContext(), "No Response",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
protected void onPostExecute(String h) {
String result = h;
tv.setText(result + "°F");
}
}
This is the function which is to call the function in the server to perform some task. But i have to pass in a ArrayList and a String value. I have trouble passing the ArrayList to the server. Can anyone tell me what i should do?
public void findLocation(ArrayList<APData> apdatalist, String profilename){
SoapObject request = new SoapObject(NAMESPACE, "FindLocation");
PropertyInfo quotesProperty = new PropertyInfo();
quotesProperty.setName("profileName");
quotesProperty.setValue(profilename);
quotesProperty.setType(String.class);
request.addProperty(quotesProperty);
request.addProperty("AP_List", APData.class);
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpRequest = new HttpTransportSE(URL);
httpRequest.debug = true;
String result = "";
try
{
httpRequest.call(SOAP_ACTION, envelope);
Log.e("Request",httpRequest.requestDump.toString());
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
Log.e("Response",httpRequest.responseDump.toString());
result = response.toString();
if(result == null){
Log.e("AndroidRuntime", "No location result is return!");
}
}
catch(Exception e)
{
e.printStackTrace();
}
//return temp;
}
I've found a possible solution. I've modified the SoapSerializationEnvelope class to deal with ArrayList.
I modified the method
public void writeObjectBody(XmlSerializer writer, KvmSerializable obj)
public void writeObjectBody(XmlSerializer writer, KvmSerializable obj) throws IOException
{
// Added this code for parsing attributes of KvmSerializable objects
int cnt = obj.getPropertyCount();
PropertyInfo info = new PropertyInfo();
for (int i = 0; i < cnt; i++)
{
Object prop = obj.getProperty(i);
if(!(prop instanceof SoapObject)) {
// prop is a PropertyInfo
obj.getPropertyInfo(i, properties, info);
// Added this code to parse ArrayList objects in requests
if(prop instanceof ArrayList<?>){
ArrayList<?> values = (ArrayList<?>)prop;
for(Object o : values){
writer.startTag(info.namespace, info.name);
writeProperty(writer, o, info);
writer.endTag(info.namespace, info.name);
}
}else if ((info.flags & PropertyInfo.TRANSIENT) == 0)
{
writer.startTag(info.namespace, info.name);
writeProperty(writer, obj.getProperty(i), info);
writer.endTag(info.namespace, info.name);
}
} else {
// prop is a SoapObject
SoapObject nestedSoap = (SoapObject)prop;
writer.startTag(nestedSoap.getNamespace(), nestedSoap.getName());
writeObjectBody(writer, nestedSoap);
writer.endTag(nestedSoap.getNamespace(), nestedSoap.getName());
}
}
}
This seems to work for me