在android中使用异步任务调用web服务?

在android中使用异步任务调用web服务?,android,web-services,android-asynctask,Android,Web Services,Android Asynctask,我有一个单独的web服务类,在这个类中,我只传递响应方法、url和数据数组列表,这些数据是发出请求和获得响应所必需的。我在我的登录活动中调用此web服务,如下所示 JifWebService webServices = new JifWebService(); webServices.Execute(RequestMethod.POST, Jifconstant.LOGIN_URL, null, logindata)

我有一个单独的web服务类,在这个类中,我只传递响应方法、url和数据数组列表,这些数据是发出请求和获得响应所必需的。我在我的登录活动中调用此web服务,如下所示

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

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

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

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();
    }

    }
}
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.os.AsyncTask;
导入android.os.Bundle;
公共类异步示例扩展活动{
专用字符串url=”http://www.google.co.in";
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
新建AsyncCaller().execute();
}
私有类AsyncCaller扩展了AsyncTask
{
ProgressDialog pdLoading=新建ProgressDialog(AsyncExample.this);
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//此方法将在UI线程上运行
setMessage(“正在加载…”);
pdLoading.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
//此方法将在后台线程上运行,因此不要从此处更新UI
//在这里执行长期运行的http任务,您不想传递参数,您可以在这里访问父类的变量url
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
//此方法将在UI线程上运行
pdLoading.disclose();
}
}
}

Done

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

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();
    }

    }
}
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.os.AsyncTask;
导入android.os.Bundle;
公共类异步示例扩展活动{
专用字符串url=”http://www.google.co.in";
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
新建AsyncCaller().execute();
}
私有类AsyncCaller扩展了AsyncTask
{
ProgressDialog pdLoading=新建ProgressDialog(AsyncExample.this);
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//此方法将在UI线程上运行
setMessage(“正在加载…”);
pdLoading.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
//此方法将在后台线程上运行,因此不要从此处更新UI
//在这里执行长期运行的http任务,您不想传递参数,您可以在这里访问父类的变量url
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
//此方法将在UI线程上运行
pdLoading.disclose();
}
}
}
完成