如何解决从PHP服务器获取空响应的问题

时间:2019-05-15 13:04:55

标签: java php android api

我当时正在开发一个应用程序,该应用程序使用PHP作为中间人从在线服务器上获取数据。但是现在突然之间,我不断收到来自PHP函数的空响应。而且我知道我的PHP服务器正在工作,因为我尝试使用另一个PHP文件访问该函数,并且工作得很好,

它工作了一段时间,我不知道该怎么做。

这是获取数据的异步任务

@Override
                    public void run() {
                        try {
                            Log.d("Thread" + cat, "Thread Running");
                            URLConnection conn = url.openConnection();
                            Log.d("Thread" + cat, "opened connection");
                            conn.setDoOutput(true);
                            conn.setConnectTimeout(15000);
                            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                            Log.d("Thread" + cat, "Output Stream Got");
                            wr.write(data);
                            Log.d("Thread" + cat, "Written: " + data);
                            wr.flush();
                            Log.d("Thread" + cat, "Flushed");
                            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                            Log.d("Thread" + cat, "Input Stream Loaded");
                            String line = null;
                            while ((line = reader.readLine()) != null) {
                                sb.append(line);
                            }
                            Log.d("Thread"  + cat, "Done:" + sb.toString());
                        } catch (Exception ex) {
                            Log.d("ERROR" + cat, ex.toString());
                            srl.setRefreshing(false);
                            success = false;
                        }
                    }
                });

她是php函数

<?php
    require 'connect_db.php';
    $category = $_POST['cat'];
    $query = "SELECT * FROM  articles WHERE category LIKE '" . $category . "' ORDER BY ID DESC LIMIT 25";
    $jsonData = array();
    $jsonTempData = array();
    if ($query_run = mysqli_query($conn, $query)) {

            while($query_row = mysqli_fetch_array($query_run)) {
                $id = $query_row['ID'];
                $title = $query_row['title'];
                $desc = $query_row['descrip'];
                $content = $query_row['content'];
                $picture = $query_row['picture'];
                $time = $query_row['time'];
                $jsonTempData = array();
                $jsonTempData['ID'] = $id;
                $jsonTempData['title'] = $title;
                $jsonTempData['desc'] = $desc;
                $jsonTempData['content'] = $content;
                $jsonTempData['picture'] = $picture;
                $jsonTempData['time'] = $time;
                $jsonData[] = $jsonTempData;

            }
        }  else {
            echo "\n Query Failed! <br>" . $conn->error;
            die;
        }
    $outputArr = array();
    $outputArr['android'] = $jsonData;
    print_r(json_encode($outputArr));    
?>

应该返回一个JSON对象,但我得到一个空字符串

1 个答案:

答案 0 :(得分:-1)

您可以在json_encode之前检查数组大小

if (count($outputArr)>0){
     print_r(json_encode($outputArr)); 
 }else{
  print(json_encode(['response'=>"notfind"]));
}