即使数据存在于db - Android中,json也会返回空数组

时间:2015-07-25 06:21:59

标签: php android json

我从woocommerce数据库中检索产品详细信息,这是非常庞大的。

Json第一次返回产品数组。但是,当我重新加载同一页面时,它就是空的。

错误日志:

第一次

Json回复

$compile('<button ng-click="navigate(locations['+i+'][5])">Navigate</button>')($scope);
第二次

Json回复

07-25 10:49:08.010  24512-24726/com.example.images E/result﹕ {"file":[{"ID":"1","product_name":"Cake1"},{"ID":"2","product_name":"Cake2"},{"ID":"3","product_name":"Cake3"},{"ID":"4","product_name":"Cake4"},{"ID":"5","product_name":" Cake5"},{"ID":"6","product_name":"Cake6"},{"ID":"7","product_name":"Cake7"},{"ID":"8","product_name":"Cake8"},{"ID":"9","product_name":"cake9"},{"ID":"10","product_name":"cake10"},{"ID":"11","product_name":"Blackforest cake"},{"ID":"12","product_name":"Chocolate truffle cake"},{"ID":"454","product_name":"star cake"},{"ID":"67","product_name":"Vanila cake"},{"ID":"787","product_name":"Butterscotch cake"},{"ID":"121","product_name":"Half kg cake"},{"ID":"787","product_name":"Strawberry Cake"},{"ID":"77","product_name":"Vanilla Cake"},{"ID":"712","product_name":"cake 12"},{"ID":"714","product_name":"Half kg chocolate cake"},{"ID":"888","product_name":"Half kg Pineapple cake"},{"ID":"777","product_name":"BARBIE DOLL CAKE"},{"ID":"45","product_name":"BLUE CAKE"},{"ID":"50","product_name":"CHOCOLATE CREAM CAKE"},{"ID":"499","product_name":"ROUND BUTTER SCOTCH CAKE"},{"ID":"44","product_name":"PRINT CAKE"},{"ID":"58","product_name":"TRUFFLE CAKE"},{"ID":"862","product_name":"CREAMY CAKE"},{"ID":"86","product_name":"FRUIT CAKE"},{"ID":"487","product_name":"FULL CHOCOLATE CAKE"},{"ID":"872","product_name":"STRAWBERRY CAKE"},{"ID":"7845","product_name":"HEART CHOCOLATE CAKE"},{"ID":"5545","product_name":"VANILLA CHOCOLATE CAKE"}]}

我试图在php中打印错误(使用了所有可能的语句),但它没有在日志中打印任何内容。

尝试添加07-25 10:49:24.290 24512-24679/com.example.images E/result﹕ {"file":[]} 但没有用。

php代码:

Content-type

分贝/ db_connection.php:

<?php  
    ini_set('display_errors',1);  
    error_reporting(E_ALL);

    include "db/db_connection.php";
    $term_id = $_POST['term_id'];
     //   header('Content-Type: application/json');

  $result =  $dbConnection->prepare("SELECT  `ID`,`post_title`,`post_content`,`post_excerpt`,`guid` FROM wp_posts WHERE post_type='product' and post_status='publish' and ID IN(SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN(?) and term_taxonomy_id IN(SELECT term_taxonomy_id FROM wp_term_taxonomy
            where taxonomy='product_cat'))");

  $result->execute(array($term_id));    
  $i=0;
  $file = array();

  if($result->rowCount()) 
  {
        while($fileName = $result->fetch()) 
      {
          $file[$i]['ID']= $fileName['ID']; 
          $file[$i]['product_name']= $fileName['post_title']; 
          $i++; 
      }
  } 
    echo json_encode(array('file'=>$file));

?>

Android接收json:

<?php
   $host ="localhost";
    $db_name ="woocommerce";
    $db_username = "root";
    $db_password = "";  

    $dbConnection = new PDO("mysql:dbname=$db_name;host=$host;charset=utf8", "$db_username", "$db_password");   
    $dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 
    $dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

?>

编辑1:

Http请求 -

try
            {
                Log.e("result",result);
                JSONObject json_data = new JSONObject(result);
                String data = json_data.getString("file");
                JSONArray json_data1 = new JSONArray(data);

                for (int i = 0; i < json_data1.length(); i++)
                {
                    json_data = json_data1.getJSONObject(i);

                    String product_name= json_data.getString("product_name");
                    String Id= json_data.getString("ID"); 
                }
            }
            catch(Exception e)
            {
                Log.e("Fail 3", e.toString());
            }

编辑2:

ArrayList<NameValuePair> nameValuePairs = new ArrayList<>(); nameValuePairs.add(new BasicNameValuePair("term_id", ""+term_id)); try { HttpClient httpclient = new DefaultHttpClient(); System.setProperty("http.keepAlive", "false"); HttpPost httppost = new HttpPost("http://192.000.0.000/Prabs/Products.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); Log.e("pass 1", "connection success "); } catch(Exception e) { Log.e("Fail 1", e.toString()); } 被分配到term_id,因为我通过0发送了term_id

现在SharedPreference通过意图传递,即使错误仍然存​​在:(

LOG

term_id

1 个答案:

答案 0 :(得分:0)

您可能遇到params.add()问题,请确保每次都执行params.add()并确保其通过term_id

以某种方式,您的应用程序未在第二个请求中正确设置term_id,您只需要找到一种方法来获取其值并正确传递它。