November 13th, 2010, 11:24 AM
|
#1 (permalink)
|
|
New Member
Thread Author (OP)
Join Date: Nov 2010
Posts: 1
Device(s):
Carrier: Not Provided
Thanks: 0
Thanked 0 Times in 0 Posts
|
Parsing data using JSON and PHP but receiving 'Error Parsing data org.json.exception'
Hello,
I am rather new to coding for android applications - in fact i'm
pretty new to this all.
I am trying to link to my database using json and php (Yes, I have
seen the previous posts about the other parsing error - but it just
doesn't seem to help me - followed it to the tee).
My PHP Code looks as follows:
<?php
$con = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("RentalDatabase", $con);
header('Content-type: application/json');
$result = mysql_query("SELECT * FROM Customer");
while ($row = mysql_fetch_array($result))
{
echo json_encode($row, JSON_FORCE_OBJECT);
}
?>
And my android code looks as follows:
public class MobileDVDRental extends Activity
{
String result = "";
private Reader in;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.i("log_tag", "got here1");
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new
BasicNameValuePair("Membership_No","1000000001"));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/
json.php");
httppost.setEntity(new
UrlEncodedFormEntity(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());
}
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());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","Membership_No:
"+json_data.getInt("Membership_No")
+ ", First_Name:
"+json_data.getString("First_Name")
+ ", Last_Name:
"+json_data.getString("Last_Name")
+ ", Title:"+json_data.getString("Title:" )
);
}
}
catch(JSONException e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
}
setContentView(R.layout.main);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
public void BufferedReader(Reader in) {
this.in = in;
}
}
The only log error I get is:
11-10 18:08:24.910: ERROR/log_tag(650): Error parsing data
org.json.JSONException: End of input at character 0 of
I would appreciate any feedback at all.
Thanks
|
|
|