我正在非常糟糕的时间试图找出我的代码中的错误来实现android应用程序和php服务器之间的连接。
以下是我在服务器上的php代码:
<?php
$json = file_get_contents('php://input');
$request = json_decode($json, true);
$response = array();
print "PHP HERE!";
// check for required fields
if (isset($request['Year']) && isset($request['Month']) && isset($request['Day_of_month']) && isset($request['Day_of_week']) && isset($request['Hour']) && isset($request['Minute'])) {
$Year= $request['Year'];
$Month= $request['Month'];
$Day_of_month= $request['Day_of_month'];
$Day_of_week= $request['Day_of_week'];
$Hour= $request['Hour'];
$Minute= $request['Minute'];
// include db connect class
require_once 'db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO date VALUES('$Year','$Month','$Day_of_month','$Day_of_week','$Hour','$Minute')");
$response["success"] = 1;
echo json_encode($response);
exit;
}
?>
我的客户端代码如下:
// Making HTTP request
try {
// Open connection to URL and perform POST request.
URL url1 = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();
urlConnection.setDoOutput(true); // Set Http method to POST
urlConnection.setDoInput(true);
urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
urlConnection.setRequestMethod("POST");
urlConnection.setChunkedStreamingMode(0); // Use default chunk size
// Write serialized JSON data to output stream.
OutputStreamWriter writer = new OutputStreamWriter(urlConnection.getOutputStream());
//OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
//BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write(params.toString());
writer.flush();
writer.close();
InputStream input = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
//BufferedReader reader = new BufferedReader(new InputStreamReader(
// is, "iso-8859-1"), 8);
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
jObj = new JSONObject(result.toString());
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// return JSON String
return jObj;
请感谢任何帮助。
答案 0 :(得分:0)
您没有打开连接
致电
<table id="rounded-corner" width=100%>
<tr>
<td>Nuotrauka</td>
<td>Pavadinimas</td>
<td>miestas</td>
<td>metai</td>
<td>kaina</td>
</tr>
<?
$url = "My Link";
$contents = file_get_contents($url);
preg_match_all("|<span class=\"ttitle2\">(.*?) </span>|U",$contents,$pavadinimas);
preg_match_all("|<span class=\"ttitle3\">(.*?)</span>|U",$contents,$miestas);
preg_match_all("|<span class=\"ttitle1\">(.*?)</span>|U",$contents,$metai);
preg_match_all("|<span class=\"ttitle1\" style='float: left;'>(.*?)<br />|U",$contents,$kaina);
preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/si", $contents, $img_link);
$output = "<tr><td><img src=$img_link></td><td>$pavadinimas</td><td>$miestas</td><td>$metai</td><td>$kaina</td></tr>";
print_r($output);
?>
</table>