Monday, April 18, 2011

Read Text file from internet, using Java code

This exercise show how to read text file from internet. The file's url is hard coded to be "http://sites.google.com/site/androidersite/text.txt".

Read Text file from internet, using Java code

package com.exercise.AndroidInternetTxt;

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

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidInternetTxt extends Activity {

TextView textMsg, textPrompt;
final String textSource = "http://sites.google.com/site/androidersite/text.txt";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textPrompt = (TextView)findViewById(R.id.textprompt);
textMsg = (TextView)findViewById(R.id.textmsg);

textPrompt.setText("Wait...");

URL textUrl;
try {
textUrl = new URL(textSource);
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(textUrl.openStream()));
String StringBuffer;
String stringText = "";
while ((StringBuffer = bufferReader.readLine()) != null) {
stringText += StringBuffer;
}
bufferReader.close();
textMsg.setText(stringText);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
textMsg.setText(e.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
textMsg.setText(e.toString());
}

textPrompt.setText("Finished!");

}
}


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/textprompt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textmsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Modify AndroidManifest.xml to grant permission of "android.permission.INTERNET".
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidInternetTxt"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidInternetTxt"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
</manifest>


Download the files.

18 comments:

deepi said...

hi i am able to get the output screen on the emulator still
Hello World,AndroidInternetTxtActivity.

the output which is on the text.txt file is not being displayed. can any one tell what could have gone wrong?

Android Er said...

hello deepi,

can you see the text "Finished!"? and some error message?

If not, means the app is still running. It's suggested to test on real device.

I tried on real device, even disable INTERNET, mis-spell textSource, it will display error message.

deepi said...

hi i am not able to see the finished text. i am using android emulator.
i am getting an message like this

"Unfortunately the AndroidInternetTxt has stopped.ok"

can you please tell why this is happening so?

Android Er said...

have you specify uses-sdk android:minSdkVersion="4" in AndroidManifest.xml?

deepi said...

ya i have specified this
in the AndroidManifest.xml. but still i am having net connection not able to view the result from the emulator.

deepi said...

ya i am specified this
uses-sdk android:minSdkVersion="10" in the AndroidManifest.xml. but still i am having net connection not able to view the result from the emulator.

Android Er said...

hello deepi,

please try it in project form. Tested on emulator run 2.3.3, and Galaxy Nexus 4.0.2.

Android Er said...

hello deepi,

I don't know is it your case. I found that it have a problem in case of android:minSdkVersion="10", please refer to the post android.os.NetworkOnMainThreadException.

deepi said...

ya i am able to get it. thank you.

deepi said...

but i want the text file in an search box column introduced and when i type the letter that sentence should be displayed. is it possible to do with the text file inside? i have tried to introduce an array . but ti show's error. how to create an search box for this internet file and access the text?

deepi said...

i want to test this code on an real time android device/mobile. what is the procedure for this ? is it necessary to build an separate path or any procedure to test on mobile ?

Android Er said...

You have to generate the apk file, refer Generate signed APK for Android Application.

On your device, open Setting -> Security, check to enable Unknown sources.

Copy the APK file to your device's sdcard, click to install it.

-------------------------
Alternatively: If your development tools setup correctly, you can directly download, run, and debug on device in Eclipse.

Optimus Huerse said...

Hi,
I'm trying to read a text file from the internet with your method, but the
Scanner s = new Scanner(url.openStream()); or BufferedReader(...)keeps throwing an exception.
Do you have any idea why?

Zuber Vohra said...

I am trying to read data from google spreadsheet in text format.. I am getting the data on android app but everything is in one line how can I make it to show in different lines.

Zuber Vohra said...

I am trying to read data from google spreadsheet in text format.. I am getting the data on android app but everything is in one line how can I make it to show in different lines.

deepi said...

hi ,

You mean your telling you have plenty number of lines and you not able to view the lines after the screen. Am i right Zuber Vohra ?

Sandra said...

Hey, I tried to run this on an emulator, but all I'm getting is the word "Finished!", no actual text, what could be the problem?

Niya said...

The output only have one line.... i need a line by line text can any one help me out... plz