Load something from Internet using URLConnection and ReadableByteChannel, in Thread

Load something from Internet using URLConnection and ReadableByteChannel, in Thread - Hello Friend GENERAL INFO FOR ANDROID DEVELOPMENT, In the article you read this time with the title Load something from Internet using URLConnection and ReadableByteChannel, in Thread, We have prepared this article for you to read and retrieve information there in. Hopefully the contents of postings Artikel Android code sample: thread, Artikel Android Network Programming, We write this you can understand. Alright, good read.

The Title : Load something from Internet using URLConnection and ReadableByteChannel, in Thread
link : Load something from Internet using URLConnection and ReadableByteChannel, in Thread

Read also


Load something from Internet using URLConnection and ReadableByteChannel, in Thread

Last example show "Load something from Internet using URLConnection and BufferedReader, in Thread". Here is another example using java.nio.channels.ReadableByteChannel.

Read operations are synchronous on a ReadableByteChannel, that is, if a read is already in progress on the channel then subsequent reads will block until the first read completes. It is undefined whether non-read operations will 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;

public class 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 = new MyThread("http://android-er.blogspot.com", textResult);
myThread.start();
}

class MyThread extends Thread{
String target;
TextView textviewResult;

private Handler handler = new Handler();

public MyThread(String target, TextView textviewResult) {
super();
this.target = target;
this.textviewResult = textviewResult;
}

@Override
public void run() {
String result = "";

URL url = null;
try {
url = new URL(target);
URLConnection urlConnection = url.openConnection();
InputStream inputStream = urlConnection.getInputStream();

ReadableByteChannel readableByteChannel = Channels.newChannel(inputStream);
ByteBuffer buffer = ByteBuffer.allocate(1024);

while (readableByteChannel.read(buffer) > 0)
{
result += new String(buffer.array());
buffer.clear();
}

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


final String finalResult = result;
handler.post(new Runnable() {
@Override
public void run() {
textviewResult.setText(finalResult);
Toast.makeText(MainActivity.this,
"finished", Toast.LENGTH_LONG).show();
}
});
}
}
}


For layout and AndroidManifest.xml, refer to the previous post "Load something from Internet using URLConnection and BufferedReader, in AsyncTask".




Thus Article Load something from Internet using URLConnection and ReadableByteChannel, in Thread

The End Article Load something from Internet using URLConnection and ReadableByteChannel, in Thread This time, hopefully can give benefits to all of you. well, see you in posting other articles.

You are now reading the article Load something from Internet using URLConnection and ReadableByteChannel, in Thread with the link address http://xorislogo.blogspot.com/2016/03/load-something-from-internet-using.html

0 Response to "Load something from Internet using URLConnection and ReadableByteChannel, in Thread"

Post a Comment