Show me a way to connect webservices asynchronously on Android
- I do not use ksoap2
- The callings must be asynchronously because other way honeycomb does not accept and it throws this exception http://developer.android开发者_运维技巧.com/reference/android/os/NetworkOnMainThreadException.html
- I am deriving the codes from .NET and Android has a very different architecturing than .NET. because of this when you leave comment please take notice of this fact.
- In code block I will call different webmethods at least 5 or 6 times.
the code structure goes like this
public void X(){ int a = webMethodA(); . doSomethingWith a . . b = webMethodB(a); . . doSomethingWith b . . c = webMethod(b); . . . }
I tried to make it using with asyncTask and Handler, I could take result value but the problem is I could not handle the result value on X method. I have to use return values in X method block
For .net datasets it is better to store your data in your self designed Object collections that can be same in Webservice and android. for example define class Person in webservice and Android which are same and deliver it.
you can use json in .Net and Android to serialize and deserialize your objects to a json string instead of using .Net xml.
for threading is this code your answer?
protected void btnCallWebservice_onClick() {
final Runnable r = new Runnable()
{
public void run()
{
threadWebservice();
}
};
performOnBackgroundThread(r);
}
public Thread performOnBackgroundThread(final Runnable runnable) {
final Thread t = new Thread() {
@Override
public void run() {
try {
runnable.run();
} finally {
}
}
};
t.start();
return t;
}
private void threadWebservice() {
try {
// call your webservice here
} catch (final Exception e) {
}
}
精彩评论