【问题标题】:Issues-parsing data to the php and jsons将数据解析到 php 和 jsons 的问题
【发布时间】:2012-01-03 05:03:26
【问题描述】:

我正在尝试将数据解析到 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();
                 ?>

【问题讨论】:

    标签: php android json


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-05-29
      • 1970-01-01
      • 1970-01-01
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多