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());
Related
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();
I know there are several similar questions however, I couldn't figure out what is wrong with my code. I am trying to read the program logs and write them in an activity. I basically wanted to write only my tagged Debug and Error messages to show but it does still show logs I don't want.
Here it is my code:
String[] command=new String[]{"logcat", "-d","-s","*:D"};
Process process = Runtime.getRuntime().exec(command);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append("\n"+line);
}
TextView txtLogs = (TextView)findViewById(R.id.txtLogs);
txtLogs.setMovementMethod(new ScrollingMovementMethod());
txtLogs.setText(log.toString());
and i have tags like ;
Log.e(TAG, "Error onCreate() " + ex.getMessage());
and I am getting result like
D/ScanFragment(18675): message
Why do I get a log like 'message' ?
thank you
I'm new to the android world and i have some problem.
I'm developing a project under android and it require a json parser. I get my json file from a web service developed under Zend framework, the link to the web service : "manganew:8080/wsmanganew/manga/manga/idmanga/1" and the content of the json file is
{
"manga": [
{
"idmanga":"1",
"titre":"naruto",
"episode":"145",
"url":"http:\/\/naruto.com\/",
"image":null,
"description":"Naruto Shippuuden .",
"tv":"TV Tokyo",
"dtdebut":"2013-05-23 12:30:00",
"iduser":"1"
}
]}
I'm following this tutorial "http://www.androidhive.info/2012/01/android-json-parsing-tutorial/".
i don't know how index to the web service link in the android, any help will be useful.
thank you
so, what do you need to do? download json object from that url into your app?
that code has method getJSONFromUrl in it's class, you should use it
though, according to comments it has flaws in it.
to read string from file use this code
BufferedReader reader = new BufferedReader(new FileReader("/mnt/sdcard/docs/file.json"));
String line, results = "";
while( (line = reader.readLine()) != null)
results += line;
reader.close();
JSONObject obj = new JSONObject(results);
replace path with path to your file
try this
BufferedReader reader = new BufferedReader(new FileReader("C:\\file.json"));
String line, results = "";
while( (line = reader.readLine()) != null)
results += line;
reader.close();
JSONObject obj = new JSONObject(results);
Replace with your local machine download field path C:\\file.json
I have get html data from webpage. But i want to get only data excluding html tags.
I have tried this:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(urlText.getText().toString());
// Get the response
BufferedReader rd = new BufferedReader(new InutStreamReader(response.getEntity().getContent()));
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = rd.readLine()) != null)
{
textView.append(line);
sb.append(line+"\n");
}
This giving me whole html data. Tell me now i can get data only.
Have you tried using Html.fromHtml(source)? or use any Java HTML parser (If they work on android) for this.
Here source is your html formatted whole data.
EDIT:
while ((line = rd.readLine()) != null)
{
sb.append(line+"\n");
}
String source = sb.toString();
textView.setText(Html.fromHtml(source));
Look at this example Android Parsing HTML Content Containing Links.
I have just started with Android development, and facing one issue in the android application.
When application tries to read the data from file (raw resources), it throws IOException on readLine() function. code is as below:
final Resources resources = m_ApplicationContext.getResources();
InputStream inputStream = resources.openRawResource(R.raw.datafile);
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream));
try {
String line;
while ((line = reader.readLine()) != null) {
the reader.readLine() function is throwing the exception. Do I need to mention any kind of additional permission for reading the file ?
Thanks in advance.
I have had the same issue. The problem seems to be that you can only read files with up to 1MB
see here
http://groups.google.com/group/android-developers/browse_thread/thread/e3765c112d112f24