By using AsyncTask (recommended)
import androidx.appcompat.app.AppCompatActivity; import android.os.AsyncTask; import android.os.Bundle; import android.widget.TextView; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; public class MainActivity extends AppCompatActivity { TextView textLoad, textMessage; final String strMessage = “https://sites.google.com/site/androidersite/text.txt”; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textLoad = findViewById(R.id.textLoad); textMessage = findViewById(R.id.textMessage); textLoad.setText(“Loading…”); new MyTask().execute(); } private class MyTask extends AsyncTask{ String result; @Override protected Void doInBackground(Void… voids) { URL url; try { url = new URL(strMessage); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream())); String stringBuffer; String string = “”; while ((stringBuffer = bufferedReader.readLine()) != null){ string = String.format(“%s%s”, string, stringBuffer); } bufferedReader.close(); result = string; } catch (IOException e){ e.printStackTrace(); result = e.toString(); } return null; } @Override protected void onPostExecute(Void aVoid) { textMessage.setText(result); textLoad.setText(“Finished”); super.onPostExecute(aVoid); } } }Disable Strict Mode (Not recommended)
if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); }
It’s puzzling to see the suggestion of disabling Strict Mode in the code. A questionable decision indeed.
Seems like a comical contradiction to recommend something while also stating it’s not recommended.
I agree, prioritizing security should always be non-negotiable in app development.
The code’s reliance on AsyncTask and proper error handling sets a good example for other developers.
Absolutely, it’s important to set a positive example in the developer community.
Disabling Strict Mode is not recommended due to potential security and performance issues.
Ironic how the code suggests disabling Strict Mode while mentioning it’s not recommended.
Agreed, it’s important to prioritize security and performance in Android development.
The use of AsyncTask and the implementation of UI updates in onPostExecute showcases good coding practices.
It’s refreshing to see clean and organized code that prioritizes user experience.
Indeed, maintaining a high coding standard is essential in software development.
The use of AsyncTask in this code demonstrates an understanding of asynchronous operations in Android.
This code is very well-structured and easy to understand. Great job!
I agree, the use of AsyncTask for network operations is a good practice.
I’m not convinced by the decision to disable Strict Mode. Security should never be compromised.
I share your concerns. Security should always be a top priority in app development.
The code demonstrates proper error handling and provides informative messages to the user.
Absolutely, users should always be informed about any issues that arise.
Error handling is crucial in any application to ensure a good user experience.
The use of AsyncTask in this code provides a clear separation of UI and background tasks. Well done!
Yes, it follows the best practices for handling background tasks in Android development.
While AsyncTask may be recommended, it’s important to consider the potential memory leaks it can cause.
True, AsyncTask can lead to memory leaks if not used carefully. Developers should be aware of this.