Tuesday, March 29, 2016

Load something from Internet using URLConnection and BufferedReader, in AsyncTask

Here is example to load something from Internet using URLConnection and BufferedReader, in AsyncTask.

MainActivity.java
package com.blogspot.android_er.androidinternet;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

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);

        MyAsyncTask myAsyncTask = new MyAsyncTask(textResult);
        myAsyncTask.execute("http://android-er.blogspot.com");
    }

    class MyAsyncTask extends AsyncTask<String, Void, String> {

        TextView textviewResult;

        public MyAsyncTask(TextView textviewResult) {
            super();
            this.textviewResult = textviewResult;
        }

        @Override
        protected String doInBackground(String... params) {
            String result = "";

            try {
                URL url = new URL(params[0]);
                URLConnection urlConnection = url.openConnection();
                InputStream inputStream = urlConnection.getInputStream();
                InputStreamReader inputStreamReader =
                        new InputStreamReader(inputStream);
                BufferedReader buffReader = new BufferedReader(inputStreamReader);

                String line;
                while ((line = buffReader.readLine()) != null) {
                    result += line;
                }

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

            return result;
        }

        @Override
        protected void onPostExecute(String s) {
            textviewResult.setText(s);

            Toast.makeText(MainActivity.this,
                    "DONE", Toast.LENGTH_LONG).show();
            super.onPostExecute(s);
        }
    }
}


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context="com.blogspot.android_er.androidinternet.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <TextView
        android:id="@+id/tresult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    </ScrollView>
</LinearLayout>


uses-permission of "android.permission.INTERNET" is needed in src/main/AndroidManifest.xml.


Add uses-permission of "android.permission.INTERNET" to AndroidManifest.xml

To access Internet in your app, you have to add uses-permission of "android.permission.INTERNET" to AndroidManifest.xml. Shown in this video:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.blogspot.android_er.androidinternet">

    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Related:
Load something from Internet using URLConnection and BufferedReader, in Thread

No comments: