简体   繁体   中英

I want to convert AsyncTask to RxJava, but I don't know how to do it

The asynchronous operation is no longer available. So I want to use RxJava instead. However, no matter how much I search on Google, there is no way to convert it. Among the 'Mainactivitie' that I uploaded, if you press the button assigned to 'btn_user', the screen changes and the information stored in mysql is displayed through the php file written in 'target'. How can I change it? Or is there anything I can use other than RxJava? Do not use kotlin. If you need more files, I will provide them to you.

I'll mark the parts that need to be changed. //---- From //----

public class MainActivity extends AppCompatActivity {

    private TextView tv_id, tv_pass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_id = findViewById(R.id.tv_id);
        tv_pass = findViewById(R.id.tv_pass);

        Button btn_user = (Button) findViewById(R.id.btn_user);

        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        String pass = intent.getStringExtra("pass");

        tv_id.setText(id);
        tv_pass.setText(pass);

        if(!id.equals("admin"))
        {
            btn_user.setVisibility(View.GONE);
        }
        //----------------------------------------------------------------
        btn_user.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                new BackgroundTask().execute();
            }
        });
    }

    class BackgroundTask extends AsyncTask<Void, Void, String> {
        String target;

        @Override
        protected void onPreExecute() {
            target = "http://MyIP/phpFile.php";
        }

        @Override
        protected String doInBackground(Void... voids) {
            try {
                URL url = new URL(target);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String temp;
                StringBuilder stringBuilder = new StringBuilder();
                while ((temp = bufferedReader.readLine()) != null) {
                    stringBuilder.append(temp + "\n");
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return stringBuilder.toString().trim();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

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

        @Override
        protected void onPostExecute(String result) {
            Intent intent = new Intent(MainActivity.this, ManagementActivity.class);
            intent.putExtra("userList", result);
            MainActivity.this.startActivity(intent);
        }
    }
//----------------------------------------------------------
}

You wouldn't. You'd convert the AsyncTask to a thread or kotlin coroutine. Then you might use RxJava to update the UI from the background thread.

Although really- look into RetroFit. Rolling your own HTTP requests is generally not something you do these days. Retrofit will get it done more easily and do most of what you need under the hood for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM