问题 - 解析数据到php和json

时间:2012-01-03 05:03:26

标签: php android json

我正在尝试将数据解析为php文件并使用jsons来检索数据。它会出错。    “解析数据时出错org.json.JSONException:字符0处的输入结束”   我究竟做错了什么?非常感谢任何帮助。

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

1 个答案:

答案 0 :(得分:2)

在PHP中,PHP代码mysql_query("Your query here")是一种方法。你忘记了括号。

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