TRS
TRS

Reputation: 490

Issues-parsing data to the php and jsons

i'm trying to parsing data to the php file and retrive data using jsons.It gives error. "Error parsing data org.json.JSONException: End of input at character 0 of" What am I doing wrong? Many thanks for any help.

     public static final String KEY_121 ="http://10.0.2.2/reports.php"; //i use my real ip here


  private String getServerData(String returnString) 
    {

        InputStream is = null;

        String result = "";

        //the year data to send
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair ("typ",SpinnerValue));
        nameValuePairs.add(new BasicNameValuePair ("Nam",AutoCompleteValue));
        nameValuePairs.add(new BasicNameValuePair ("frm","2011-09-28"));
        nameValuePairs.add(new BasicNameValuePair ("to","2011-10-09"));


        //http post

        try 
        {

         HttpClient httpclient = new DefaultHttpClient();
         HttpPost httppost = new HttpPost(KEY_121);
         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
         HttpResponse response = httpclient.execute(httppost);
         HttpEntity entity = response.getEntity();
         is = entity.getContent();

        }
        catch(Exception e)
        {
                Log.e("log_tag","Error in http connection"+e.toString());
        }

        //convert response to string
        try
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");


            // ADD THIS
            Log.i("log_tag","Line reads: " + line);

            }

            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);

                        //  String id = json_data.getString("id");
                        name =json_data.getString("Name");
                        Path= json_data.getString("Path");
                        Toast.makeText (getApplicationContext(), Path,                       
                            Toast.LENGTH_SHORT).show ();

                        //Get an output to the screen

                        returnString += "\n\t" + jArray.getJSONObject(i);
                }

        }
        catch(JSONException e)
        {
                Log.e("log_tag", "Error parsing data "+e.toString());
        }
        return returnString;




    }   



           **PHP FILE**


            <?php

              mysql_connect("localhost:3306","root","");

              mysql_select_db("infosoft");

              $q=mysql_query"SELECT * FROM report WHERE  FrmDte>='".$_REQUEST["frm"]."' AND   

              ToDte<='".$_REQUEST["to"]."' AND Name='".$_REQUEST["Nam"]."' AND 
              Type='".$_REQUEST["typ"]."' ");



               while($e=mysql_fetch_assoc($q))

               $output[]=$e;

               print(json_encode($output));

               mysql_close();
                 ?>

Upvotes: 0

Views: 408

Answers (1)

Robert Wilson
Robert Wilson

Reputation: 669

In you PHP code mysql_query("Your query here") is a method. You forgot the parenthesis.

$output = array();
while($e = mysql_fetch_assoc($q)) {
    $output[] = $e;
}
echo json_encode($output);

Upvotes: 2

Related Questions