How to send Command prompt output to android - android

I am creating an app in which it will send some command to server and i want to get the output of that command on client (android).
Basically i am sending command "systeminfo" and the output is too big to handle, so is there any way to get that big output on android as text view or anything else?
Code is as below
Process process = Runtime.getRuntime().exec("systeminfo");
and for get the output i have used
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
I am confused how to use it as too much of string. Any reference material would be appreciated.

I'd recommend using a StringBuilder to reconstruct each line from the BufferedReader
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
builder.append(line).append("\n"); // Remove the \n if you don't want newlines
}
final String execOutput = builder.toString();

Related

Xamarin exec logcat command and read results to string

Is it possible to exec commands in a Xamarin application? Consider the following:
var process = Java.Lang.Runtime.GetRuntime().Exec("logcat");
var hasExited= await process.WaitForAsync();
I would like to be able to take the results of process and read it to a string. This can be done in native Android, but I am looking for a Xamarin C# solution. Any help is greatly appreciated.
Xamarin.Android is based on native Android.So you can call this method in Xamarin.Android .
var process = Java.Lang.Runtime.GetRuntime().Exec("getprop");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.InputStream));
StringBuilder builder = new StringBuilder();
string line;
while ((line = bufferedReader.ReadLine()) != null)
{
builder.Append(line + "\n");
}
System.Console.WriteLine(builder.ToString());

Logcat is only showing my PID

I am trying to get the logcat programmatically from my application. There are plenty of tutorials online on how to do this and I am doing it in one of my fragments as such:
Process process = Runtime.getRuntime().exec("logcat -v time -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
log.append("\n");
}
bufferedReader.close();
This works great; the only problem is that what is returned are only logcat entries from my PID. I would like all logcat entries. I do have READ_LOGS permission. Any ideas what is going on here?

JAVAMAIL get Content with InputStream

I'm trying to get the content Text of a Message in my Android App with an InputStream, because there I can get a line Separator. I'm getting the following Exception when I'm trying it:
java.lang.ClassCastException: java.lang.String cannot be cast to java.io.InputStream
This is my Code:
Object o = message.getContent();
InputStream is = (InputStream)o;
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
String everything = sb.toString();
Do you know what the problem is? In every Javamail - Thread you can read that this Method runs.
Use the Message.getInputStream method.

Javamail message content newline

I've made an E-Mail Client for my Android Phone using the JavaMail API, and I try to get the message Content with the following Code:
Object contentObject = p.getContent();
InputStream is = (InputStream) contentObject;
reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
String everything = sb.toString();
System.out.println(everything);
return everything;
With this method, I get the messsage Content as a String, but without Newlines. How can I format this String that he has the newlines from the Message?
P.S.: This are German e- mails, so the problem may be the encoding!?
Add like that
sb.append(line);
sb.append(System.getProperty("line.separator"));

To get the newline in textfile

I have my textfile stored in assets folder and my requirement is to show the contents in textview pointwise.I am able to access the contents if there is no space in text file which is stored in assets folder.If I puts the space in the text file then i am not able to get the contents after space.How to achieve this means to show the contents pointwise.
for example my textfile is as follows
a)Americab)Africac)India
I want output as
a) America
b) Africa
c) India
Here is my code to access the text file from assest folder which I am getting.
InputStream in = this.getAssets().open("detailtext.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
line = reader.readLine();
Since you need to read multiple lines, you need to loop through, till you reach the EOF. Try something like this:-
StringBuffer sb = new StringBuffer(0);
InputStream in = this.getAssets().open("detailtext.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = reader.readLine()) != null){
sb.append(line);
sb.append("\n");
}
String wholeText = sb.toString();
You're only reading one line, you need to use a while loop and continue to read each line until the end of the file
InputStream in = this.getAssets().open("detailtext.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while((line = reader.readLine()) != null){
// do whatever with line
}

Categories

Resources