调用使用异步任务的android web服务?任务、android、web

2023-09-06 00:07:04 作者:凉生初雨

我在哪,我传递这是需要提出请求并得到响应数据的响应方法,URL和数组列表我单独的Web服务类。我喜欢这个我登录活动调用这个Web服务

I have my separate web service class in which i just pass response method, url and array list of data which is required for making the request and getting the response. I call this web service in my login activity like this

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

在此登录数据是包含一些数据的数组列表。现在,我想用异步任务调用后台此Web服务。但我只是没有得到它正确。我的Web服务的逻辑是用完全不同的Java文件和它工作正常,但我想打电话给我的内部异步任务的Web服务方法。在此输入code

In this login data is a array list which contains some data. Now i want to call this web service in background using async task. But i am just not getting it correctly. My web service logic is written in totally different java file and its working fine but i want to call my web service methods inside async task.enter code here

推荐答案

您可以试试下面$ C $下异步任务和还调用Web服务的 doInBackground

You can try below code for Async Task and also call web service in 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 background thread so don't update UI frome here
        //do your long running http tasks here,you dont 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();
    }

    }
}

完成