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());
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 want to return an array of strings to my Android client and populate a ListView.
I am using the SOAP library (org.ksoap2.*) to invoke an ASP.NET web service.
Here is the code for the web service:
1. ASP Web Service
Imports ...
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports ...
<WebService(Namespace:="...")>_
<WebService(ConformsTo:=...)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class EnquiryWS
Inherits System.Web.Services.WebService
' Web method
<WebMethod()> _
Public Function GetList() As String()
'Hardcoded list
Return New String() { "item1", "item2", "item3" }
End Function
I've tested the web service by accessing the asmx, there are no runtime errors.
I've also tested it with just a simple string, the web service returned the string to Android. Like this:
' Web method
<WebMethod()> _
Public Function GetString() As String
Return "My string."
End Function
2. Android Activity
Secondly, here is my Android code that is invoking the ASP.NET web service.
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class MainActivity extends AppCompatActivity {
private ArrayList<String> list;
private ListView listview;
private ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
//...
new GetPersonList().execute("AsyncTask String");
//...
}
// Inner AsyncTask class
private class GetPersonList extends AsyncTask<String, Integer,String> {
private static final String SOAP_ACTION = "https://myNamespace/GetList";
private static final String METHOD_NAME = "GetList";
private static final String NAMESPACE = "https://myNamespace/";
private static final String URL =
"https://myIISsite/myASMXfile.asmx";
#Override
protected void onPreExecute() {
super.onPreExecute();
// onPreExecute stuff
}
#Override
protected String doInBackground(String... arg) {
String result = null;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//Required for .net
envelope.dotNet = true;
//Set output SOAP object
envelope.setOutputSoapObject(request);
//Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
//Invoke web service
androidHttpTransport.call(SOAP_ACTION, envelope);
//Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
//Assign it to response to a static variable
result = response.toString();
} catch (Exception e) {
result = "error " + e.getMessage();
}
return result;
}
#Override
protected void onPostExecute(String result) {
System.out.println("Returned SOAP XML: " + result);
MyFunction(result);
}
}
}
MyFunction is a method that I created to do some additional work.
3. MyFunction
Here is MyFunction method code:
public void MyFunction(String s) {
// Add Webservice response to list
list.add(s);
// Set adapter
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item1, list);
listview.setAdapter(adapter);
}
The argument I pass to MyFunction is the SOAP response, I then add it to the list and set the adapter.
Okay, so the web service is returning an array of strings, but the overriden onPostExecute method is working with one string, if I declare the onPostExecute parameter as a Collection, it is obviously not overriding anymore.
This is the error that I am getting in logcat:
Return SOAP XML: error expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG <html>#1:7 in java.io.InputStreamReader#4182d238)
Could anyone please advise?
I have found a solution. I am casting the response as a SoapObject and not as a SoapPrimitive, the reason for that is because a SoapPrimitive is for primitive data types, SoapObject supports composite data types. Therefore, I am casting my array as a SoapObject and not as a SoapPrimitive anymore.
I have removed the MyFunction() method, because I am setting the adapter in the onPostExecute() method by override run(). And finally, I have added a ProgressDialog, I am showing the ProgressDialog in the onPreExecute() method while it's processing, and then I am invoking dismiss() in the onPostExecute() method.
private class GetPersonList extends AsyncTask<Void, Void, String> {
private static final String SOAP_ACTION = "http://myNamespace/myMethod";
private static final String METHOD_NAME = "myMethod";
private static final String NAMESPACE = "http://myNamespace/";
private static final String URL =
"http://myURL/myAsmxFile.asmx";
ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog= ProgressDialog.show(MainActivity.this,
"Wait",
"Retrieving data",
true
);
}
#Override
protected String doInBackground(Void... params) {
String finalResult = null;
//Create request object
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//Create envelope to which we add our request object
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
//Required for .net
envelope.dotNet = true;
//Add the request object to the envelope
envelope.setOutputSoapObject(request);
//Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
//Invoke web service
androidHttpTransport.call(SOAP_ACTION, envelope);
// Get the response
// Cast as SoapObject and not SoapPrimitive
SoapObject response = (SoapObject) envelope.getResponse();
//Assign it to response to a static variable
finalResult = response.toString();
// Now loop through the response (loop through the properties)
// and add them to the list
for(int i = 0; i < response.getPropertyCount(); i++) {
list.add(response.getProperty(i).toString());
}
} catch (Exception e) {
System.out.println("######## ERROR " + e.getMessage());
}
return finalResult;
}
#Override
protected void onPostExecute(String str) {
progressDialog.dismiss();
System.out.println("Returned SOAP XML: " + str);
runOnUiThread(new Runnable() {
#Override
public void run() {
// Set adapter
adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_item, R.id.product_name, list);
listview.setAdapter(adapter);
}
});
}
}
I have heard that there is another way of doing this using Gson/Json, I will post that once I have figured it out.
Cheerz
I am a newbie on android development. I am trying to develop an application which will connect with .net webservice in order to retrieve data. I would like to make the ksoap2 call with AsyncTask. How I call it asyncronus with asynctask?
My SoapCall class is
public class SoapCall {
public final static String SOAP_ACTION = "http://www.alpha.net.com/ExecuteEBSCommand";
public final static String OPERATION_NAME = "ExecuteEBSCommand";
public final static String NAMESPACE = "http://www.alpha.net.com";
public final static String URL = "http://192.168.2.100/Ebs2Alpha/Service.asmx";
public String connection(String Command, String CommandParameters) throws Throwable, Throwable {
String response = null;
SoapObject Request = new SoapObject(NAMESPACE, OPERATION_NAME);
Request.addProperty("strCommand", Command);
Request.addProperty("strCommandParameters", CommandParameters);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
// Needed to make the internet call
// Allow for debugging - needed to output the request
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
// this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject) soapEnvelope.bodyIn;
response = result.getProperty(0).toString();
return response;
}
}
So far I am getting the response by calling the connection method in main activity with
SoapCall call1= new SoapCall();
call1.connection("get_clients", "%");
Using AsyncTask is straightforward. Here is an example.
public class MyTask extends AsyncTask<String, Integer, String>{
#Override
protected String doInBackground(String... params) {
String response = null;
SoapObject Request = new SoapObject(NAMESPACE, OPERATION_NAME);
Request.addProperty("strCommand", params[0]);
Request.addProperty("strCommandParameters", params[1]);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
// Needed to make the internet call
// Allow for debugging - needed to output the request
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
// this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject) soapEnvelope.bodyIn;
response = result.getProperty(0).toString();
return response;
}
}
And the call to the task with parameters.
MyTask myTask = new MyTask();
myTask.execute(new String[] {Command, CommandParameters});
Hope it will help.
I'd suggest you use the AsyncTaskLoader which for my taste is easier than the AsyncTask.
Have a look here, the example is very extensive and looks intimidating at first, you'll probably find much simpler ones. The idea is that your Activity implements LoaderCallbacks for the creation of the loader and a method that is being called when the loader has finished. You 'start' a loader via the LoaderManager.
The AsynctaskLoader is a class that extends AsyncTaskLoader and does the asynchronous stuff.
I'll give you a simple example:
This is the AsyncTaskLoader:
public class StartupLoader extends AsyncTaskLoader<Boolean> {
Context context;
public StartupLoader(Context context) {
super(context);
this.context = context;
forceLoad();
}
#Override
public Boolean loadInBackground() {
// DO STUFF!
return true;
}
#Override
protected void onStopLoading() {
}
#Override
public void onCanceled(Boolean data) {
super.onCanceled(data);
}
#Override
protected void onReset() {
super.onReset();
}
}
This is what you have in the Activity that will start the loader, it is an inner class:
public class StartupCallback implements
LoaderManager.LoaderCallbacks<Boolean> {
#Override
public void onLoadFinished(Loader<Boolean> loader, Boolean succ) {
// Here you get your results back
}
#Override
public Loader<Boolean> onCreateLoader(int id, Bundle args) {
return new StartupLoader(getApplicationContext());
}
#Override
public void onLoaderReset(Loader<Boolean> loader) {
}
}
And this is how you start the loader from whereever you want (within that Activity):
StartupCallback startupCallback = new StartupCallback();
getSupportLoaderManager().initLoader(0, null, startupCallback);
where 0 is an ID that you give the loader, null is a Bundle of arguments.
Good luck :)
i'm developing an android app. Now i would like to use an AsyncTask. It runs perfectly in android 4.1 but the doInBackground Method is not running in android 2.3.3. There are no errors in the logcat..
I have a class that extends vom AsyncTask. So i start it with new Class().execute(). The constructor always works.
Do you have any idea how i can solve the problem in android 2.3.3?
Edit:
My Code:
private class TopicList extends AsyncTask<String, Integer, String> {
private TopicList(){
Log.e("Constructor","Works");
}
#Override
protected String doInBackground(String... arg0) {
Log.e("InBackground","Works");
new Thread(new Runnable() {
public void run() {
// Network processes
}
}).start();
return null;
}
}
I have target version 17 and minSdkVersion 7. Do you still need more information? I execute it by using this code:
new TopicList().execute();
The error log in logcat only shows that the constructor works.
AsyncTask API:
http://developer.android.com/reference/android/os/AsyncTask.html
After reading the documentation above, it's clear that AsyncTask should work fine in version 2.3.3 (it was added in API 3 = Android 1.5).
Are you sure the correct params are being passed? Here is a simple example of an Async Task which calls a WebService and shows the result in a TextView of the activity, I hope this helps you get in the right track:
private class AsyncTaskWebServiceCaller extends AsyncTask<Void, Void, String> {
private ProgressDialog dialog = new ProgressDialog(MainActivity.this);
#Override
protected void onPreExecute() {
dialog.setMessage("Connecting to Web Service...");
dialog.show();
}
#Override
protected String doInBackground(Void...voids) {
//create the envelope for the message
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = false;
//get the card (SOAPObject) that will be put into the envelope
SoapObject soapObject = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
envelope.setOutputSoapObject(soapObject);
//put something on the card
PropertyInfo propertyInfo = new PropertyInfo();
propertyInfo.setType(PropertyInfo.STRING_CLASS);
propertyInfo.setName("macAddress");
propertyInfo.setValue(macAddress);
soapObject.addProperty(propertyInfo);
//call WebService
try {
HttpTransportSE httpTransportSE = new HttpTransportSE(SOAP_ADDRESS);
httpTransportSE.debug = true;
httpTransportSE.call(SOAP_ACTION, envelope);
Object response = envelope.getResponse();
return response.toString();
} catch (Exception e) {
String text = "Oops!! App has crashed with exception: "+e.getMessage() + "\n" + "Stack trace below:\n";
StackTraceElement[] stackTrace = e.getStackTrace();
for (StackTraceElement stackTraceElement : stackTrace) {
text += stackTraceElement.toString() + "\n";
}
return text;
}
}
#Override
protected void onPostExecute(String result) {
if (dialog.isShowing()) {
dialog.dismiss();
}
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText(result);
}
}
And to call the AsyncTask, just do this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new AsyncTaskWebServiceCaller().execute();
}
I'm coding an app that primarily uses data gotten from a web service, and I want to use AsyncTask to run the SOAP calls in the background... I'm fairly new to Android(being an iOS programmer), so I'm a bit new at this...
Now, I have a login screen, where I take a user-provided login and check it against information on a server...
So in my login activity:
loginBtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//Run the connection to authenticate the user
AuthenticateConnection mAuth = new AuthenticateConnection();
mAuth.mNumber = number;
mAuth.mPassword = pass;
mAuth.connection();
}
}
and my soap class is this:
public class AuthenticateConnection
{
private static final String SOAP_ACTION = "http://tempuri.org/Authenticate";
private static final String METHOD_NAME = "Authenticate";
private static final String NAMESPACE = "http://tempuri.org/";
private String URL;
public Boolean userOK;
public String mNumber;
public String mPassword;
public AuthenticateConnection()
{
}
public void connection()
{
Singleton service = Singleton.getInstance();
String firstURL = service.getURL();
URL = firstURL + "Parent.svc";
System.out.println("Connection to: " + URL);
//Initialize soap request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//Add parameters
request.addProperty("login", mNumber);
request.addProperty("password", mPassword);
//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.implicitTypes=true;
envelope.setAddAdornments(false);
//Prepare request
envelope.setOutputSoapObject(request);
//Needed to make the internet call
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
//Allow for debugging - needed to output the request
androidHttpTransport.debug = true;
try
{
//this is the actual part that will call the web service
androidHttpTransport.call(SOAP_ACTION, envelope);
//Get the SoapResult from the envelope body.
//Object result = envelope.getResponse();
//Object result = envelope.bodyIn;
SoapObject sResult = (SoapObject)envelope.bodyIn;
String tempID = sResult.getProperty("AuthenticateResult").toString();
//Check if the user exists and has the correct password
if(tempID != "-1")
{
userOK = true;
//Store the values in the singleton class
service.parentID = sResult.getProperty("AuthenticateResult").toString();
service.parentToken = sResult.getProperty("token").toString();
}
//If -1 is returned, then either the number or the password is incorrect
else
{
userOK = false;
}
} catch(org.xmlpull.v1.XmlPullParserException ex2)
{
//System.out.println(androidHttpTransport.requestDump.toString());
} catch (Exception e)
{
e.printStackTrace();
System.out.println(androidHttpTransport.requestDump.toString());
}
}
}
So my question is, how would I do this with AsyncTask?
I've been looking at some tutorial on AsyncTask, but haven't really "gotten it" so far...
You can do:
private class ConnectionTask extends AsyncTask<String, Void, Void> {
private ProgressDialog dialog = new ProgressDialog(ACTIVITY_NAME.this);
protected void onPreExecute() {
dialog.setMessage("Connecting...");
dialog.show();
}
protected void doInBackground(String... args) {
AuthenticateConnection mAuth = new AuthenticateConnection();
mAuth.mNumber = args[0];
mAuth.mPassword = args[1];
mAuth.connection();
}
protected void onPostExecute(Void v) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
And then call it:
loginBtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//Run the connection to authenticate the user
new ConnectionTask().execute(number, pass);
}
}
Your connection method in AuthenticateConnection should return something to ensure the user has been authenticated. Then you can use that value in the onPostExecute, something like this:
protected void onPostExecute(Integer res) {
if (dialog.isShowing()) {
dialog.dismiss();
}
if (res.intValue() == OK) {
/* Maybe start a new Activity ...*/
} else {
/* Maybe show a Toast with an error message ...*/
}
}
In this case the signature of the asynctask will change:
private class ConnectionTask extends AsyncTask<String, Void, Integer>
and the doInBackground should return an Integer.
Hope it helps.