Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓改型:等待响应_Android_Asynchronous_Retrofit2_Android Webservice - Fatal编程技术网

Android 安卓改型:等待响应

Android 安卓改型:等待响应,android,asynchronous,retrofit2,android-webservice,Android,Asynchronous,Retrofit2,Android Webservice,我正在我的应用程序中实施改型2以调用web服务。我的代码如下: SignUp.java ConnectionDetector connectionDetector = new ConnectionDetector(SignUpActivity.this); if (connectionDetector.isConnectingToInternet()) { ArrayList<HashMap<String, String>> arrayListCountryDet

我正在我的应用程序中实施改型2以调用web服务。我的代码如下:

SignUp.java

ConnectionDetector connectionDetector = new ConnectionDetector(SignUpActivity.this);
if (connectionDetector.isConnectingToInternet()) {
    ArrayList<HashMap<String, String>> arrayListCountryDetails = new ArrayList<>();
    GetCountryList getCountryList = new GetCountryList();
    arrayListCountryDetails = getCountryList.CallWebServiceForCountryDetails(this);

    // The app should wait here till the above retrofit web service calling returns response


    CountryDetailsAdapter countryDetailsAdapter = new CountryDetailsAdapter(SignUpActivity.this, arrayListCountryDetails);
    spinnerCountryName.setAdapter(countryDetailsAdapter);
} else {
    String message = "No internet connection.";
    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle(getResources().getString(R.string.app_name));
    alertDialog.setMessage(message);
    alertDialog.setCancelable(false);
    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
            new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
                   dialog.dismiss();
               }
            });
    alertDialog.show();
}
public class GetCountryList {
    ProgressDialog dialog;
    APIService mAPIService;
    ArrayList<HashMap<String, String>> arrayListCountryDetails;

    public ArrayList<HashMap<String, String>> CallWebServiceForCountryDetails(final Context context) {
        dialog = new ProgressDialog(context);
        dialog.setMessage("Please wait...");
        dialog.setCancelable(false);
        dialog.show();

        arrayListCountryDetails = new ArrayList<>();

        mAPIService = ApiUtils.getAPIService();
        mAPIService.getCountryDetails().enqueue(new Callback<CountryDetailsResponseModel>() {
            @Override
            public void onResponse(Call<CountryDetailsResponseModel> call, Response<CountryDetailsResponseModel> response) {

                if (response.isSuccessful()) {
                    HashMap<String, String> cntDetails = new HashMap<>();
                    cntDetails.put("airLineID", "0");
                    cntDetails.put("airLineName", "Select Airline");
                    arrayListCountryDetails.add(cntDetails);

                    // Get response
                    try {
                        if (response.body().getStatus() == 200 && response.body().getMessage().equalsIgnoreCase("success")) {
                            for (int count = 0; count < response.body().getCountry().size(); count++) {
                                cntDetails = new HashMap<>();
                                String countryID = response.body().getCountry().get(count).getCountryId();
                                String countryName = response.body().getCountry().get(count).getCountryName();

                                cntDetails.put("countryID", countryID);
                                cntDetails.put("countryName", countryName);
                                arrayListCountryDetails.add(cntDetails);
                            }

                            // do UI work here
                            if (dialog.isShowing()) {
                                dialog.dismiss();
                            }
                        } else {
                            // do UI work here
                            if (dialog.isShowing()) {
                                dialog.dismiss();
                            }
                        }
                    } catch (Exception e) {
                        // do UI work here
                        if (dialog.isShowing()) {
                            dialog.dismiss();
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<AirLineDetailsResponseModel> call, Throwable t) {
                // do UI work here
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });

        return arrayListCountryDetails;
    }
}
我知道这是因为微调器初始化是在改装响应之前执行的。

请告诉我如何等待改装响应。在上面的代码中我需要做什么更改。由于这个问题,我无法前进。
提前感谢。

大致做如下操作。我只是在
AsyncTask
方法中放置了必要的代码部分。根据需要修改

if (connectionDetector.isConnectingToInternet()) {


        // The app should wait here till the above retrofit web service calling returns response

    AsyncTask task = new AsyncTask<Void, Void, List<Map<String, String>>>() {
        @Override
        protected String doInBackground(Void... params) {
            List<Map<String, String>> arrayListCountryDetails = new ArrayList<>();
            GetCountryList getCountryList = new GetCountryList();
            arrayListCountryDetails = getCountryList.CallWebServiceForCountryDetails(this);
            return arrayListCountryDetails;
        }

        @Override
        protected void onPostExecute(List<Map<String, String>> arrayListCountryDetails) {
            CountryDetailsAdapter countryDetailsAdapter = new CountryDetailsAdapter(SignUpActivity.this, arrayListCountryDetails);
            spinnerCountryName.setAdapter(countryDetailsAdapter);
        }

        @Override
        protected void onPreExecute() {}

        @Override
        protected void onProgressUpdate(Void... values) {}
    }
    task.execute();

}
if(connectionDetector.isConnectingToInternet()){
//应用程序应在此处等待,直到上述改装web服务调用返回响应
AsyncTask任务=新建AsyncTask(){
@凌驾
受保护字符串doInBackground(无效…参数){
List arrayListCountryDetails=新建ArrayList();
GetCountryList GetCountryList=新建GetCountryList();
arrayListCountryDetails=getCountryList.CallWebServiceCountryDetails(此);
返回arrayListCountryDetails;
}
@凌驾
受保护的void onPostExecute(列表arrayListCountryDetails){
CountryDetailsAdapter CountryDetailsAdapter=新的CountryDetailsAdapter(SignUpActivity.this,arrayListCountryDetails);
喷丝头CountryName.setAdapter(countryDetailsAdapter);
}
@凌驾
受保护的void onPreExecute(){}
@凌驾
受保护的void onProgressUpdate(void…值){}
}
task.execute();
}

还可以从您的
GetCountryList
中删除任何UI调用,因为这将在加载数据时在“后台”上运行传递微调器对象,并在加载完成后设置适配器

public class GetCountryList {
ProgressDialog dialog;
APIService mAPIService;


public void  CallWebServiceForCountryDetails(final Context context,final Spinner spinnerCountryName) {
    dialog = new ProgressDialog(context);
    dialog.setMessage("Please wait...");
    dialog.setCancelable(false);
    dialog.show();

   final ArrayList<HashMap<String, String>> arrayListCountryDetails = new ArrayList<>();

    mAPIService = ApiUtils.getAPIService();
    mAPIService.getCountryDetails().enqueue(new Callback<CountryDetailsResponseModel>() {
        @Override
        public void onResponse(Call<CountryDetailsResponseModel> call, Response<CountryDetailsResponseModel> response) {

            if (response.isSuccessful()) {
                HashMap<String, String> cntDetails = new HashMap<>();
                cntDetails.put("airLineID", "0");
                cntDetails.put("airLineName", "Select Airline");
                arrayListCountryDetails.add(cntDetails);

                // Get response
                try {
                    if (response.body().getStatus() == 200 && response.body().getMessage().equalsIgnoreCase("success")) {
                        for (int count = 0; count < response.body().getCountry().size(); count++) {
                            cntDetails = new HashMap<>();
                            String countryID = response.body().getCountry().get(count).getCountryId();
                            String countryName = response.body().getCountry().get(count).getCountryName();

                            cntDetails.put("countryID", countryID);
                            cntDetails.put("countryName", countryName);
                            arrayListCountryDetails.add(cntDetails);
                        }

                        // do UI work here
                        if (dialog.isShowing()) {
                            dialog.dismiss();
                        }
                  //set Adapter 

                  CountryDetailsAdapter countryDetailsAdapter = new CountryDetailsAdapter(context, arrayListCountryDetails);
                  spinnerCountryName.setAdapter(countryDetailsAdapter);
                    } else {
                        // do UI work here
                        if (dialog.isShowing()) {
                            dialog.dismiss();
                        }
                    }
                } catch (Exception e) {
                    // do UI work here
                    if (dialog.isShowing()) {
                        dialog.dismiss();
                    }
                }
            }
        }

        @Override
        public void onFailure(Call<AirLineDetailsResponseModel> call, Throwable t) {
            // do UI work here
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    });


}
公共类GetCountryList{
进程对话;
APIService和mAPIService;
public void callWebServiceCountryDetails(最终上下文上下文,最终微调器微调器CountryName){
dialog=新建进度对话框(上下文);
setMessage(“请稍候…”);
对话框。可设置可取消(false);
dialog.show();
最终ArrayList ArrayList CountryDetails=新ArrayList();
mAPIService=aputils.getAPIService();
mAPIService.getCountryDetails().enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
HashMap cntDetails=新建HashMap();
cntDetails.put(“airLineID”、“0”);
cntDetails.put(“airLineName”、“Select Airline”);
arrayListCountryDetails.add(cntDetails);
//得到回应
试一试{
if(response.body().getStatus()==200&&response.body().getMessage().equalsIgnoreCase(“成功”)){
对于(int count=0;count
在arrayListCountryDetails中有国家/地区列表后,简短而简单地使用AsyncTask并设置适配器在设置适配器之前检查列表大小>0
AsyncTask
是访问数据库、URL和其他“后台”时的正确方法为了不阻塞UI,请进行一些研究,得出您自己的结论@pleft-infranction-network-call已经在后台了,我认为使用AsyncTask不是一个好方法。
public class GetCountryList {
ProgressDialog dialog;
APIService mAPIService;


public void  CallWebServiceForCountryDetails(final Context context,final Spinner spinnerCountryName) {
    dialog = new ProgressDialog(context);
    dialog.setMessage("Please wait...");
    dialog.setCancelable(false);
    dialog.show();

   final ArrayList<HashMap<String, String>> arrayListCountryDetails = new ArrayList<>();

    mAPIService = ApiUtils.getAPIService();
    mAPIService.getCountryDetails().enqueue(new Callback<CountryDetailsResponseModel>() {
        @Override
        public void onResponse(Call<CountryDetailsResponseModel> call, Response<CountryDetailsResponseModel> response) {

            if (response.isSuccessful()) {
                HashMap<String, String> cntDetails = new HashMap<>();
                cntDetails.put("airLineID", "0");
                cntDetails.put("airLineName", "Select Airline");
                arrayListCountryDetails.add(cntDetails);

                // Get response
                try {
                    if (response.body().getStatus() == 200 && response.body().getMessage().equalsIgnoreCase("success")) {
                        for (int count = 0; count < response.body().getCountry().size(); count++) {
                            cntDetails = new HashMap<>();
                            String countryID = response.body().getCountry().get(count).getCountryId();
                            String countryName = response.body().getCountry().get(count).getCountryName();

                            cntDetails.put("countryID", countryID);
                            cntDetails.put("countryName", countryName);
                            arrayListCountryDetails.add(cntDetails);
                        }

                        // do UI work here
                        if (dialog.isShowing()) {
                            dialog.dismiss();
                        }
                  //set Adapter 

                  CountryDetailsAdapter countryDetailsAdapter = new CountryDetailsAdapter(context, arrayListCountryDetails);
                  spinnerCountryName.setAdapter(countryDetailsAdapter);
                    } else {
                        // do UI work here
                        if (dialog.isShowing()) {
                            dialog.dismiss();
                        }
                    }
                } catch (Exception e) {
                    // do UI work here
                    if (dialog.isShowing()) {
                        dialog.dismiss();
                    }
                }
            }
        }

        @Override
        public void onFailure(Call<AirLineDetailsResponseModel> call, Throwable t) {
            // do UI work here
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    });


}