BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str
while ((str =in.readLine()) != null)
{
items = str.split("\n");
}
in.close();
String (str) contains data from a text file like:
January
February
March
etc.
Each word is on ag new line. I want to read the string and separate each word on a new line and store into an array of String objects (this would be the variable named 'items').
Actually, BufferedReader.readLine
already splits the input based on newlines.
So, where you currently have:
items=str.split("\n");
you only need to append str
to your array.
For example, with the infile
file holding:
January
February
March
April
May
June
the following program outputs 6
(the size of the array list created):
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
class Test {
public static void main (String[] args) {
try {
ArrayList<String> itms = new ArrayList<String> ();
BufferedReader br = new BufferedReader (new FileReader ("infile"));
String str;
while ((str = br.readLine()) != null)
itms.add(str);
br.close();
System.out.println (itms.size());
} catch (Exception e) {
System.out.println ("Exception: " + e);
}
}
}
The readLine
method already reads line-by-line. There will be no \n
characters in this string.
Try this instead:
ArrayList<String> itemList = new ArrayList<String>();
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
itemList.add(str);
}
in.close();
this my code and its work for me
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
ArrayList<String> itemList = new ArrayList<String>();
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content, "iso-8859-1"));
StringBuilder sb = new StringBuilder();
String s = null;
while ((s = buffer.readLine()) != null) {
itemList.add(s);
}
response = itemList.get(0);
content.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
@Override
protected void onPostExecute(String result) {
textView.setText(Html.fromHtml(result));
}
}
public void readWebpage(View view) {
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://www.yourURL.com" });
}
readWebpage function is onclick function I create in my button xml like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/readWebpage" android:onClick="readWebpage" android:text="Load Webpage"></Button>
<TextView android:id="@+id/TextView01" android:layout_width="match_parent" android:layout_height="match_parent" android:text="Example Text"></TextView>
in my code I try to get the first line so I use itemList.get(0) if you want to get another line just change the index like itemList.get(1) or itemList.get(n)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With