アプリケーションがPOSTをphp-scriptサーバー側に送信するAndroid用のクライアントサーバーアプリケーションをプログラミングしています。このスクリプトはデータベースを検索し、結果を返します。しかし、私はそれを機能させていません。クライアントとサーバー間の通信は json 形式です。
これは、android アクティビティの関数です。2 番目の機能はコピー アンド ペーストです。
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 は私の実際のサーバーではありません。私のファイルでは、ここに別のことが書かれています。ユーザー名とパスワードも私のコードでカスタマイズされています。
前もって感謝します!