PHP脚本服务器端不会向Android应用程序返回任何内容


PHP-script server side not returning anything back to Android app

我正在为Android编写一个客户端-服务器应用程序,其中应用程序将POST发送到php脚本服务器端。此脚本搜索数据库并返回结果。但是我没有让它工作。客户端和服务器之间的通信采用 json 格式。

这是安卓活动中的功能。第二个功能是一个复制粘贴,我不能相信它,这是我在互联网上找到的:

private void testDB(){
    //Creating a json-representation of my query
    JSONObject query = new JSONObject();
    try {
        query.put("author", "SomeAuthor");
        query.put("title", "SomeTitle");
    } catch (JSONException e1) {
        e1.printStackTrace();
    }
    String json = query.toString();
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://myserver.com/search_books.php");

    try {           
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  
        nameValuePairs.add(new BasicNameValuePair("jsondata", json));  
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        InputStream in = response.getEntity().getContent();
        String results = convertStreamToString(in);
        Toast.makeText(getApplicationContext(), results, Toast.LENGTH_LONG).show();
    } catch (ClientProtocolException e) {
        System.out.println("ERROR (ClientProtocolException): " + e.toString());
    } catch (IOException e) {
        System.out.println("ERROR (IOException): " + e.toString());     
    } 
}
private String convertStreamToString(InputStream is) {
    /*
     * To convert the InputStream to String we use the BufferedReader.readLine()
     * method. We iterate until the BufferedReader return null which means
     * there's no more data to read. Each line will appended to a StringBuilder
     * and returned as String.
     */
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "'n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

这是php脚本:

<?php
//Get the information from the request (POST)
$jsonInput = $_POST['jsondata'];
$decoded = json_decode($jsonInput,true);
//Connect to the database
$connection = mysql_connect(localhost, username, password);
if (!$connection) {
    die("Could not connect to db: " . mysql_error());   
}
//Select the database to be used
mysql_select_db("muffin_books", $connection) or die(mysql_error());
//Construct the query
$sql = "SELECT * FROM Books WHERE author='" . $decoded['author'] . "' AND title='" . $decoded['title'] . "'";
//Make the query to the database with the connection
$query_results = mysql_query($sql, $connection); 
//If no results were returned
if (!$query_results) {
    die("Could not connect to db: ", mysql_error());
}
//Get the array from the results
$info = mysql_fetch_array($query_results, MYSQL_ASSOC);
//Return the data encoded in json
echo json_encode($info);
//Disconnect database
mysql_close($connection);
?>

问题是吐司只是空的。我 100% 确定该行存在于数据库中。myserver.com 不是我的实际服务器,在我的文件中,它在这里说了别的东西。用户名和密码也在我的代码中自定义。

提前感谢!

由于您要将 Strig 发送到服务器,我认为您需要将 SQL 查询修改为

$sql = "SELECT * FROM Books WHERE author=" . $decoded['author'] . " AND title=" .      $decoded['title'] . "";