I would like to connect to a Website, filter some content and then put it in a String but I don´t know how to do this.
public void zahlenLaden (View view) throws Exception {
URL oracle = new URL("http://www.blabla.de");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
//What I have to write here?
}
Declare a String to output to before the while loop:
String output = "";
Then just append to that String in each iteration:
output += inputLine + "\n"; (don't forget the omitted newline)
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine + "\n");
}
then just do sp.toString();
Nikola's answer is OK, just an improvement on the use of StringBuilder:
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine).append("\n");
}
Related
I want to get SDCard ID.
I am getting problem in Nougat(7.0) and above OS.
I already tried this,
private String getExternalSdCARDId() {
try {
String exsdcard_path = "/sys/block/mmcblk1";
File file = new File(exsdcard_path);
String memBlk = null;
if (file.exists() && file.isDirectory()) {
memBlk = "mmcblk1";
} else {
System.out.println("not a directory");
memBlk = "mmcblk0";
}
Process cmd = Runtime.getRuntime().exec("cat /sys/block/" + memBlk + "/device/cid");
BufferedReader br = new BufferedReader(new InputStreamReader(cmd.getInputStream()));
return br.readLine();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
But this method returns NULL.
try this :
String path2 = "/sys/block/mmcblk0/device";
BufferedReader serial;
BufferedReader name ;
BufferedReader manfid;
BufferedReader oemid;
BufferedReader mfDate;
BufferedReader CID;
serial = new BufferedReader(new FileReader(path2 + "/serial"));
name = new BufferedReader(new FileReader(path2 + "/name"));
manfid = new BufferedReader(new FileReader(path2 + "/manfid"));
oemid = new BufferedReader(new FileReader(path2 + "/oemid"));
mfDate = new BufferedReader(new FileReader(path2 + "/date"));
CID = new BufferedReader(new FileReader(path2 + "/cid"));
String sdSerial = serial.readLine();
String sdName = name.readLine();
String sdMfId = manfid.readLine();
String sdOemId = oemid.readLine();
String sdMfDate = mfDate.readLine();
String sdCid = CID.readLine();
or also use this
File input = new File("/sys/class/mmc_host/mmc1");
String cid_directory = null;
int i = 0;
File[] sid = input.listFiles();
for (i = 0; i < sid.length; i++) {
Log.d(TAG,"sid info "+sid[i]);
if (sid[i].toString().contains("mmc1:")) {
cid_directory = sid[i].toString();
String SID = (String) sid[i].toString().subSequence(cid_directory.length() - 4, cid_directory.length());
Log.d(TAG, " SID of MMC = " + SID);
break;
}
}
BufferedReader serial = new BufferedReader(new FileReader(cid_directory + "/serial"));
BufferedReader name = new BufferedReader(new FileReader(cid_directory + "/name"));
BufferedReader manfid = new BufferedReader(new FileReader(cid_directory + "/manfid"));
BufferedReader oemid = new BufferedReader(new FileReader(cid_directory + "/oemid"));
BufferedReader mfDate = new BufferedReader(new FileReader(cid_directory + "/date"));
BufferedReader CID = new BufferedReader(new FileReader(cid_directory + "/cid"));
String sdSerial = serial.readLine();
String sdName = name.readLine();
String sdMfId = manfid.readLine();
String sdOemId = oemid.readLine();
String sdMfDate = mfDate.readLine();
String sdCid = CID.readLine();
//make sure before using this code check sdcard is present or not;
Above 7.0:Use
StorageVolume.getUuid()on StorageVolume which you get from StorageManager.
The value is volume ID assigned during formatting of the card, and its length/format differs depending on file system type. For FAT32 it is XXXX-XXXX, for NTFS it's longer hex string, for Internal mass storage it returns null.
Need to parse this file (mixed latin & arabic):
1|حِيمِ
2|الَمِينَ
The file was saved as UTF8 in notepad++, and put in android asset folder.
Expected result: for line1, the entries are "1" and "حِيمِ" (split by "|").
AssetManager manager = context.getAssets();
InputStream inStream = null;
inStream = manager.open("file.txt");
BufferedReader buffer = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
String line = "";
while ((line = buffer.readLine()) != null) {
String lineEnc = URLEncoder.encode(line, "UTF-8");
String[] columns = lineEnc.split("%7C");
if (columns.length>=3) {
Toast toast = Toast.makeText(context, "Line: " + columns[0] + " and " + columns[1], Toast.LENGTH_LONG);
toast.show();
}
}
Actual Result:
columns[0] = "1" ok, but
columns[1] = "%D8%AD%D9..." not Ok, expected "حِيمِ".
How to fix this, or is there better way? Please help. Thanks in advance.
Solved, changing:
while ((line = buffer.readLine()) != null) {
String lineEnc = URLEncoder.encode(line, "UTF-8");
String[] columns = lineEnc.split("%7C");
into
while ((line = buffer.readLine()) != null) {
String[] columns = line.split("\\|");
I've an asynctask , this is the code inside background :
try {
URL url = new URL(urls);
Log.v("this", urls);
URLConnection conn = url.openConnection();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(conn.getInputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String lineC;
StringBuffer sb = new StringBuffer();
while ((lineC = bufferedReader.readLine()) != null)
{
sb.append(lineC );
}
Log.v("this",sb.toString() + " I need this for something else");
NodeList nodes = doc.getElementsByTagName("data");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList title = element.getElementsByTagName("content");
Element line = (Element) title.item(0);
if (i == 0)
random = line.getTextContent();
else if (i == 1)
topsell = line.getTextContent();
else if (i == 2)
jadidtarin = line.getTextContent();
else if (i == 3)
pishnahad = line.getTextContent();
}
} catch (Exception e) {
e.printStackTrace();
goterror = true;
Log.v("this", e.getMessage() + "eror");
}
When I remove these lines , it works fine and I get no problem.
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String lineC;
StringBuffer sb = new StringBuffer();
while ((lineC = bufferedReader.readLine()) != null)
{
sb.append(lineC );
}
I don't close anything but when I run the code, I get this error "Stream is closed" , I don't know why .
How can I solve this problem ? as I said in the code, I need to save the whole returned value .
How can I solve this problem ?
Instead of using:
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
Use
BufferedReader bufferedReader = new BufferedReader(new BufferedInputStream(conn.getInputStream()));
Hello I get some xml file
They are on UTF-8 so i follow some sample and my code look like this
String text = "";
String str;
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(Path), "UTF-8"));
while ((str = in.readLine()) != null) {
text += str;
}
return text;
And then i try to parse the code with the dom parser
Document doc = parser.getDomElement(result);
And this fail
I have check my xml file with a hexeditor
I have the following charcode before "<": ef bb bf
What have i miss? why getDomElement tell me
Unexpected token (position:TEXT #1:2)
text += str + "\n";
If there was a line break in a tag:
<img
src="smile.jpg"/>
you could get:
<imgsrc="smile.jpg">
And some other cases.
StringBuilder text = new StringBuilder();
try (BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(Path), "UTF-8"))) {
String str;
while ((str = in.readLine()) != null) {
text.append(str).append("\n");
}
} // Does an in.close()
return text.toString();
i am reading a file using the following code
`FileReader fr=new FileReader("/mnt/sdcard/content.csv");
BufferedReader in = new BufferedReader(fr);
String reader = "";
while ((reader = in.readLine()) != null){
String[] RowData = reader.split(",");
id = RowData[0];
path = RowData[1];`
and i have also tried using the opencsv class but with both the method i am only able to read the last entry in the file..
what am i missing?can someone explain to me?
FileReader fr=new FileReader("/mnt/sdcard/playlist_record.csv");
BufferedReader in = new BufferedReader(fr);
String reader = "";
while ((reader = in.readLine()) != null){
String[] RowData = reader.split(",");
id = RowData[0];
path = RowData[1];
type= RowData[2];
update= RowData[3];
server= RowData[4];
t1.setText(id);
// t1.append(path);
// t1.append(type);
// t1.append(server);
}
in.close();
this is my code i am using to read the file