JSON Android PHP如何更正错误或如何显示数据

JSON Android PHP如何更正错误或如何显示数据,php,android,mysql,json,database,Php,Android,Mysql,Json,Database,我是JSON和PHP新手,对Android知之甚少,我测试了以下内容: 我的java代码: ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("QUERY", "SELECT tblWeather.humidity,tblWeather.rainfall,tblWeat

我是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
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";
      }

      ?>
我需要帮助。。谢谢

但是,当我使用
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}
有人能告诉我出了什么问题吗?

输出

{"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.
}

for(int i=0;i您的站点值被包装在tblStations数组中。(tblStations.Station)您能给我一些关于这个包装器对象的示例或教程吗?或者您对如何显示数据有什么建议吗?非常感谢..doInBackground()中有一个错误方法。同时粘贴AsyncTask类公共类DoPOST扩展AsyncTask{Context mContext=null;String strNameToSearch=“”;//结果数据字符串站;String-Tenture;Exception-Exception=null;DoPOST(Context-Context,String-nameToSearch){mContext=Context;strNameToSearch=nameToSearch;}@Override protected Boolean doInBackground(字符串…arg0){try{这是异步任务class@user3315040看看我的答案,它对你有用。我有一个问题…这行:String station=jsonObject.getString(“station”);应该是:String station=curr.getString(“station”)我试过了,先生,最管用的是这个字符串诱惑=curr.getString(“温度”);
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.
}