Android中的Asynctask已被弃用,需要替代示例。

6

我刚接触Android Java,希望能够得到一些参考资料和/或示例,以便进行简单的网络调用,而无需使用AsyncTask。

我正在编写一个程序来解析来自URL的简单JSON对象。

在Android 11 (API 30)中,所有AsyncTask都将被弃用,如此处所示:

https://developer.android.com/reference/android/os/AsyncTask

2个回答

2

这是一个示例,演示如何使用线程发送请求而不使用AsyncTask。

  void send_request(final String url) {
    try {
        Thread thread = new Thread() {
            public void run() {
                Looper.prepare();
                final JSONObject[] maindata = {new JSONObject()};

                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        String data = "";
                        String error_data = "";

                        HttpURLConnection httpURLConnection = null;
                        try {

                            httpURLConnection = (HttpURLConnection) new URL(url).openConnection();
                            httpURLConnection.setRequestMethod("GET");
                            httpURLConnection.setRequestProperty("Content-Type", "application/json");






                            int status = httpURLConnection.getResponseCode();
                            Log.d("GET RX", " status=> " + status);

                            try {
                                InputStream in = httpURLConnection.getInputStream();
                                InputStreamReader inputStreamReader = new InputStreamReader(in);

                                int inputStreamData = inputStreamReader.read();
                                while (inputStreamData != -1) {
                                    char current = (char) inputStreamData;
                                    inputStreamData = inputStreamReader.read();
                                    data += current;
                                }
                                Log.d("GET RX =>", " " + data);

                                sdbw sd = new sdbw(act);
                                maindata[0] = new JSONObject(data);



                            } catch (Exception exx) {
                                InputStream error = httpURLConnection.getErrorStream();
                                InputStreamReader inputStreamReader2 = new InputStreamReader(error);

                                int inputStreamData2 = inputStreamReader2.read();
                                while (inputStreamData2 != -1) {
                                    char current = (char) inputStreamData2;
                                    inputStreamData2 = inputStreamReader2.read();
                                    error_data += current;
                                }
                                Log.e("TX", "error => " + error_data);

                            }


                        } catch (Exception e) {
                            Log.e("TX", " error => " + e.getMessage());
                            e.printStackTrace();
                        } finally {
                            if (httpURLConnection != null) {
                                httpURLConnection.disconnect();
                            }
                        }

                        handler.removeCallbacks(this);
                        Looper.myLooper().quit();
                    }
                }, 2000);

                Looper.loop();
            }
        };
        thread.start();

     } catch (Exception ex) {
        Log.e("ERROR =>", "" + ex.getMessage());
        ex.printStackTrace();
    }

}

2
似乎比仅使用AsyncTask要复杂得多。我有点困惑它的弃用如何能让我们的代码变得更好。 - drmrbrewer
请查看:https://www.techyourchance.com/asynctask-deprecated/ - Code Demon

1
这是关于房间查询的内容,很有效。
val x = Executors.newSingleThreadExecutor().submit(Callable { mDao.getX() }).get()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接