Go Back   Android Forums > Android Development > Application Development
Application Development Dev Lounge for the Coder Folks
Gamers - Check out our new sister sites!
Nintendo Wii U!    |    OUYA - $99 Android System!

test: Reply
 
LinkBack Thread Tools
Old 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
Default 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

farha is offline  
Reply With Quote
Sponsors
Old November 16th, 2010, 10:25 AM   #2 (permalink)
Junior Member
 
Join Date: Nov 2010
Posts: 15
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 1 Time in 1 Post
Default

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.
droidotheplains is offline  
Reply With Quote
Old November 16th, 2010, 12:24 PM   #3 (permalink)
Junior Member
 
Join Date: Oct 2010
Posts: 17
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 4 Times in 4 Posts
Default

$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);
aguywithathing is offline  
Last edited by aguywithathing; November 16th, 2010 at 12:27 PM.
Reply With Quote
Old July 12th, 2011, 05:50 AM   #4 (permalink)
New Member
 
Join Date: Jul 2011
Posts: 1
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
prateekagrawal is offline  
Reply With Quote
Old October 11th, 2012, 09:51 AM   #5 (permalink)
New Member
 
Join Date: Oct 2012
Posts: 2
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by prateekagrawal View Post
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
glamlafp is offline  
Reply With Quote
Reply
Tags
json, json parsing, parse, php


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 02:51 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.