Load Something From Network Using Urlconnection In Addition To Readablebytechannel, Inward Thread

Posted on

Last illustration demo “Load something from Internet using URLConnection as well as BufferedReader, inward Thread“. Here is simply about other illustration using java.nio.channels.ReadableByteChannel.

Read operations are synchronous on a ReadableByteChannel, that is, if a read is already inward progress on the channel thus subsequent reads volition block until the outset read completes. It is undefined whether non-read operations volition block.

MainActivity.java

package com.blogspot.android_er.androidinternet;  import android.os.Bundle; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; import android.widget.Toast;  import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel;  populace shape MainActivity extends AppCompatActivity {      TextView textResult;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         textResult = (TextView)findViewById(R.id.tresult);          MyThread myThread = novel MyThread("https://rootandroid.org/", textResult);         myThread.start();     }      shape MyThread extends Thread{         String target;         TextView textviewResult;          somebody Handler handler = novel Handler();          populace MyThread(String target, TextView textviewResult) {             super();             this.target = target;             this.textviewResult = textviewResult;         }          @Override         populace void run() {             String upshot = "";              URL url = null;             try {                 url = novel URL(target);                 URLConnection urlConnection = url.openConnection();                 InputStream inputStream = urlConnection.getInputStream();                  ReadableByteChannel readableByteChannel = Channels.newChannel(inputStream);                 ByteBuffer buffer = ByteBuffer.allocate(1024);                  spell (readableByteChannel.read(buffer) > 0)                 {                     upshot += novel String(buffer.array());                     buffer.clear();                 }              } grab (MalformedURLException e) {                 e.printStackTrace();             } grab (IOException e) {                 e.printStackTrace();             }              concluding String finalResult = result;             handler.post(new Runnable() {                 @Override                 populace void run() {                     textviewResult.setText(finalResult);                     Toast.makeText(MainActivity.this,                             "finished", Toast.LENGTH_LONG).show();                 }             });         }     } }  

For layout as well as AndroidManifest.xml, mention to the previous post service “Load something from Internet using URLConnection as well as BufferedReader, inward AsyncTask“.