开发者

Show me a way to connect webservices asynchronously on Android

开发者 https://www.devze.com 2023-04-11 17:35 出处:网络
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/Netwo
  1. I do not use ksoap2
  2. 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
  3. 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.
  4. In code block I will call different webmethods at least 5 or 6 times.
  5. the code structure goes like this

    public void X(){
        int a = webMethodA();
        . doSomethingWith a
        .
        .
        b = webMethodB(a);
        .
        . doSomethingWith b
        .
        .
        c = webMethod(b);
        .
        . 
        .
    }
    
  6. 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) {

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号