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.
hi i am able to get the output screen on the emulator still
ReplyDeleteHello World,AndroidInternetTxtActivity.
the output which is on the text.txt file is not being displayed. can any one tell what could have gone wrong?
hello deepi,
ReplyDeletecan 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.
hi i am not able to see the finished text. i am using android emulator.
ReplyDeletei am getting an message like this
"Unfortunately the AndroidInternetTxt has stopped.ok"
can you please tell why this is happening so?
have you specify uses-sdk android:minSdkVersion="4" in AndroidManifest.xml?
ReplyDeleteya i have specified this
ReplyDeletein the AndroidManifest.xml. but still i am having net connection not able to view the result from the emulator.
ya i am specified this
ReplyDeleteuses-sdk android:minSdkVersion="10" in the AndroidManifest.xml. but still i am having net connection not able to view the result from the emulator.
hello deepi,
ReplyDeleteI 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.
ya i am able to get it. thank you.
ReplyDeletebut 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?
ReplyDeletei 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 ?
ReplyDeleteYou have to generate the apk file, refer Generate signed APK for Android Application.
ReplyDeleteOn 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.
Hi,
ReplyDeleteI'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?
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.
ReplyDeleteI 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.
ReplyDeletehi ,
ReplyDeleteYou 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 ?
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?
ReplyDeleteThe output only have one line.... i need a line by line text can any one help me out... plz
ReplyDeleteI have tried the above code to run app on android mobile.....but it is giving java.io.FileNotFoundException (No Such file or directory).....while i have rechecked my file path...it is correct...
ReplyDeleteFirst,I want to ask to run this on mobile ....is it necessary to have internet connection on mobile....?I have tried to run this on android mobile bot i am getting "Finished" ,no actual text and java.io.FileNotFoundException
ReplyDeleteHi. Can you tell me how to read a zip file that contains a single file??
ReplyDelete