If I run my php file manually I am getting the output as follows:
[{"org_id":"39575","orgname":"ARTHRITIS FOUNDATION - VIRGINIA CHAPTER","orgcity":"RICHMOND","orgstate":"VA"}]
The php code is as folows:
<html>
<body style="background-color:#33990f">
<?php
// connect include
require ("connect.php");
$query = "SELECT * FROM orgs WHERE org_id = 39575";
$result = mysql_query($query)or die(mysql_error());
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
while ($row = mysql_fetch_assoc($result))
{
$output[]=$row;
echo(json_encode($output));
}
/* Closes Connection to MySQL server */
mysql_close ($connect);
exit();
?>
</body></html>
If I access it using my Android code shown below I get the following errors:
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): Error parsing data org.json.JSONException: A JSONArray text must start with '[' at character 0 of
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<OrgItem> nameValuePairs = new ArrayList<OrgItem>();
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("graborginfo.php");
httppost.setEntity((HttpEntity) new UrlEncodedFormEntity((List<? extends NameValuePair>) nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in 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", "Error converting result "+e.toString());
}
//parse json data
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", "Error parsing data "+e.toString());
}
}
I am new to this so I am subject to beginners errors. Any help would be appreciated.
Thanks,
Capt Spaghetti