Problems running Unix commands in native Android Java... - Android Software Development

Hello,
I've been trying to do some android stuff on java for some time now, and i've come across a problem here: i can't get the app to execute linux stuff, as there is no system() method like on other platforms... so i searched some code and found this:
Code:
protected void system(String[] Commands){
e Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
DataInputStream osRes = new DataInputStream(process.getInputStream());
Vector<String> res = new Vector<String>();
for (String single : Commands) {
e os.writeBytes(single + "\n");
e os.flush();
e res.add(osRes.readLine());
// Log.e("CMDs", osRes.readLine());
}
e os.writeBytes("exit\n");
e os.flush();
process.waitFor();
}
However that won't work because of some errors i have in the marked lines:
"Unhandled exception type IOException"
and the process.waitFor(); line also gives me an error:
"Unhandled exception type InterruptedException"
Any ideas?

You need to add a try/catch block around that code which catches the IO exception and the interrupted exception.

deleted

So, first of all thanks to both of you it appears to be working now... i tried in on the emulator, and of course "su" didn't work there (broken pipe), so i replaced it by "sh", however this didn't seem to work well too. the application just locked up with a warning in android.... strange...
edit: tried using /system/bin/sh, didn't work, locked up again

What version of Android in the emulator? I've done it with 1.5 through 2.2 in the emulator, just by using "sh".

could you post the code you used please? would be AWESOME!
i'm trying to get this working on 2.1

Sure, I can post some more details later, but for now just the code.
Include the file in your project and use with:
Code:
ShellCommand cmd = new ShellCommand();
CommandResult r = cmd.sh.runWaitFor("ls -l");
if (!r.success()) {
Log.v(TAG, "Error " + r.stderr);
} else {
Log.v(TAG, "Success! " + r.stdout);
}
If you want su you can either use cmd.shOrSu().runWaitFor("..."); which will try su (by running "id", it just tests the status code but it's a nice entry in logcat for debugging) and fallback to sh. Or you can use cmd.su.runWaitFor("...");
Also at
teslacoilsw.com/files/ShellCommand.java
Code:
package com.teslacoilsw.quicksshd;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import android.util.Log;
public class ShellCommand {
private static final String TAG = "ShellCommand.java";
private Boolean can_su;
public SH sh;
public SH su;
public ShellCommand() {
sh = new SH("sh");
su = new SH("su");
}
public boolean canSU() {
return canSU(false);
}
public boolean canSU(boolean force_check) {
if (can_su == null || force_check) {
CommandResult r = su.runWaitFor("id");
StringBuilder out = new StringBuilder();
if (r.stdout != null)
out.append(r.stdout).append(" ; ");
if (r.stderr != null)
out.append(r.stderr);
Log.v(TAG, "canSU() su[" + r.exit_value + "]: " + out);
can_su = r.success();
}
return can_su;
}
public SH suOrSH() {
return canSU() ? su : sh;
}
public class CommandResult {
public final String stdout;
public final String stderr;
public final Integer exit_value;
CommandResult(Integer exit_value_in, String stdout_in, String stderr_in)
{
exit_value = exit_value_in;
stdout = stdout_in;
stderr = stderr_in;
}
CommandResult(Integer exit_value_in) {
this(exit_value_in, null, null);
}
public boolean success() {
return exit_value != null && exit_value == 0;
}
}
public class SH {
private String SHELL = "sh";
public SH(String SHELL_in) {
SHELL = SHELL_in;
}
public Process run(String s) {
Process process = null;
try {
process = Runtime.getRuntime().exec(SHELL);
DataOutputStream toProcess = new DataOutputStream(process.getOutputStream());
toProcess.writeBytes("exec " + s + "\n");
toProcess.flush();
} catch(Exception e) {
Log.e(QuickSSHD.TAG, "Exception while trying to run: '" + s + "' " + e.getMessage());
process = null;
}
return process;
}
private String getStreamLines(InputStream is) {
String out = null;
StringBuffer buffer = null;
DataInputStream dis = new DataInputStream(is);
try {
if (dis.available() > 0) {
buffer = new StringBuffer(dis.readLine());
while(dis.available() > 0)
buffer.append("\n").append(dis.readLine());
}
dis.close();
} catch (Exception ex) {
Log.e(TAG, ex.getMessage());
}
if (buffer != null)
out = buffer.toString();
return out;
}
public CommandResult runWaitFor(String s) {
Process process = run(s);
Integer exit_value = null;
String stdout = null;
String stderr = null;
if (process != null) {
try {
exit_value = process.waitFor();
stdout = getStreamLines(process.getInputStream());
stderr = getStreamLines(process.getErrorStream());
} catch(InterruptedException e) {
Log.e(TAG, "runWaitFor " + e.toString());
} catch(NullPointerException e) {
Log.e(TAG, "runWaitFor " + e.toString());
}
}
return new CommandResult(exit_value, stdout, stderr);
}
}
}

Thanks kevin The code you are using there is awesome Looking good so far, however it keeps returning permission denied... is it some setting in the android manifest?

Actually "Permission denied" often also means "no such file or directory" on android :-/ . It's very frustrating.
Try running something simple to start with like:
cmd.sh.runWaitFor("echo foo");

[email protected] said:
Actually "Permission denied" often also means "no such file or directory" on android :-/ . It's very frustrating.
Try running something simple to start with like:
cmd.sh.runWaitFor("echo foo");
Click to expand...
Click to collapse
yep, i tried running echo as i was confused by the "permission denied" although i had already set write permissions for the sdcard... didn't work, for some odd reason

Related

csharp - exception was unhandeld

Hello,
I just started with developing in csharp for my mobile phone. The first project i'm working on is creating a GPS tracker program that will safe the current GPS location to a text file. This file i'll load to a website from where people can download an google earth file (i prefer php programming )
But to get to the point. The program worked fine untill i added some extra functionality (SystemIdleTimerReset and the date in the name of the datafile)
After adding this the program starts in the emulator, however after 30 seconds (when the trigger start for the first time) it comes with the exception (at the red comment):
Exception was unhandeld
The method or operation is not implemented
My code (i added everything from the main form, since i don't know where the error is)
Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.WindowsMobile.Samples.Location;
namespace GPS_Tracker
{
public partial class mainform : Form
{
GpsDeviceState device = null;
GpsPosition position = null;
Gps gps = new Gps();
int iTicker = 0;
int iCounter = 0;
string fileName;
private EventHandler updateDataHandler;
private EventHandler timerTicker;
DateTime currentTime = DateTime.Now;
public mainform()
{
InitializeComponent();
}
private void menuExit_Click(object sender, EventArgs e)
{
if (gps.Opened)
{
gps.Close();
}
Close();
}
private void menuStart_Click(object sender, EventArgs e)
{
if (!gps.Opened)
{
gps.Open();
}
menuStart.Enabled = false;
menuStop.Enabled = true;
}
private void menuStop_Click(object sender, EventArgs e)
{
if (gps.Opened)
{
gps.Close();
}
menuStart.Enabled = true;
menuStop.Enabled = false;
}
private void mainform_Load(object sender, EventArgs e)
{
timerTicker = new EventHandler(timerCheck);
updateDataHandler = new EventHandler(UpdateData);
gps.DeviceStateChanged += new DeviceStateChangedEventHandler(gps_DeviceStateChanged);
gps.LocationChanged += new LocationChangedEventHandler(gps_LocationChanged);
Status.Text = "";
}
protected void gps_LocationChanged(object sender, LocationChangedEventArgs args)
{
position = args.Position;
// call the UpdateData method via the updateDataHandler so that we
// update the UI on the UI thread
Invoke(timerTicker);
}
void gps_DeviceStateChanged(object sender, DeviceStateChangedEventArgs args)
{
device = args.DeviceState;
// call the UpdateData method via the updateDataHandler so that we
// update the UI on the UI thread
Invoke(timerTicker);[COLOR="Red"]// place where error comes[/COLOR]
}
void timerCheck(object sender, System.EventArgs args)
{
Invoke(updateDataHandler);
}
void UpdateData(object sender, System.EventArgs args)
{
if (iTicker == 1)
{
SystemIdleTimerReset();
if (gps.Opened)
{
string str = "";
if (device != null)
{
str = device.FriendlyName + " " + device.ServiceState + ", " + device.DeviceState + "\n";
}
if (position != null)
{
iCounter++;
if (position.SeaLevelAltitudeValid &&
position.EllipsoidAltitudeValid &&
position.SpeedValid &&
position.LatitudeValid &&
position.LongitudeValid &&
position.SatellitesInSolutionValid &&
position.SatellitesInViewValid &&
position.SatelliteCountValid &&
position.TimeValid)
{
fileName = "data_" + currentTime.ToString("yyyyMMdd") + ".txt";
StreamWriter output;
output = File.AppendText(fileName);
output.WriteLine(position.SeaLevelAltitude + ";" +
position.EllipsoidAltitude + ";" +
position.SpeedKmh + ";" +
position.Longitude + ";" +
position.Latitude + ";" +
position.GetSatellitesInSolution().Length + "/" +
position.GetSatellitesInView().Length + " (" +
position.SatelliteCount + ";" +
position.Time.ToString());
output.Close();
str += "Saving data to File \nEntry: " + iCounter + "\n";
str += "Current speed: " + position.SpeedKmh + "\n";
str += "Current height: " + position.SeaLevelAltitude + "\n";
}
}
Status.Text = str;
}
iTicker = 0;
}
}
private void SystemIdleTimerReset()
{
throw new Exception("The method or operation is not implemented.");
}
private void Ticker_Tick(object sender, EventArgs e)
{
iTicker = 1;
}
}
}
the error details:
Error details:
System.Exception was unhandled
Message="The method or operation is not implemented."
StackTrace:
at GPS_Tracker.mainform.SystemIdleTimerReset()
at GPS_Tracker.mainform.UpdateData()
at TASK.Invoke()
at System.Windows.Forms.Control._InvokeAll()
at System.Windows.Forms.Control.InvokeHelper()
at System.Windows.Forms.Control.Invoke()
at GPS_Tracker.mainform.timerCheck()
at TASK.Invoke()
at System.Windows.Forms.Control._InvokeAll()
at System.Windows.Forms.Control.WnProc()
at System.Windows.Forms.ContainerControl.WnProc()
at System.Windows.Forms.Form.WnProc()
at System.Windows.Forms.Control._InternalWnProc()
at Microsoft.AGL.Forms.EVL.EnterMainLoop()
at System.Windows.Forms.Application.Run()
at GPS_Tracker.Program.Main()
Click to expand...
Click to collapse
I hope anybody can help me.
Thanks in advance
StruiS
I removed (commented) the SystemIdleTimerReset function and it works again.
Can someone help me implement this function?
Thanks

[Q] How to read directories?

Hey guys, this is the first time im trying my hand at android development, im still fairly new to development altogether.
Basically what I'm trying to do at the moment is read a list of folder-names in a particular directory, then write those to another file.
The file I'm writing to is at /data/data/com.rone/files/app_list
I'm trying to read from /data/data/
The app has been given su privileges as well. My issue is that I can't seem to read from any folder other than / (root)
Code:
private OnClickListener sort_listener = new OnClickListener() {
@Override
public void onClick(View v) {
String datafolders ="";
File dir = new File("/data/data/");
if(dir.isDirectory() && dir.canRead()){
Log.v("DEBUG", dir.getName() + " is the searching directory!");
for(int i = 0; i < dir.list().length; i++) {
datafolders += dir.list()[i] + " \n";
}
}
else {
Log.v("ERROR", "directory does not exist or can not be accessed!");
}
writeFile(datafolders, "app_list");
}
};
public void writeFile(String input, String filename) {
try {
FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
fos.write(input.getBytes());
fos.close();
return;
} catch (FileNotFoundException e) {
Log.v("ERROR", e.getMessage());
} catch (IOException e) {
Log.v("ERROR", e.getMessage());
}
}
public String readFile(String filename) {
try {
FileInputStream fis = openFileInput(filename);
String temp = "";
int ch;
while ((ch = fis.read()) > -1) {
temp += (char) ch;
}
fis.close();
return temp;
} catch (FileNotFoundException e) {
Log.v("ERROR", e.getMessage());
return "Exception" + e;
} catch (IOException e) {
Log.v("ERROR", e.getMessage());
return "Exception" + e;
}
}
It only writes to the file when I use:
Code:
File dir = new File("./");
Also, my app is specifically written for the SGS and I'm using Eclipse. Any way to get and import a custom SGS skin with the Menu and Back button functionality?
No replies? No one knows how to read the directories? Is there a limitation built-in that stops from reading directories? Even with su permissions?

[Q] Can I register a listener on process state?

I'm an experienced developer but new to Android development. I have an app that runs some native binaries, and I provide a status indicator to show when the native process is running and when it's not. Currently I poll the device to figure this out, using the ActivityManager API to determine if specific processes are running or not.
I'm hoping there is some way to register a listener on process state changes, so I can get notified when my process starts or stops. I looked through the API, and there doesn't seem to be such a thing. Does anyone know how I can keep track of process start and stop other than polling via ActivityManager?
MidnightJava said:
I'm an experienced developer but new to Android development. I have an app that runs some native binaries, and I provide a status indicator to show when the native process is running and when it's not. Currently I poll the device to figure this out, using the ActivityManager API to determine if specific processes are running or not.
I'm hoping there is some way to register a listener on process state changes, so I can get notified when my process starts or stops. I looked through the API, and there doesn't seem to be such a thing. Does anyone know how I can keep track of process start and stop other than polling via ActivityManager?
Click to expand...
Click to collapse
Afaik there's no way to accomplish that other than your way or being system/root app. See this similar question here for reference.
Can you show how you start the process?
EmptinessFiller said:
Can you show how you start the process?
Click to expand...
Click to collapse
Sure. Here's the class that manages starting, stopping, and statusing (running or not) the binary executable. In this case, it's the omniNames service of the omni ORB (CORBA broker).
Code:
public class RHManager {
private TimerTask task = new TimerTask() {
@Override
public void run() {
if (RHManager.this.listener != null) {
listener.running(isOmniNamesRunning());
}
}
};
private IStatusListener listener;
public RHManager() {
}
public void startOmniNames() {
final Exec exec = new Exec();
final String[] args = new String[]
{RhMgrConstants.INSTALL_LOCATION_OMNI_NAMES_SCRIPTS + "/" + RhMgrConstants.OMNI_NAMES_SCRIPT_FILE,
"start"};
final String[] env = new String[] {"LD_LIBRARY_PATH=/sdcard/data/com.axiosengineering.rhmanager/omniORB/lib"};
Thread t = new Thread() {
public void run() {
try {
int res = exec.doExec(args, env);
logMsg("omniNames start return code " + res);
} catch (IOException e) {
logMsg("Failed to start omniNames");
e.printStackTrace();
}
String std = exec.getOutResult();
logMsg("omniNames start: std out==> " + std );
String err = exec.getErrResult();
logMsg("omniNames start: err out==> " + err );
};
};
t.start();
logMsg("omniNames started");
}
private boolean isOmniNamesRunning() {
String pid_s = getOmniNamesPid();
Integer pid = null;
if (pid_s != null) {
try {
pid = Integer.parseInt(pid_s);
} catch (NumberFormatException e) {
return false;
}
}
if (pid != null) {
RunningAppProcessInfo activityMgr = new ActivityManager.RunningAppProcessInfo("omniNames", pid, null);
return activityMgr.processName != null ;
}
return false;
}
public void stopOmniNames() {
String pid = getOmniNamesPid();
android.os.Process.killProcess(Integer.parseInt(pid));
android.os.Process.sendSignal(Integer.parseInt(pid), android.os.Process.SIGNAL_KILL);
}
private String getOmniNamesPid() {
Exec exec = new Exec();
final String[] args = new String[]
{RhMgrConstants.INSTALL_LOCATION_OMNI_NAMES_SCRIPTS + "/" + RhMgrConstants.OMNI_NAMES_SCRIPT_FILE,
"pid"};
String pid = "";
try {
int res = exec.doExec(args, null);
logMsg("oniNames pid return code: " + res);
} catch (IOException e) {
logMsg("Failed to start omniNames");
e.printStackTrace();
return pid;
}
String std = exec.getOutResult();
logMsg("omniNames pid: std out ==> " + std);
String err = exec.getErrResult();
logMsg("omniNames pid: err out ==> " + err);
String[] parts = std.split("\\s+");
if (parts.length >= 2) {
pid = parts[1];
}
return pid;
}
//monitor omniNames status and report status periodically to an IStatusListener
public void startMonitorProcess(IStatusListener listener, String string) {
this.listener = listener;
Timer t = new Timer();
t.schedule(task, 0, 1000);
}
private void logMsg(String msg) {
if (RhMgrConstants.DEBUG) {
System.err.println(msg);
}
}
}
Here's the Exec class that handles invocation of Runtime#exec(), consumes std and err out, and reports those and process return status to the caller.
Code:
public class Exec {
private String outResult;
private String errResult;
private Process process;
private boolean failed = false;
StreamReader outReader;
StreamReader errReader;
public int doExec(String[] cmd, String[] envp) throws IOException{
Timer t = null;
try {
process = Runtime.getRuntime().exec(cmd, envp);
outReader = new StreamReader(process.getInputStream());
outReader.setPriority(10);
errReader = new StreamReader(process.getErrorStream());
outReader.start();
errReader.start();
t = new Timer();
t.schedule(task, 10000);
int status = process.waitFor();
outReader.join();
errReader.join();
StringWriter outWriter = outReader.getResult();
outResult = outWriter.toString();
outWriter.close();
StringWriter errWriter = errReader.getResult();
errResult = errWriter.toString();
errWriter.close();
return (failed ? -1: status);
} catch (InterruptedException e) {
return -1;
} finally {
if (t != null) {
t.cancel();
}
}
}
public int doExec(String[] cmd) throws IOException{
return doExec(cmd, null);
}
public String getOutResult(){
return outResult;
}
public String getErrResult(){
return errResult;
}
private static class StreamReader extends Thread {
private InputStream is;
private StringWriter sw;
StreamReader(InputStream is) {
this.is = is;
sw = new StringWriter(30000);
}
public void run() {
try {
int c;
while ((c = is.read()) != -1){
sw.write(c);
}
}
catch (IOException e) { ; }
}
StringWriter getResult() {
try {
is.close();
} catch (IOException e) {
System.err.println("Unable to close input stream in StreamReader");
}
return sw;
}
}
private TimerTask task = new TimerTask() {
@Override
public void run() {
failed = true;
process.destroy();
}
};
}
Here's the script that startOminNames() invokes. It's the shell script installed with omniORB with functions other than start and get_pid removed, since those are handled by Android classes. You can invoke any executable in place of the script, or wrap your executable in a script.
Code:
#
# omniNames init file for starting up the OMNI Naming service
#
# chkconfig: - 20 80
# description: Starts and stops the OMNI Naming service
#
exec="/sdcard/data/com.axiosengineering.rhmanager/omniORB/bin/omniNames"
prog="omniNames"
logdir="/sdcard/data/com.axiosengineering.rhmanager/omniORB/logs"
logfile="/sdcard/data/com.axiosengineering.rhmanager/omniORB/logs/omninames-localhost.err.log"
options=" -start -always -logdir $logdir -errlog $logfile"
start() {
#[ -x $exec ] || exit 5
echo -n $"Starting $prog: "
$exec $options
}
get_pid() {
ps | grep omniNames
}
case "$1" in
start)
start && exit 0
$1
;;
pid)
get_pid
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
esac
exit $?
And here's the IStatusListener interface
Code:
public interface IStatusListener {
public void running(boolean running);
}
Runtime.exec() has some pitfalls. See this helpful Runtime.exec tutorial for a nice explanation.
And you may also want to check out this post on loading native binaries in Android.

[HOW-TO] Use Code Syntax Highlighting on XDA [Raise Awareness]

XDA is an awesome place for developers to share their knowledge and help each other, no question about it.
However, in my opinion, one of the flaws that prevent the app development forums from being more popular is the
Code:
tags.
Indeed, these tags do not provide syntax highlighting, and when people start pasting their whole Java class, the lack of proper indentation plus the lack of syntax highlighting leaves us with a very off-putting chunk of code, and even if you intend to help in the first place, having to go through this unreadable code can discourage many.
I was ranting about this in the [URL="http://forum.xda-developers.com/showpost.php?p=49938207&postcount=1976"]xda suggestions & feedback thread[/URL], when someone pointed-out the existence of the [B][PHP][/B] tags, which provide syntax highlighting for php, and, the syntaxes of PHP and Java being somewhat similar, they also do the trick for Java.
[FONT="Garamond"][I]Example:[/I][/FONT]
[PHP]
package com.androguide.recovery.emulator;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import android.util.Log;
public class CMDProcessor {
private final String TAG = getClass().getSimpleName();
private static final boolean DEBUG = false;
private Boolean can_su;
public SH sh;
public SH su;
public CMDProcessor() {
sh = new SH("sh");
su = new SH("su");
}
public SH suOrSH() {
return canSU() ? su : sh;
}
public boolean canSU() {
return canSU(false);
}
public class CommandResult {
private final String resultTag = TAG + '.' + getClass().getSimpleName();
public final String stdout;
public final String stderr;
public final Integer exit_value;
CommandResult(final Integer exit_value_in) {
this(exit_value_in, null, null);
}
CommandResult(final Integer exit_value_in, final String stdout_in,
final String stderr_in) {
exit_value = exit_value_in;
stdout = stdout_in;
stderr = stderr_in;
if (DEBUG)
Log.d(TAG, resultTag + "( exit_value=" + exit_value
+ ", stdout=" + stdout + ", stderr=" + stderr + " )");
}
public boolean success() {
return exit_value != null && exit_value == 0;
}
}
public class SH {
private String SHELL = "sh";
public SH(final String SHELL_in) {
SHELL = SHELL_in;
}
private String getStreamLines(final InputStream is) {
String out = null;
StringBuffer buffer = null;
final DataInputStream dis = new DataInputStream(is);
try {
if (dis.available() > 0) {
buffer = new StringBuffer(dis.readLine());
while (dis.available() > 0) {
buffer.append("\n").append(dis.readLine());
}
}
dis.close();
} catch (final Exception ex) {
Log.e(TAG, ex.getMessage());
}
if (buffer != null) {
out = buffer.toString();
}
return out;
}
public Process run(final String s) {
Process process = null;
try {
process = Runtime.getRuntime().exec(SHELL);
final DataOutputStream toProcess = new DataOutputStream(
process.getOutputStream());
toProcess.writeBytes("exec " + s + "\n");
toProcess.flush();
} catch (final Exception e) {
Log.e(TAG,
"Exception while trying to run: '" + s + "' "
+ e.getMessage());
process = null;
}
return process;
}
public CommandResult runWaitFor(final String s) {
if (DEBUG)
Log.d(TAG, "runWaitFor( " + s + " )");
final Process process = run(s);
Integer exit_value = null;
String stdout = null;
String stderr = null;
if (process != null) {
try {
exit_value = process.waitFor();
stdout = getStreamLines(process.getInputStream());
stderr = getStreamLines(process.getErrorStream());
} catch (final InterruptedException e) {
Log.e(TAG, "runWaitFor " + e.toString());
} catch (final NullPointerException e) {
Log.e(TAG, "runWaitFor " + e.toString());
}
}
return new CommandResult(exit_value, stdout, stderr);
}
}
public boolean canSU(final boolean force_check) {
if (can_su == null || force_check) {
final CommandResult r = su.runWaitFor("id");
final StringBuilder out = new StringBuilder();
if (r.stdout != null) {
out.append(r.stdout).append(" ; ");
}
if (r.stderr != null) {
out.append(r.stderr);
}
Log.d(TAG, "canSU() su[" + r.exit_value + "]: " + out);
can_su = r.success();
}
return can_su;
}
}
[/PHP]
Oddly enough, the [B][PHP][/B] tags also work for XML (while the [html] tags, that do exist, don't work):
[PHP]
<LinearLayout
android:id="@+id/contentLayout"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:layout_weight="90"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:fontFamily="sans-serif-light"
android:textColor="#33B6EA"
android:textSize="22sp" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
android:ellipsize="end"
android:fontFamily="sans-serif-light"
android:textColor="#232323"
android:maxLines="50"
android:textSize="16sp" />
</LinearLayout>
[/PHP]
This doesn't fix the indentation issues, but as you can see, the code is much more readable and welcoming.
By using the [B][PHP][/B] tags instead of the [strike][CODE][/strike] tags, you will be more likely to get answers to your questions involving code.
[B][SIZE="3"][CENTER]So Please, for the sake of the app development forums and the eyes of your readers:
USE THE [COLOR="RoyalBlue"][PHP][/COLOR] TAGS :victory:[/CENTER][/SIZE][/B]
[CENTER][I]Please raise awareness about this, suggest people who don't use the [B][PHP][/B] tags to use them, and together we'll make this app development forum a better place than it already is :good:[/I][/CENTER]
Whoa! Never knew about this. Nice find.
Wow, this is really cool! Thanks for finding. The only problem I have with the PHP tags is on mobile, tapatalk is obviously not supporting it at all:
SimplicityApks said:
Wow, this is really cool! Thanks for finding. The only problem I have with the PHP tags is on mobile, tapatalk is obviously not supporting it at all:
Click to expand...
Click to collapse
Too bad, thanks for reporting :good:
This obviously is just a workaround, xda could quite easily implement a dedicated syntax highlighting plugin for vBulletin as I suggested in the suggestions thread, but for the time being this is probably the closest thing we've got.
Androguide.fr said:
Too bad, thanks for reporting :good:
This obviously is just a workaround, xda could quite easily implement a dedicated syntax highlighting plugin for vBulletin as I suggested in the suggestions thread, but for the time being this is probably the closest thing we've got.
Click to expand...
Click to collapse
They've got it working way better with the CODE tags: Code syntax highlighting announcement!

How can I loop through my JSON object?

I am new to Java and I am getting kind of stuck by trying to loop through my JSON. I am retrieving a JSON object where I want to loop through.
My JSON looks as follow:
Code:
{"message":{"2":[{"uid":"2","title":"","message":"Test1","success":1,"created_at":null,"updated_at":null}],"3":[{"uid":"3","title":"","message":"Test2 !","success":1,"created_at":null,"updated_at":null}],"4":[{"uid":"4","title":"Bla","message":"Test3!","success":1,"created_at":null,"updated_at":null}]}}
I tried a loop like this:
Code:
for(int i = 0; i<json.names().length(); i++){
try {
Log.v("TEST", "key = " + json.names().getString(i) + " value = " + json.get(json.names().getString(i)));
} catch (JSONException e) {
e.printStackTrace();
}
}
But this will only target "message" which includes the whole JSON "string". I want to loop through each message and retrieving the value of uid 1, uid 2 etc. How can I achieve this?
Thanks in advance.
Here is how I'm doing it:
Code:
private static final String AllNewsItemsURL = "some_url_here.php";
private static final String TAG_SUCCESS = "success";
private static final String NEWS = "news";
private static final String TITLE = "title";
private static final String STORY = "story";
private final JSONParser jParser = new JSONParser();
private JSONArray newsItems = null;
..... / code snipped / ....
try {
JSONObject json = jParser.makeHttpRequest(AllNewsItemsURL, params);
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
newsItems = json.getJSONArray(NEWS);
for (int i = 0; i < newsItems.length(); i++) {
JSONObject obj = newsItems.getJSONObject(i);
Integer id = i + 1;
String title = obj.getString(TITLE);
String story = obj.getString(STORY);
}
} else {
Log.e("JSON Response", "success == 0");
}
} catch (Exception e) {
e.printStackTrace();
}
Thanks for your reply. I tried it but getting this exception:
Code:
org.json.JSONException: Value {"2":[{"uid":"2","title":"","message":"Test1","success":1,"created_at":null,"updated_at":null}],"3":[{"uid":"3","title":"","message":"Test2","success":1,"created_at":null,"updated_at":null}],"4":[{"uid":"4","title":"Bla","message":"Test3","success":1,"created_at":null,"updated_at":null}]} at messages of type org.json.JSONObject cannot be converted to JSONArray
CodeMonkeyy said:
Thanks for your reply. I tried it but getting this exception:
Code:
org.json.JSONException: Value {"2":[{"uid":"2","title":"","message":"Test1","success":1,"created_at":null,"updated_at":null}],"3":[{"uid":"3","title":"","message":"Test2","success":1,"created_at":null,"updated_at":null}],"4":[{"uid":"4","title":"Bla","message":"Test3","success":1,"created_at":null,"updated_at":null}]} at messages of type org.json.JSONObject cannot be converted to JSONArray
Click to expand...
Click to collapse
You might want to try the matching JSONParser I have for it, sorry forgot to include it:
https://github.com/JonnyXDA/WGSB/bl...om/jonny/wgsb/material/parser/JSONParser.java
Also noting that your entire JSON Array is called "message" but you also have a parameter called "message" - maybe rename the Array to "messages"?
As for the code you should have something like:
Code:
private static final String AllNewsItemsURL = "some_url_here.php";
private static final String TAG_SUCCESS = "success";
private static final String MESSAGES = "messages";
private static final String TITLE = "title";
private static final String MESSAGE = "message";
private final JSONParser jParser = new JSONParser();
private JSONArray messageItems = null;
..... / code snipped / ....
try {
JSONObject json = jParser.makeHttpRequest(AllNewsItemsURL, params);
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
messageItems = json.getJSONArray(MESSAGES);
for (int i = 0; i < messageItems.length(); i++) {
JSONObject obj = messageItems.getJSONObject(i);
String title = obj.getString(TITLE);
String message = obj.getString(MESSAGE);
}
} else {
Log.e("JSON Response", "success == 0");
}
} catch (Exception e) {
e.printStackTrace();
}
My code looks like the this:
Code:
//Message task
MessageTask task = new MessageTask(DashboardActivity.class);
task.execute();
try {
json = task.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
messageItems = json.getJSONArray(MESSAGES);
for (int i = 0; i < messageItems.length(); i++) {
JSONObject obj = messageItems.getJSONObject(i);
String title = obj.getString(TITLE);
String message = obj.getString(MESSAGE);
}
} else {
Log.e("JSON Response", "success == 0");
}
} catch (Exception e) {
e.printStackTrace();
}
I am using Async Task to retrieve my JSON.
And my JSON parser looks like this:
Code:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
But it doesn't get through the if statement, because it can't find the value success. ( org.json.JSONException: No value for success ). Don't really know what I am doing wrong here. Is it because I am using AsyncTask and retrieving my JSON the wrong way?
I also renamed my Array to "messages", stupid mistake thanks!
CodeMonkeyy said:
But it doesn't get through the if statement, because it can't find the value success. ( org.json.JSONException: No value for success ). Don't really know what I am doing wrong here. Is it because I am using AsyncTask and retrieving my JSON the wrong way?
I also renamed my Array to "messages", stupid mistake thanks!
Click to expand...
Click to collapse
We're getting closer! With regards to using AsyncTask - thats fine and the recommended way to do service side sync/download operations (doesn't block the UI thread) so no need to change that.
I just took a look at my reference JSON and I have the success tag outside of an item eg:
Code:
{"topical":[{"tid":"5","title":"Exam countdown... just 12 weeks left!","story":"some_story_text_here","staff":"0","red":"0","show":"1"}],[COLOR="red"]"success":1[/COLOR]}
Whereas your success tag is put in each item:
Code:
{"message":{"2":[{"uid":"2","title":"","message":"Test1","[COLOR="red"]success":1,[/COLOR]"created_at":null,"updated_at":null}],"3":[{"uid":"3","title":"","message":"Test2 !",[COLOR="red"]"success":1[/COLOR],"created_at":null,"updated_at":null}],"4":[{"uid":"4","title":"Bla","message":"Test3!","[COLOR="Red"]success":1[/COLOR],"created_at":null,"updated_at":null}]}}
I'm guessing that your php line for:
PHP:
$response["success"] = 1;
is inside of the while loop:
PHP:
while ($row = mysql_fetch_array($result)) {
Taking it out of the while loop should fix that
That makes sense, because I am trying to get a success code for my whole JSON response. I changed my PHP code to:
Code:
while($row = mysqli_fetch_array( $messages )) {
// create rowArr
$rowArr = array(
'uid' => $row['id'],
'title' => $row['title'],
'message' => $row["message"],
'created_at' => $row['created_at'],
'updated_at' => $row['updated_at'],
);
// store rowArr in $return_arr
$return_arr[$row['id']][] = $rowArr;
}
$return_arr['success'] = 1;
// Json encode
echo json_encode(array("messages" => $return_arr));
}
Retrieving the following JSON:
Code:
{"messages":{"2":[{"uid":"2","title":"","message":"Test1","created_at":null,"updated_at":null}],"3":[{"uid":"3","title":"","message":"Test2 !","created_at":null,"updated_at":null}],"4":[{"uid":"4","title":"Bla","message":"Test3!","created_at":null,"updated_at":null}],"success":1}}
But I am still getting the following exception:
Code:
org.json.JSONException: No value for success
The success tag is still being encoded as part of an inner array, not the first array - try this:
PHP:
if (mysql_num_rows($messages) > 0) {
$response["messages"] = array();
while ($row = mysql_fetch_array($messages)) {
$messagesArray= array(
'uid' => $row['id'],
'title' => $row['title'],
'message' => $row['message'],
'created_at' => $row['created_at'],
'updated_at' => $row['updated_at'],
);
array_push($response["messages"], $messagesArray);
}
$response["success"] = 1;
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "No messages found";
echo json_encode($response);
}
And it's finally working!
Getting the following JSON result:
Code:
{"tag":"message","success":1,"error":0,"messages":[{"uid":"2","title":"","message":"Test1","created_at":null,"updated_at":null},{"uid":"3","title":"","message":"Test2!","created_at":null,"updated_at":null},{"uid":"4","title":"Bla","message":"Test3!","created_at":null,"updated_at":null}]}
And I can successfully loop through my JSON with the following code:
Code:
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
messageItems = json.getJSONArray(MESSAGES);
for (int i = 0; i < messageItems.length(); i++) {
JSONObject obj = messageItems.getJSONObject(i);
String title = obj.getString(TITLE);
String message = obj.getString(MESSAGE);
Log.e("TITLE :", title);
Log.e("MESSAGE :", message);
}
} else {
Log.e("JSON Response", "success == 0");
}
} catch (Exception e) {
e.printStackTrace();
}
Thank you very much! So the problem was that I placed the "success" tag outside my Array?

Categories

Resources