1. Check out our companion app, Forums for Android! Download from Google Play

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

Discussion in 'Android Development' started by farha, Nov 13, 2010.

  1. farha

    farha New Member
    Thread Starter
    5

    Nov 13, 2010
    1
    0
    5
    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
     

    Advertisement

  2. droidotheplains

    16

    Nov 11, 2010
    15
    1
    16
    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.
     
  3. aguywithathing

    16

    Oct 12, 2010
    17
    5
    16
    $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);
     
  4. prateekagrawal

    prateekagrawal New Member
    5

    Jul 12, 2011
    1
    0
    5
    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
     
  5. glamlafp

    glamlafp New Member
    5

    Oct 11, 2012
    2
    0
    5
    And i'm having also this one:

    Buffer Error - Error Converting Result java.long.nullpointerException
     

Share This Page

Loading...