Can an Android AsyncTask doInBackground be synchronized to serialize the task execution?

前端 未结 4 598
滥情空心
滥情空心 2020-12-03 12:06

Is it possible to make AsyncTask.doInBackground synchronized - or achieve the same result in another way?



        
4条回答
  •  盖世英雄少女心
    2020-12-03 12:18

    public class RestAsyncTask1 extends AsyncTask {
    
        private AsyncTaskCompleteListener callback;
        private Context context;
        private String method;
        private static final AtomicInteger PROGRESS_NUM = new AtomicInteger(0);
        private static ProgressDialog PROGRESS_DIALOG;
    
        public RestAsyncTask1(Context context, AsyncTaskCompleteListener callback, String method) {
            this.callback = callback;
            this.context = context;
            this.method = method;
        }
    
        public static String format(String url, String... params) {
            String[] encoded = new String[params.length];
    
            for (int i = 0; i < params.length; i++) {
                encoded[i] = Uri.encode(params[i]);
            }
    
            return String.format(url, (String[]) encoded);
        }
    
        @Override
        protected void onPreExecute() {
            int x = PROGRESS_NUM.getAndIncrement();
    
            if (x == 0) {
                String title = "M_yug";
                PROGRESS_DIALOG = new ProgressDialog(context);
               // PROGRESS_DIALOG.setTitle(title);
                PROGRESS_DIALOG.setIndeterminate(true);
                PROGRESS_DIALOG.setCancelable(false);
                PROGRESS_DIALOG.setOnCancelListener(null);
                PROGRESS_DIALOG.setMessage("Loading. Please wait...");
                PROGRESS_DIALOG.show();
            }
        }
    
        @Override
        protected String doInBackground(String... params) {
            String url = params[0];
            String response = null;
            HttpURLConnection connection = null;
    
            if (params.length > 1) {
                if (method.equals(Method.GET)) {
                    url = format(url, (String[]) Arrays.copyOfRange(params, 1, params.length));
                } else if (params.length > 2) {
                    url = format(url, (String[]) Arrays.copyOfRange(params, 1, params.length - 1));
                }
    
                try {
                    URL call = new URL(url);
                    connection = (HttpURLConnection) call.openConnection();
                    connection.setRequestProperty("Content-Type", "application/json");
                    //connection.setRequestProperty("M-Yug", Utilities.VERSION);
                    connection.setRequestMethod(method);
                    connection.setDoOutput(true);
    
                    if (method.equals("POST")) {
                        BufferedOutputStream outputStream = new BufferedOutputStream(connection.getOutputStream());
                        outputStream.write(params[params.length - 1].getBytes());
                        outputStream.flush();
                    }
    
                    int status = connection.getResponseCode();
    
                    if (status == HttpURLConnection.HTTP_OK) {
                        InputStream is = connection.getInputStream();
                        response = readValue(is);
                    } else if (status == 400) {
                        InputStream is = connection.getErrorStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                        StringBuilder builder = new StringBuilder();
                        String line;
    
                        while ((line = reader.readLine()) != null) {
                            builder.append(line);
                        }
    
                        reader.close();
                        Toast.makeText(context, "" + builder.toString(), Toast.LENGTH_SHORT).show();
                    }
    
                    connection.disconnect();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (connection != null) {
                        connection.disconnect();
                    }
                }
            }
    
            return response;
        }
    
        @Override
        protected void onPostExecute(String s) {
            int x = PROGRESS_NUM.decrementAndGet();
    
            if (x == 0 && PROGRESS_DIALOG != null && PROGRESS_DIALOG.isShowing()) {
                PROGRESS_DIALOG.dismiss();
            }
    
            if (s!=null) {
                String resopnse=s.toString();
                callback.onSuccess(resopnse);
            } else {
               Toast.makeText(context,"Server Not Responding",Toast.LENGTH_SHORT).show();
            }
        }
    
        private String readValue(InputStream is) {
            BufferedReader br = null;
            StringBuilder sb = new StringBuilder();
            String line;
    
            try {
                br = new BufferedReader(new InputStreamReader(is));
    
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
            } catch (Exception e) {
            }
    
            return sb.toString();
        }
    
        enum Method {
            GET, POST
        }
    }
    

提交回复
热议问题