How to read a .txt document line by line in android? - android

In this code, only the last word entered in the .txt file is being read. I am calling the same activity when a condition is met. What changes should I make to the code so that it reads a word, calls the same activity again and then reads a new word?
public class Page extends Activity {
Button b;
TextView t;
EditText e;
int i = 0;
String str2;
String w, x=null;
static BufferedReader c = null;
static BufferedReader ic = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
b = (Button) findViewById(R.id.benter);
t = (TextView) findViewById(R.id.tv);
e = (EditText) findViewById(R.id.et);
InputStream f = getResources().openRawResource(R.raw.incorrect);
ic = new BufferedReader(new InputStreamReader(f));
try {
while ((w = ic.readLine()) != null) {
t.setText(w);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
str2 = e.getText().toString();
if (str2.equals(x)) {
Toast.makeText(getApplication(), "RIGHT", Toast.LENGTH_LONG).show();
Intent open = new Intent(Page.this, Page.class);
startActivity(open);
} else {
Toast.makeText(getApplication(), "WRONG", Toast.LENGTH_LONG).show();
e.setText("");
}
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

You can read file line by line using this code
try {
FileReader fileReader = new FileReader("FilePath");
BufferedReader reader = new BufferedReader(fileReader);
String data = null;
StringBuilder builder = new StringBuilder();
while ((data = reader.readLine()) != null) {
builder.append(data);
}
System.out.println(builder.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
In your code there is problem in your onclick listener .. please put onclick listener on out of while loop..

There is minot mistake in your onClickListener() :
public class Page extends Activity {
Button b;
TextView t;
EditText e;
int i = 0;
String str2;
String w, x=null;
static BufferedReader c = null;
static BufferedReader ic = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
b = (Button) findViewById(R.id.benter);
t = (TextView) findViewById(R.id.tv);
e = (EditText) findViewById(R.id.et);
InputStream f = getResources().openRawResource(R.raw.incorrect);
ic = new BufferedReader(new InputStreamReader(f));
try {
while ((w = ic.readLine()) != null) {
t.setText(w);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
str2 = e.getText().toString();
// it was str2.equals(x) nut x is null, replace w with x
if (str2.equals(w)) {
Toast.makeText(getApplication(), "RIGHT", Toast.LENGTH_LONG).show();
Intent open = new Intent(Page.this, Page.class);
startActivity(open);
} else {
Toast.makeText(getApplication(), "WRONG", Toast.LENGTH_LONG).show();
e.setText("");
}
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
This code will work for you

You can use Apache Commons I/O library for this.It's so simple. And keep your code beauty. And why do get .txt files from assets ?
AssetManager am = context.getAssets();
InputStream fs = am.open("a.txt");
List<String> lines = IOUtils.readLines(file,"UTF-8");

You can save your text file in "Assets" folder of project and use following code to retrieve that file in java class
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("YOUR_TEXT_FILE.txt")));
StringBuilder total = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
total.append(line);
}
message=total.toString();
System.out.println(message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
After that you have that file in String message

Your program is reading the last line because you are overwriting your 'w' variable every time that you are calling the readLine() method. What you should do is set your 'w' variable empty, and append it with readLine() to keep all the lines in your text file that have been read, then call setText() once to place everything in your View. Without appending, you are essentially overwriting the 'w' variable every time your while loop runs through, which is why you are receiving the last line from your text file.

Related

compiling cpp files on runtime Android Studio

I am making a compiling app with my custom made a language. This app will change the custom made language code automatically to a CPP code. So all I need to do is compile the CPP code on runtime.
I got stuck compiling a CPP file in my app.
I have already checked out using dexmaker and image-playground, but I had problems with the Gradle-build. They are somewhat old projects and I'm not really sure how to use it so I passed that one.
So I am trying to use the command line tool with the android studio. I have saved my CPP file and can use it I think. Below includes my file i/o code. This is my code.
Code:
public class Code_cpp extends AppCompatActivity {
private Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.code_cpp);
TextView textView = (TextView) findViewById(R.id.view);
textView.setMovementMethod(new ScrollingMovementMethod());
context = this;
}
public void savebutton(View v) {
final EditText edit = (EditText) findViewById(R.id.edit_text);
TextView textView = (TextView) findViewById(R.id.view);
Log.v("EditText", edit.getText().toString());
String messageString = edit.getText().toString();
writeToFile(messageString,context);
Toast.makeText(getApplicationContext(), "saved", Toast.LENGTH_LONG).show();
}
private void writeToFile(String data,Context context) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("config.cpp", Context.MODE_PRIVATE));
outputStreamWriter.write(data);
outputStreamWriter.close();
Log.e("Success", "File write Success ");
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
public void loadbutton(View v){
String msg = readFromFile(context);
TextView textView = (TextView) findViewById(R.id.view);
textView.setText(msg);
}
private String readFromFile(Context context) {
String ret = "";
try {
InputStream inputStream = context.openFileInput("config.cpp");
if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString).append("\n");
}
inputStream.close();
ret = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}
return ret;
}
public void run(View v){
try {
Process process = Runtime.getRuntime().exec("g++ config.cpp;./a.out");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
// Waits for the command to finish.
process.waitFor();
//return output.toString();
String result=output.toString();
TextView textView = (TextView) findViewById(R.id.view);
textView.setText(result);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
I get an error when I click the run button
Caused by: java.io.IOException: Cannot run program "g++": error=13, Permission denied
I don't want to root my phone because I don't want to use this app just on my phone but with other phones too when I download it. Do I have to try another way to compile this? Why do I get permission denied errors?

How to read a .txt file with a button (inside a condition) in android studio?

It could seem a simple question, but I can't find the answer everywhere. I know how to read a text file.. but I'd like to read it only if a button is pressed under a if-statament.. this is the code where I come from...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView textview1 = (TextView)findViewById(R.id.textviewone);
Bundle extradata1 = getIntent().getExtras();
String textString = extradata1.getString("text");
if (textString.equals("firsttext"))
{
String texttxt = "";
StringBuffer sbuffer = new StringBuffer();
InputStream is = this.getResources().openRawResource(R.raw.file);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
while ((texttxt = reader.readLine()) !=null){
sbuffer.append(texttxt + "n");
}
textview1.setText(sbuffer);
is.close();
}catch(Exception e) {
e.printStackTrace();
}
}
Now.. this obviously reads it without the button.. what if I want to read the file only if a button is pressed? I'm not really sure of where to put the intent.
Implement this:
Button button = (Button) findViewById(R.id.your_button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (textString.equals("firsttext")) {
String texttxt = "";
StringBuffer sbuffer = new StringBuffer();
InputStream is = this.getResources().openRawResource(R.raw.file);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
while ((texttxt = reader.readLine()) != null) {
sbuffer.append(texttxt + "n");
}
textview1.setText(sbuffer);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
Check this also.
Please Try below code
public class DemoActivity extends AppCompatActivity {
Button btn;
TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
/**
* get your bundle text value */
Bundle extradata1 = getIntent().getExtras();
String textString = extradata1.getString("text");
btn = (Button) findViewById(R.id.btn); // Your Button
txt = (TextView) findViewById(R.id.textView); //Your Textview
//OnClick handle of button
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (textString.equals("firsttext")) {
String texttxt = "";
StringBuffer sbuffer = new StringBuffer();
InputStream is = this.getResources().openRawResource(R.raw.file);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
while ((texttxt = reader.readLine()) != null) {
sbuffer.append(texttxt + "n");
}
txt.setText(sbuffer);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
}
This is how you can achieve this.
Make textString as class varibale.
Add a Button in your layout file.
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read File"
android:textAllCaps="false"/>
Create a reference for this button in class file.
Button button = (Button)findViewById(R.id.button);
Add click listener for this button and implement onClickListener interface.
button.setOnClickListener(this);
Implement onClick() method.
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
if (textString.equals("firsttext")){
String texttxt = "";
StringBuffer sbuffer = new StringBuffer();
InputStream is = this.getResources().openRawResource(R.raw.file);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
while ((texttxt = reader.readLine()) !=null){
sbuffer.append(texttxt + "n");
}
textview1.setText(sbuffer);
is.close();
}catch(Exception e) {
e.printStackTrace();
}
}
break;
}
}

Android : Reading File

I am trying to get last 10 rows from file but not able to fetch.
i have two activities:
in the first, i want to write text from an EditText to a file.
in the second activity i try to read the stored data and write it to a textView
public class Date_Location extends Activity {
ImageView imageView;
EditText editTextDate, editTextLocation, editTextEdit;
private static final String TAG = Date_Location.class.getName();
private static final String FILENAME = "myFile.txt";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.date_location);
editTextDate = (EditText) findViewById(R.id.editText1);
editTextLocation = (EditText) findViewById(R.id.editText2);
editTextEdit = (EditText) findViewById(R.id.editText3);
imageView = (ImageView) findViewById(R.id.next);
}
public void goNext(View view) {
String Date = editTextDate.getText().toString();
String Location = editTextLocation.getText().toString();
String Comment = editTextEdit.getText().toString();
writeToFile(Date);
writeToFile(Location);
writeToFile(Comment);
Intent intent = new Intent(this, Detail_Data.class);
startActivity(intent);
Date_Location.this.finish();
}
private void writeToFile(String data) {
String newline = "\r\n";
try {
OutputStreamWriter oswName = new OutputStreamWriter(openFileOutput(
FILENAME, Context.MODE_APPEND));
oswName.write(newline);
oswName.write(data);
oswName.close();
} catch (IOException e) {
Log.e(TAG, "File write failed: " + e.toString());
}
}
}
And my second Activity is below
public class Detail_Data extends Activity {
TextView textView1;
ImageView imageView;
private static final String FILENAME = "myFile.txt";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_data);
textView1 = (TextView) findViewById(R.id.textView1);
imageView = (ImageView) findViewById(R.id.imageView2);
String date = readFromFile();
textView1.setText(date);
}
private String readFromFile() {
String ret = "";
try {
InputStream inputStream = openFileInput(FILENAME);
ArrayList<String> bandWidth = new ArrayList<String>();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream);
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString+'\n');
bandWidth.add(receiveString);
if (bandWidth.size() == 10)
bandWidth.remove(0);
}
ret = stringBuilder.toString();
inputStream.close();
}
} catch (FileNotFoundException e) {
Log.i("File not found", e.toString());
} catch (IOException e) {
Log.i("Can not read file:", e.toString());
}
return ret;
}
public void goNext(View view) {
imageView.setColorFilter(0xFFFF3D60, PorterDuff.Mode.MULTIPLY);
Intent intent = new Intent(this, Agreement.class);
startActivity(intent);
Detail_Data.this.finish();
}
}
please if any one have any idea then help me. I have tried with other solution too but then also i am not getting last 10 records. Instead of last 10 data i am getting all the records which is written in file.
Firstly, If you are writing file on SDcard, be sure that you have added the uses-permission tag in AndroidManifest.xml
<uses-permission Android:name="Android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Secondly, don't forget flush()
oswName.write(data);
oswName.flush();
oswName.close();
Then, there is something wrong with your readFromFile() method,
remove this line from while loop
stringBuilder.append(receiveString+'\n');
and add this right after the while loop
for(String str : bandWidth)
stringBuilder.append(str + "\n");
readFromFile() should be like following
private String readFromFile() {
String ret = "";
try {
InputStream inputStream = openFileInput(FILENAME);
ArrayList<String> bandWidth = new ArrayList<String>();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream);
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((receiveString = bufferedReader.readLine()) != null) {
bandWidth.add(receiveString);
if (bandWidth.size() == 10)
bandWidth.remove(0);
}
for(String str : bandWidth)
stringBuilder.append(str + "\n");
ret = stringBuilder.toString();
inputStream.close();
}
} catch (FileNotFoundException e) {
Log.i("File not found", e.toString());
} catch (IOException e) {
Log.i("Can not read file:", e.toString());
}
return ret;
}
After opening the input/output stream, use that methods:
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Files {
public static String readStringFile(FileInputStream fis) throws java.io.IOException {
StringBuffer fileContent = new StringBuffer("");
byte[] buffer = new byte[1024];
while (fis.read(buffer) != -1) {
fileContent.append(new String(buffer));
}
return fileContent.toString();
}
public static void writeStringFile(FileOutputStream fos, String text) throws java.io.IOException {
fos.write(text.getBytes());
}
}
First, create the FileInputStream or FileOutputStream with your desired file name and then call the methods above. Please notice that the methods only work for reading and writing strings.
You store each line to list and remove 0 position only if list size = 10
So as 1st step store all file in list:
Instead
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString+'\n');
bandWidth.add(receiveString);
if (bandWidth.size() == 10)
bandWidth.remove(0);
}
Write
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString+'\n');
bandWidth.add(receiveString);
}
After copy last 10 lines to new list.
For example if you have :
List<String> bandWidth = new ArrayList<String>(Arrays.asList("a1", "a2", "a3","a4", "a5", "a6","a7", "a8", "a9","a10", "a11", "a12"));
Than with subList:
List<String> bandWidth10rows= bandWidth.subList(bandWidth.size()-10, bandWidth.size());
It will copy last 10 list items to new list.
Totally it should be something like:
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString+'\n');
bandWidth.add(receiveString);
}
List<String> bandWidthLastTenRows= bandWidth.subList(bandWidth.size()-10, bandWidth.size());

How to read next line from a text file

I want to read several lines from a text file. These line should be displayed one by one, when I click the next button. I am able to store the strings in a file and read the first line. But when I try to read the next line using the next button, it stops working. Can anybody tell me a solution for this. Thanks in advance.
I have defined the following,
BufferedReader buffreader;
String line;
String fav;
StringBuilder text;
InputStream instream;
String favQuote0;
This is in my oncreate method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_save);
StringBuilder text = new StringBuilder();
try {
// open the file for reading
InputStream instream = openFileInput("myfilename.txt");
// if file the available for reading
if (instream != null) {
// prepare the file for reading
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
while (( line = buffreader.readLine()) != null) {
text.append(line);
text.append('\n');
fav = text.toString();
}
}
} catch (IOException e) {}
TextView tv1 = (TextView)findViewById(R.id.textView2);
tv1.setText(fav);
}
This is my next button
public void next (View view1) {
try {
// open the file for reading
InputStream instream = openFileInput("myfilename.txt");
// if file the available for reading
if (instream != null) {
while (( line = buffreader.readLine()) != null) {
text.append(line);
text.append('\n');
fav = text.toString();
}
}
} catch (IOException e) {}
TextView tv1 = (TextView)findViewById(R.id.textView2);
tv1.setText(fav);
}
Both answers do it the wrong way. I will post a description below my code. This is the proper way of doing it.
private int mCurrentLine;
private ArrayList<String> mLines;
private TextView mTextView;
private Button mButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_save);
mCurrentLine = -1;
mLines = new ArrayList<String>();
mTextView = (TextView) findViewById(R.id.text_view);
mButton = (Button) findViewById(R.id.button);
try {
readLinesAndSaveToArrayList();
}
catch (IOException e) {
e.printStackTrace();
}
setTextAndIncrement();
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
setTextAndIncrement();
}
});
}
private void readLinesAndSaveToArrayList() throws IOException {
File file = new File("myfilename.txt");
if (!file.exists()) {
throw new FileNotFoundException("myfilename.txt was unable to locate");
}
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
mLines.add(line);
}
bufferedReader.close();
}
private void setTextAndIncrement() {
if (mCurrentLine == mLines.size() - 1) {
return;
}
mCurrentLine++;
mTextView.setText(mLines.get(mCurrentLine));
}
What I do is cache the file's contents in a scalable ArrayList. Each line will be assigned to an index in the array, eg. 0, 1, 2 and so on.
mCurrentLine takes care of positioning in the array. It starts at -1 because there is no current line. In the setTextAndIncrement() it will be set to 0, which in the array is the first index (the first line). The continuous calls will increment the position and take the next lines. If you come to the limit I have put in a check that looks if the mCurrentLine is equal to the max size of lines (mLines.size() - 1, a - 1 because arrays starts from 0 instead of 1).
Other than that I would advice you to use full length names on methods and variables; there is no need to keep them short, it will only make reading them harder. XML should also only contain low letters instead of camelCases.
Try this. This i working for me
BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
StringBuffer bf = new StringBuffer();
String json = reader.readLine();
try {
do {
bf.append(json);
json = reader.readLine();
}while (json != null);
wt.flush();
wt.close();
Log.d("LOG", " read line output "+new String(bf)+" json "+json);
reader.close();
} catch (Exception e) {
e.printStackTrace(); }
wt is the bufferedwriter which i used to write line. I read from reader and write into file stored locally.
try {
// open the file for reading
InputStream instream = new FileInputStream("myfilename.txt");
// if file the available for reading
if (instream != null) {
// prepare the file for reading
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
// read every line of the file into the line-variable, on line at the time
while (buffreader.hasNext()) {
line = buffreader.readLine();
// do something with the line
}
}
} catch (Exception ex) {
// print stack trace.
} finally {
// close the file.
instream.close();
}

problem with display web content

I had add in this remember me function inside my project, and I need to display the search result in the webview, but now everytime I click on the search button, the search content can be remembered, but the web content can not be displayed.
public class InternalStorageDemo extends Activity {
EditText txt;
Button writeBtn;
Button readBtn;
TextView tv;
WebView wv;
String FILE_NAME = "mFile";
String FILE_CONTENT;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt = (EditText)findViewById(R.id.txt);
writeBtn = (Button)findViewById(R.id.writeBtn);
readBtn = (Button)findViewById(R.id.readBtn);
writeBtn.setOnClickListener(listener);
readBtn.setOnClickListener(listener);
}
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.writeBtn:
FILE_CONTENT = txt.getText().toString().equals("")?"null":txt.getText().toString();
try {
FileOutputStream fos = openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
fos.write(FILE_CONTENT.getBytes());
fos.close();
//Toast.makeText(InternalStorageDemo.this, "stored done", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.readBtn:
FileInputStream fis;
try {
fis = openFileInput(FILE_NAME);
byte[] input = new byte[fis.available()];
while(fis.read(input) != -1){}
txt.setText(new String(input));
try{
String str = txt.getText().toString();
URL url = new URL("http://epoly.tp.edu.sg/tpapp/isistt/TTServlet?txtModule=StudentSearch&txtAction=GetTT&txtSelStudentID=" + str);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
// Read the contents line by line (assume it is text),
// storing it all into one string
String content ="";
String line = reader.readLine();
while (line != null) {
//adding on to the string (+=)
//"\n" goes to a new line so that it has a break
content += line + "\n";
line = reader.readLine();
}
//close reader after reading contents
reader.close();
//using substring to get html contents from a specific tag
String myString = content.substring(content.indexOf("</script>"));
int start = myString.indexOf("</script>");
//if start less than 0, no contents from start tag found,
//nothing will be displayed in webview, error message will be logged
if (start < 0) {
Log.d(this.toString(), "Academic calendar start tag not found");
}
else {
int end = myString.indexOf("</body>", start);
if (end < 0) {
Log.d(this.toString(), "Academic calendar end tag not found");
} else {
//load only <newcollection> tag
myString = "<html><body>" + myString.substring(start, end) + "</body></html>";
}
}
//display contents that have been extracted to webview
WebView wv = (WebView)findViewById(R.id.wv);
wv.getSettings().setBuiltInZoomControls(true);
//set the webview contents' size
wv.setInitialScale(80);
//wv.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
//wv.loadData(myString, "text/html", "utf-8");
wv.loadDataWithBaseURL("http://epoly.tp.edu.sg/tpapp/isistt/TTServlet?txtModule=StudentSearch&txtAction=GetTT&txtSelStudentID="+str, myString, "text/html", "UTF-8", "about:blank");
}catch (IOException e) {
e.printStackTrace();
Log.d(this.toString(), "Error!");
}} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//Log.d(this.toString(), "Error!");
}break;
}
}
};
}

Categories

Resources