【问题标题】:JSON Android PHP how to correct error or HOW to DISPLAY the dataJSON Android PHP 如何纠正错误或如何显示数据
【发布时间】:2014-03-21 06:20:03
【问题描述】:

我是 JSON 和 PHP 方面的新手,我对 Android 知之甚少,我对此进行了测试:

我的 java 代码:

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("QUERY", "SELECT tblWeather.humidity,tblWeather.rainfall,tblWeather.wDate,tblWeather.wTime,tblStations.IMEI, tblStations.Station, tblWeather.IMEI,tblWeather.msgID,tblWeather.tempture FROM tblStations INNER JOIN tblWeather ON tblStations.IMEI = tblWeather.IMEI WHERE tblStations.IMEI= 013226007289958 ORDER BY msgID DESC LIMIT 0,1")); 
            //Add more parameters as necessary

            //Create the HTTP request
            HttpParams httpParameters = new BasicHttpParams();

            //Setup timeouts
            HttpConnectionParams.setConnectionTimeout(httpParameters, 150000);
            HttpConnectionParams.setSoTimeout(httpParameters, 150000);          

            HttpClient httpclient = new DefaultHttpClient(httpParameters);
            HttpPost httppost = new HttpPost("http://210.14.5.179/aws/getLocal2.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));        
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            String result = EntityUtils.toString(entity);

            // Create a JSON object from the request response
            JSONObject jsonObject = new JSONObject(result);

            //Retrieve the data from the JSON object
            Station = jsonObject.getString("Station");
            tempture = jsonObject.getString("tblWeather.tempture");

我有这个 PHP 代码,它连接到从上述代码中获取 QUERY 语句的 Web 服务器。

PHP 代码:

<?php
if (isset ( $_POST ["QUERY"] ) && $_POST ["QUERY"] != "") {
    /*
     * Following code will list all the products
     */
    $QUERY = $_POST ["QUERY"];

    // array for JSON response
    $response = array ();

    $con = mysqli_connect ( "localhost", "user", "pass", "db" );
    $QUERY = mysqli_real_escape_string ( $con, $QUERY );

    // get all products from products table
    $result = mysqli_query ( $con, $QUERY ) or die ( mysql_error () );

    // check for empty result
    if (mysqli_num_rows ( $result ) > 0) {
        // looping through all results
        // products node
        $response ["tblStations"] = array ();

        while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) {
            // temp user array
            $data = array ();
            // $data ["msgID"] = $row ["msgID"];
            $data ["Station"] = $row ["Station"];
            // $product ["IMEI"] = $row ["IMEI"];
            $data ["Date"] = $row ["wDate"];
            $data ["Time"] = $row ["wTime"];
            $data ["temperature"] = $row ["tempture"];
            $data ["humidity"] = $row ["humidity"];
            $data ["rainfall"] = $row ["rainfall"];

            // push single product into final response array
            array_push ( $response ["tblStations"], $data );
        }
        // success
        $response ["success"] = 1;

        // echoing JSON response
        echo json_encode ( $response );
    } else {
        // no products found
        $response ["success"] = 0;
        $response ["message"] = "No products found";

        // echo no users JSON

        echo json_encode ( $response );
       }
       } else {
    echo "Could not complete query. Missing parameter";
      }

      ?>

我不断收到下面列出的这个错误是我的日志:

LOG:

    03-21 14:00:56.552: E/ClientServerDemo(383): Error:
    03-21 14:00:56.552: E/ClientServerDemo(383): org.json.JSONException: No value for        Station
    03-21 14:00:56.552: E/ClientServerDemo(383):    at   org.json.JSONObject.get(JSONObject.java:354)
    03-21 14:00:56.552: E/ClientServerDemo(383):    at org.json.JSONObject.getString(JSONObject.java:510)
    03-21 14:00:56.552: E/ClientServerDemo(383):    at com.oadigital.clientserverrestdemo.MainActivity$DoPOST.doInBackground(MainActivity.java:134)
    03-21 14:00:56.552: E/ClientServerDemo(383):    at com.oadigital.clientserverrestdemo.MainActivity$DoPOST.doInBackground(MainActivity.java:1)
    03-21 14:00:56.552: E/ClientServerDemo(383):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
    03-21 14:00:56.552: E/ClientServerDemo(383):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    03-21 14:00:56.552: E/ClientServerDemo(383):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    03-21 14:00:56.552: E/ClientServerDemo(383):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    03-21 14:00:56.552: E/ClientServerDemo(383):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
    03-21 14:00:56.552: E/ClientServerDemo(383):    at java.lang.Thread.run(Thread.java:1019)

我需要帮助..谢谢

但是当我使用POTMAN-REST 客户端时,它会给我这样的输出:

{"tblStations":[{"Station":"AWS01 - USeP","Date":"2014-03-19","Time":"16:15:01","temperature":"26.9","humidity":"86.4","rainfall":"1.63"}],"success":1}

谁能告诉我哪里错了?

【问题讨论】:

  • 您的 Station 值包含在 tblStations 数组中。 (tblStations.Station)
  • 你能给我一些关于这个包装对象的示例或教程吗?或者你对我如何显示数据有什么建议吗?非常感谢..
  • doInBackground() 方法有错误。还要粘贴 AsyncTask 类
  • 公共类 DoPOST 扩展 AsyncTask{ Context mContext = null;字符串 strNameToSearch = ""; //结果数据 String Station;字符串诱惑;异常异常 = null; DoPOST(Context context, String nameToSearch){ mContext = context; strNameToSearch = nameToSearch; } @Override protected Boolean doInBackground(String... arg0) { try{ 这是异步任务类
  • @user3315040 看看我的答案,它会为你工作。

标签: php android mysql json database


【解决方案1】:

输出

{"tblStations":[{"Station":"AWS01 - USeP","Date":"2014-03-19","Time":"16:15:01","temperature":"26.9","humidity":"86.4","rainfall":"1.63"}],"success":1}

这是初始化 JSON 解析器的方法:

//Retrieve the data from the JSON object
JSONObject jsonObject = new JSONObject(result);

这会将整个字符串作为 Json 对象提供给您。从那里,拉出一个单独的数组作为 JsonArray,如下所示:

JSONArray jsonArray = jsonObject.getJSONArray("tblStations");

要访问每个“站和温度”,您可以使用循环逻辑:

for(int i=0;i<jsonArray.length();i++)
{
JSONObject curr = jsonArray.getJSONObject(i);

String station = curr.getString("Station");
String tempture = curr.getString("temperature");

//Do stuff with the Station and temperature String here
//Add it to a list, print it out, etc.
}

【讨论】:

  • 我有一个问题...这一行: String station = jsonObject.getString("Station");应该是 :String station = curr.getString("Station");??
  • 我试过了,先生,有效的是这个 String tempture = curr.getString("temperature");
猜你喜欢
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-09
  • 1970-01-01
  • 1970-01-01
  • 2015-02-25
  • 2010-10-18
相关资源
最近更新 更多