I have a .net Api to send data on the server.I am using KSoap2 to post that data But fail to do .I am using the code
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new backMethod().execute(); // This is an important Step
}
public class backMethod extends AsyncTask<String, Object, Object > {
private final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
#Override
protected void onPreExecute() {
this.dialog.setMessage("Checking...");
this.dialog.show();
}
#Override
protected void onPostExecute(Object result) {
//Here All your UI part is Done
if (result != null) {
TextView tv=(TextView)findViewById(R.id.textView1);
tv.setTag(result);
} else {
Toast.makeText(getApplicationContext(),
"Result Found is == " + result + "", Toast.LENGTH_LONG).show();
}
super.onPostExecute(result);
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
super.onPostExecute(result);
}
#Override
protected Object doInBackground(String... params) {
SoapObject request = new SoapObject(NAMESPACE, OPERATION_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
response = (SoapObject) envelope.getResponse();
//here SoapPrimitive is an important part
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
while debugging flow of code automatically move to catch block and give the exception network not found.
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'm using SOAP, In AsyncTask classs I'm trtying to set text of driverIDText, shipmentIDText, freightShipment textviews but it's saying unable to resolve setText()
Here is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_up);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView driverIDText = (TextView) findViewById(R.id.text_DriverID);
TextView shipmentIDText = (TextView) findViewById(R.id.text_ShipmentID);
TextView freightShipment = (TextView) findViewById(R.id.text_FreightShipment);
new RetrieveFeedTask(driverIDText.getText.toString(),
shipmentIDText.getText().toString(),
freightShipment.getText().toString()
).execute();
}
class RetrieveFeedTask extends AsyncTask<String, String, Void> {
private String driverIDText, shipmentIDText, freightShipment;
RetrieveFeedTask(String driverIDText, String shipmentIDText, String freightShipment) {
this.driverIDText = driverIDText;
this.shipmentIDText = shipmentIDText;
this.freightShipment = freightShipment;
}
protected Void doInBackground(String... urls) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("DriverID", driverIDText);
request.addProperty("ShipmentID", shipmentIDText);
request.addProperty("FreightShipment", freightShipment);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
if (result != null) {
// HERE IS PROBLEM IN SETTEXT()
driverIDText.setText(result.getProperty(0).toString());
} else {
Toast.makeText(getApplicationContext(), "Login Failed!", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Inside AsyncTask you declared driverIDText as String not TextView, So Create one more param for TextView and send driverIDText(TextView) from Oncreate and set the data in textView in onPostExecute.
Ex:
class RetrieveFeedTask extends AsyncTask<String, String, String> {
private String driverIDText, shipmentIDText, freightShipment;
private TextView driverIDTextTv;
RetrieveFeedTask(String driverIDText, String shipmentIDText, String freightShipment, TextView textView) {
this.driverIDText = driverIDText;
this.shipmentIDText = shipmentIDText;
this.freightShipment = freightShipment;
driverIDTextTv = textView;
}
#Override
protected void onPostExecute(String text) {
super.onPostExecute(text);
driverIDTextTv.setText(text);
}
protected String doInBackground(String... urls) {
String localDriverId = "";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("DriverID", driverIDText);
request.addProperty("ShipmentID", shipmentIDText);
request.addProperty("FreightShipment", freightShipment);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
if (result != null) {
// HERE IS PROBLEM IN SETTEXT()
// driverIDText.setText(result.getProperty(0).toString());
localDriverId = result.getProperty(0).toString();
} else {
Toast.makeText(getApplicationContext(), "Login Failed!", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
return localDriverId;
}
}
and you can call AsyncTask like this,
new RetrieveFeedTask(driverIDText.getText.toString(),
shipmentIDText.getText().toString(),
freightShipment.getText().toString(), driverIDText
).execute();
I'm using ksoap to make request.
But, I'm having doubts on how to send header and below format in soap request.
I'm having soap request data, I want to send soap request to server:
Soap Request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ihc="http://testingsite.com" xmlns:ihcl="http://testingsite.com">
<soapenv:Header>
<wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext">
<wsse:UsernameToken>
<wsse:Username>name</wsse:Username>
<wsse:Password>password</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<MyList xmlns="http://testingsite.com">
<Details>
<Name1>hi</Name1>
<Name2>Hello</Name2>
<MyAddress>
<Address1 IsPrimary="Y">
<Add1>Personal</Add1>
<Add2>Personal</Add2>
</Address1>
</MyAddress>
</Details>
</MyList>
</soapenv:Body>
</soapenv:Envelope>
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.test);
AsyncTaskRunner runner = new AsyncTaskRunner();
runner.execute();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
private class AsyncTaskRunner extends AsyncTask<String, String, String> {
private String resp;
#Override
protected String doInBackground(String... params) {
publishProgress("Loading contents...");
try {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
SoapObject request = new SoapObject(NAMESPACE, METHOD);
PropertyInfo quotesProperty = new PropertyInfo();
request.addProperty("WsClientName","RAKDEDTester");
request.addProperty("Password", "d41d8cd98f00b204e9800998ecf8427e");
envelope.bodyOut = request;
HttpTransportSE transport = new HttpTransportSE(URL);
try {
transport.call(NAMESPACE + SOAP_ACTION_PREFIX + METHOD, envelope);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
if (envelope.bodyIn != null) {
SoapPrimitive resultSOAP = (SoapPrimitive) ((SoapObject) envelope.bodyIn).getProperty(0);
resp=resultSOAP.toString();
}
} catch (Exception e) {
e.printStackTrace();
resp = e.getMessage();
}
return resp;
}
#Override
protected void onPostExecute(String result) {
textView.setText(result);
}
#Override
protected void onPreExecute() {
}
#Override
protected void onProgressUpdate(String... text) {
textView.setText(text[0]);
}
}
}
Tried using Async but got can't handler even there is no toast in it,
also tried call calling method runUIThread but no luck ,
Async method works but it also gets run time exception
when i use Async method data gets on the Textview too late (5-10 mins) , but when i use it in debuggers it gets instantly ,
below webservice is test using wizdl application and response is instant
i'm confused please help
private final String NAMESPACE = "http://????????.com/EventGetter/";
private final String URL = "http://?????????.com/EventGetter.asmx";
private final String SOAP_ACTION = "http://????????.com/EventGetter/GetTimeTable";
private final String METHOD_NAME = "GetTimeTable";
private class TPaccessData extends AsyncTask<String, Void, Void>{
#Override
protected Void doInBackground(String... params) {
GetData();
return null;
}
#Override
protected void onPostExecute(Void result) {
Log.i(LogStr, "onPostExecute");
}
#Override
protected void onPreExecute() {
GetData();
Log.i(LogStr, "onPreExecute");
}
#Override
protected void onProgressUpdate(Void... values) {
Log.i(LogStr, "onProgressUpdate");
}
}
public void GetData(){
//Create request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//set properties aka param
request.addProperty("stream","where ttBatch = 'F.Y.J.C'");
//Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
//Set output SOAP object
envelope.setOutputSoapObject(request);
//Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.debug = true;
//Invole web service
androidHttpTransport.call(SOAP_ACTION, envelope);
//Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
//Assign it to Text static variable
text.setText(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
also tried calling get data in oncreate method
NotifyList.this.runOnUiThread(new Runnable() {
#Override
public void run() {
GetData();
// TPaccessData task = new TPaccessData();
// task.execute();
}
});
Remove youe GetData() method from onPreExecute()
and call this line inside onPost
text.setText(response.toString());
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");
}
}