【问题标题】:Calling web service using Async task in android?在android中使用异步任务调用Web服务?
【发布时间】:2014-04-22 09:49:37
【问题描述】:

我有一个单独的 Web 服务类,在其中我只传递发出请求和获取响应所需的响应方法、url 和数据数组列表。我在我的登录活动中像这样调用这个网络服务

JifWebService webServices = new JifWebService();
                webServices.Execute(RequestMethod.POST,
                        Jifconstant.LOGIN_URL, null, logindata);
                loginResponse = webServices.getResponse();
                loginResponseCode = webServices.getResponseCode();

在这个登录数据中是一个包含一些数据的数组列表。现在我想使用异步任务在后台调用这个 Web 服务。但我只是没有正确理解它。我的 web 服务逻辑是用完全不同的 java 文件编写的,它工作正常,但我想在异步任务中调用我的 web 服务方法。enter code here

【问题讨论】:

    标签: android web-services android-asynctask


    【解决方案1】:

    您可以尝试下面的异步任务代码,也可以在 doInBackground 中调用网络服务:

    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.AsyncTask;
    import android.os.Bundle;
    
    public class AsyncExample extends Activity{
    
    
    private String url="http://www.google.co.in";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         new AsyncCaller().execute();
    }
    
    
    private class AsyncCaller extends AsyncTask<Void, Void, Void>
    {
        ProgressDialog pdLoading = new ProgressDialog(AsyncExample.this);
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
    
            //this method will be running on UI thread
            pdLoading.setMessage("Loading...");
            pdLoading.show();
        }
        @Override
        protected Void doInBackground(Void... params) {
    
            //this method will be running on a background thread so don't update UI from here
            //do your long-running http tasks here, you don't want to pass argument and u can access the parent class' variable url over here
    
    
            return null;
        }
    
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
    
            //this method will be running on UI thread
    
            pdLoading.dismiss();
        }
    
        }
    }
    

    完成

    【讨论】:

      猜你喜欢
      • 2013-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2011-09-03
      相关资源
      最近更新 更多