gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:21:38 25 4
gpt4 key购买 nike

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

这是android Activity 中的函数。第二个功能是复制粘贴,我不能相信它是我在互联网上找到的:

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);
?>

问题是 Toast 是空的。我 100% 确定该行存在于数据库中。myserver.com 不是我的实际服务器,在我的文件中它还提到了其他内容。用户名和密码也在我的代码中自定义。

提前致谢!

最佳答案

由于您正在向服务器发送字符串,我认为您需要将 sql 查询修改为

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

关于java - PHP 脚本服务器端不向 Android 应用程序返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10313607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com