1

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

这是android活动中的功能。第二个功能是复制粘贴,我不相信它,我在 internetz 上找到了它:

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 不是我的实际服务器,在我的文件中它在此处说明了其他内容。用户名和密码也在我的代码中自定义。

提前致谢!

4

1 回答 1

0

因为您正在向服务器发送 srig 我认为您需要将您的 sql 查询修改为

$sql = "SELECT * FROM Books WHERE author=" . $decoded['author'] . " AND title=" .      $decoded['title'] . "";
于 2012-04-25T10:19:17.343 回答