Hello everyone, there is a code to display the contents of the file on the Internet. I need to display content from 2 files in toast. here is the full activation code

package com.example.byfile; import android.os.Bundle; import com.example.byfile.R; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.net.*; import java.io.*; import java.nio.charset.Charset; public class MainActivity extends Activity { Button btnSend; @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); final Button btnSend = (Button)this.findViewById(R.id.btnSend); btnSend.setOnClickListener(new View.OnClickListener() { public void onClick(final View v) { new Thread(new Runnable() { @Override public void run() { try { final URL myURL = new URL("http://мой-сайт/1.txt"); final URLConnection connection = myURL.openConnection(); connection.setDoInput(true); final Reader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8"))); final URL myURL2 = new URL("http://мой-сайт/2.txt"); final URLConnection connection2 = myURL2.openConnection(); connection2.setDoInput(true); final Reader reader2 = new BufferedReader(new InputStreamReader(connection2.getInputStream(), Charset.forName("UTF-8"))); try { //буфер 1 final char [] buffer1 = new char[1024]; final StringBuilder message = new StringBuilder(); int readCount; do { readCount = reader.read(buffer1); message.append(buffer1); } while (readCount >= buffer1.length); //буфер 2 final char [] buffer2 = new char[1024]; final StringBuilder message2 = new StringBuilder(); int readCount2; do { readCount2 = reader2.read(buffer2); message2.append(buffer2); } while (readCount2 >= buffer2.length); MainActivity.this.runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this.getApplicationContext(), message + message2.toString(), Toast.LENGTH_LONG).show(); } }); } catch (final IOException ex) { Log.d("IOException", ex.getMessage()); } finally { reader.close(); } } catch (final Exception ex) { Log.d("Some exception", ex.getMessage()); } } }).start(); } }); }} 
  • and what is the problem then? It is displayed only from one file, only from the second, from no one, does the program crash or what at all? - afiki am
  • in this problem it is better to use AsynkTask, which has normal access to the main thread before and after execution - andreich
  • Only the second file is output, but both are needed. can you write an example with AsynkTask? - Rammsteinik
  • The code works as it should (tested it with the files from the dropbox). maybe you have the first file in the wrong encoding? - afiki

1 answer 1

Maybe it's all about this:

 Toast.makeText(MainActivity.this.getApplicationContext(), message + message2.toString(), Toast.LENGTH_LONG).show(); 

And it should be replaced by:

 Toast.makeText(MainActivity.this.getApplicationContext(), message.toString() + message2.toString(), Toast.LENGTH_LONG).show();