Take this out of your php file:
<html>
<body style="background-color:#33990f">
And this too:
</body></html>
Your php code needs to print out the JSON data array "as is".
The way you have it, it's wrapped with html tags. HTML tags are for web
browsers, and are not part of JSON.
This error should go away then:
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
-- Kostya
12.10.2010 23:51, Capt Spaghetti пишет:
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 follows:
=====================
<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));
}
/* Close 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
ANDROID Code
===============
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());
}
}
The LOGCAT output for the error is 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): Error parsing data
org.json.JSONException: A JSONArray text must start with '[' at
character 0 of
I am new to this so I am subject to beginners errors. Any help would
be appreciated.
Thanks,
Capt Spaghetti
--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com
--
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