I was unable to get the code you suggested (json_encode($topLevelArray, JSON_FORCE_OBJECT); ) to operate without error. I am using a php file and MySQL SELECT statements on my site to present a series of organizations which can be further reduced to a single organization. I am trying to bring back the single selected organization to my Android application. I was able to grab the line I needed and place it into the variable jnow but for some reason it does not seem to recognize that the first character is indeed '['. The code is as follows:
public class SelectLocation extends Activity { /** Called when the activity is first created. */ String result = ""; String jnow = ""; private Reader in; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); BufferedReader in = null; try { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(); request.setURI(new URI("http://www.mysite.com/ grabitgood.php")); HttpResponse response = client.execute(request); in = new BufferedReader (new InputStreamReader(response.getEntity().getContent())); StringBuffer sb = new StringBuffer(""); String line = ""; String NL = System.getProperty("line.separator"); while ((line = in.readLine()) != null) { sb.append(line + NL); } in.close(); String result = sb.toString(); String jnow = result.substring(result.indexOf("["), result.length()-1); int herenow = (result.lastIndexOf("[")); System.out.println(jnow); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } //parse json data try{ JSONArray jArray = new JSONArray(jnow); for(int i=0;i<jArray.length();i++){ JSONObject json_data = jArray.getJSONObject(i); Log.i("log_tag","org_id: "+json_data.getInt("id")+ ", orgname: "+json_data.getString("orgname")+ ", cityname: "+json_data.getString("cityname")+ ", statename: "+json_data.getString("statename") ); } }catch(JSONException e){ Log.e("log_tag", "Error parsing data "+e.toString()); } } public void BufferedReader(Reader in) { this.in = in; } } There are two lines that are read. The first line is: <html><head><title>Find Organizations</title></head><body style="background-color:#33990f"><body> The second line read is: [{"org_id":"39575","clubname":"ARTHRITIS FOUNDATION - VIRGINIA CHAPTER","orgcity":"RICHMOND","orgstate":"VA"}] The contents of jnow is only the second line which is: [{"org_id":"39575","clubname":"ARTHRITIS FOUNDATION - VIRGINIA CHAPTER","orgcity":"RICHMOND","orgstate":"VA"}] My logcat gives me the following error and I am not sure why? Can anyone point me in the right direction? 10-22 16:39:13.433: INFO/global(222): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k- char buffer is required. 10-22 16:39:13.463: INFO/System.out(222): [{"org_id":"39575","clubname":"ARTHRITIS FOUNDATION - VIRGINIA CHAPTER","orgcity":"RICHMOND","orgstate":"VA"}] 10-22 16:39:16.553: ERROR/log_tag(222): Error parsing data org.json.JSONException: A JSONArray text must start with '[' at character 0 of 10-22 16:39:17.343: INFO/ActivityManager(53): Displayed activity com.oys.gfa.acecapper/.SelectLocation: 8167 ms (total 8167 ms) On Oct 12, 10:07 pm, Brad Gies <rbg...@gmail.com> wrote: > You should be able to get rid of the HTML, but it's tough to give you > advice when we don't know the web server setup. > > If it's causing too many problems to get rid of the HTML, you can always > do other things. I had the same problem on one of my sites, and to get > the website admin to do anything was a 6 week problem, so I just sent > back my results like this: > > echo "|||STARTMYJSON|||=" . json_encode($json0) . "=|||ENDMYJSON|||"; > > When you get your results in your Android app into a string, just do a > substring starting with the Start location and ending with the end > location. It's not pretty but it's fairly fast. > > Also, I trashed your first email before I decided to respond, but I > think you were using PHP. You need to be aware that PHP will return > aJSONobject on occassion depending on your data, so if you have a newer > version of PHP, you can use the JSON_FORCE_OBJECT param to make it an > object every time, and your array will be the first property. It's > safer, faster, and less code than having to code for either an object or > an array in your Android app. > > json_encode($topLevelArray, JSON_FORCE_OBJECT); > > Sincerely, > > Brad Gies > ----------------------------------------------------------------------- > Bistro Bot - Bistro > Blurbhttp://bgies.comhttp://bistroblurb.comhttp://ihottonight.comhttp://forcethetruth.com > ----------------------------------------------------------------------- > > Everything in moderation, including abstinence (paraphrased) > > Every person is born with a brain... Those who learn to use it well are the > successful happy ones - Brad Gies > > Adversity can make or break you... It's your choice... Choose wisely - Brad > Gies > > Never doubt that a small group of thoughtful, committed people can > change the world. Indeed. It is the only thing that ever has - Margaret Mead > > On 12/10/2010 2:49 PM, Capt Spaghetti wrote: > > > > > I removed html from the php file as suggested by Kostya Vasilyev and > > changed the approach to retrieve the information. The new Android code > > is as follows: > > > ======================== > > public void onCreate(Bundle savedInstanceState) { > > super.onCreate(savedInstanceState); > > BufferedReader in = null; > > try { > > HttpClient client = new DefaultHttpClient(); > > HttpGet request = new HttpGet(); > > request.setURI(new > > URI("http://www.onyoursixinc.com/ > > grabitgood.php")); > > HttpResponse response = client.execute(request); > > in = new BufferedReader (new > > InputStreamReader(response.getEntity().getContent())); > > StringBuffer sb = new StringBuffer(""); > > String line = ""; > > String NL = System.getProperty("line.separator"); > > while ((line = in.readLine()) != null) { > > sb.append(line + NL); > > } > > in.close(); > > > String page = sb.toString(); > > System.out.println(page); > > } catch (URISyntaxException e) { > > // TODO Auto-generated catch block > > e.printStackTrace(); > > } catch (ClientProtocolException e) { > > // TODO Auto-generated catch block > > e.printStackTrace(); > > } catch (IOException e) { > > // TODO Auto-generated catch block > > e.printStackTrace(); > > } finally { > > if (in != null) { > > try { > > in.close(); > > } catch (IOException e) { > > e.printStackTrace(); > > } > > } > > } > > > //parsejsondata > > try{ > > JSONArray jArray = new JSONArray(result); > > for(int i=0;i<jArray.length();i++){ > > JSONObject json_data = > > jArray.getJSONObject(i); > > > Log.i("log_tag","org_id:"+json_data.getInt("id")+ > > ", orgname: > > "+json_data.getString("orgname")+ > > ", orgcity: > > "+json_data.getString("orgcity")+ > > ", > > orgstate:"+json_data.getString("orgstate") > > ); > > } > > }catch(JSONException e){ > > Log.e("log_tag", "Errorparsing data "+e.toString()); > > } > > > } > > ===================== > > from my LOGCAT I can see I am getting the correct select response but > > it looks like I am now picking up the html tags from the connect.php > > file ince I use > > require ("connect.php"); The LOGCAT now shows me the following: > > > ================ > > 10-12 17:37:41.320: INFO/System.out(218):<html><head><title>Find > > Organizations</title></head><body style="background- > > color:#33990f"><body> > > 10-12 17:37:41.330: INFO/System.out(218): > > [{"org_id":"39575","orgname":"ARTHRITIS FOUNDATION - VIRGINIA > > CHAPTER","orgcity":"RICHMOND","orgstate":"VA"}] > > 10-12 17:37:41.350:ERROR/log_tag(218):Errorparsing data > > org.json.JSONException: A JSONArray text must start with '[' at > > character 0 of > > 10-12 17:37:42.042: INFO/ActivityManager(53): Displayed activity > > com.oys.gfa.acecapper/.SelectOrgs: 40958 ms (total 40958 ms) > > > I am getting closer but still "no cigar". I don't know how to get rid > > of all the HTML since I will actually need to have the user enter a > > city and state and then "down select" from the response. The php file > > I am using is a hard coded SELECT since I figured dealing withJSON > > would be the more difficult problem to tackle. > > > Thanks, > > Capt Spaghetti > > > On Oct 12, 3:57 pm, Mark Murphy<mmur...@commonsware.com> wrote: > >> On Tue, Oct 12, 2010 at 3:51 PM, Capt Spaghetti<gene_august...@msn.com> > >> wrote: > >>> HttpPost httppost = new HttpPost("graborginfo.php"); > >> That is not a valid URL. > > >>> HttpResponse response = httpclient.execute(httppost); > >>> HttpEntity entity = response.getEntity(); > >>> InputStream is = entity.getContent(); > >>> }catch(Exception e){ > >>> Log.e("log_tag", "Errorin http connection > >>> "+e.toString()); > >>> } > >>> //convert response to string > >>> try{ > >>> InputStreamReader is = new > >>> InputStreamReader(System.in); > >>> BufferedReader reader = new BufferedReader(is); > >>> StringBuilder sb = new StringBuilder(); > >>> String line = null; > >>> while ((line = reader.readLine()) != null) { > >>> sb.append(line + "\n"); > >>> } > >>> is.close(); > >>> result=sb.toString(); > >>> }catch(Exception e){ > >>> Log.e("log_tag", "Errorconverting result > >>> "+e.toString()); > >>> } > >> For this, I recommend using the BasicResponseHandler pattern: > > >>http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/httpc... > > >> (orhttp://bit.ly/lCVgZifthat URL is too long) > > >>> The LOGCAT output for theerroris as follows: > >>> ===================== > >>> 10-12 11:14:08.199: INFO/global(233): Default buffer size used in > >>> BufferedReader constructor. It would be better to be explicit if an 8k- > >>> char buffer is required. > >>> 10-12 11:14:20.460:ERROR/log_tag(233):Errorparsing data > >>> org.json.JSONException: A JSONArray text must start with '[' at > >>> character 0 of > >> You may want to dump the value you are trying toparseasJSONto > >> LogCat, as it is not what you think it is. > > >> -- > >> Mark Murphy (a Commons > >> Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy > > >> Android 2.2 Programming Books:http://commonsware.com/books-Hide quoted > >> text - > > >> - Show quoted text -- Hide quoted text - > > - Show quoted text - -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en