• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Parsing data using JSON and PHP but receiving 'Error Parsing data org.json.exception'

farha

Lurker
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
 
You're beginning to parse your data before you have it. You will have to either have your php complete the file and place it in an accessible drectory on your server, generate a url and redirect -- or finish the download on your client then parse.
 
$result = mysql_query("SELECT * FROM Customer");
while ($row = mysql_fetch_array($result))
echo json_encode($row, JSON_FORCE_OBJECT);

your throwing out multiple json objects, from a quick look that's the most obvious

therefore the string is not a valid json object, try:

$result = mysql_query("SELECT * FROM Customer");
while ($row = mysql_fetch_array($result))
$array[] = $row;

echo json_encode( $array, JSON_FORCE_OBJECT);
 
hello friend,
I am getting the same error..
Error parsing data org.json.JSONException: End of input at character 0 of

How did u resolve your error???
Tel me...
Thanks
 
hello friend,
I am getting the same error..
Error parsing data org.json.JSONException: End of input at character 0 of

How did u resolve your error???
Tel me...
Thanks

And i'm having also this one:

Buffer Error - Error Converting Result java.long.nullpointerException
 
Back
Top Bottom