我想通过http post添加新的支持数据。我得到一个错误响应代码是500.我检查代码和PHP文件我不能看到任何问题。而且我没有完成代码如何在解决500错误后得到结果。
NewSupport.java
try{
URL url = new URL("myurl/db_newSupport.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("uniq", "UNIQ")
.appendQueryParameter("data", "DATA")
.appendQueryParameter("date", "DATE");
String query = builder.build().getEncodedQuery();
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
}
}
catch(Exception e){
e.printStackTrace();
}
db_newSupport.php
$response = array();
if (isset($_POST['uniq']) && isset($_POST['data']) && isset($_POST['date'])) {
$uniq = $_POST['uniq'];
$data = $_POST['data'];
$date = $_POST['date'];
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$result= mysql_query("INSERT INTO new(uniq, data, date) VALUES('$uniq', '$data', '$date')");
if ($result) {
$response["code"] = 1;
$response["info"] = "successfully";
echo json_encode($response);
} else {
$response["code"] = 0;
$response["info"] = "error";
echo json_encode($response);
}
} else {
$response["code"] = 99;
$response["info"] = "error";
echo json_encode($response);
}
?>
答案 0 :(得分:3)
这是服务器端问题。
将POST方法更改为GET以检查结果是否是任何浏览器以简化debuggind。
3.在此处写入结果日志。
看看这个:https://stackoverflow.com/a/9711595/1979882
并且:https://stackoverflow.com/a/9717056/1979882
简而言之,执行此chmod -R 755 /usr/local/apache/htdocs/
答案 1 :(得分:1)
500 响应代码表示服务器端崩溃。您应该查看服务器日志以获取崩溃详细信息。
答案 2 :(得分:0)
500表示内部服务器错误。
你给出了URL的相对路径。尝试给出绝对路径,例如" http://<您的域名> /db_newSupport.php"。