-1
這是我的php腳本。我一直在嘗試使用PHP腳本將數據發佈到MySQL服務器。數據已成功添加到寄存器功能中。我的網址連接也沒有問題。什麼似乎是問題?即使在java端有數據,也無法使用httpurlconnection將數據發佈到數據庫
<?php
include ('classes/functions.php');
if(isset($_POST['product_id'])){
$product_id = $_POST['product_id'];
$insert_order = "INSERT INTO orders (product_id) values
('$product_id')";
$run_order = mysqli_query($con, $insert_order);
if($run_order){
echo"<script>alert('Order Successfully!')</script>";
}
}
?>
這是我的java腳本。
public final class confirm extends Context {
private static final String REGISTER_URL =
"http://192.168.43.214/apexStore2/confirm.php";
public static void register(String id) {
class RegisterUser extends AsyncTask<String, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
@Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<>();
data.put("product_id",params[0]);
String result = sendPost(REGISTER_URL,data);
return result;
}
}
RegisterUser ru = new RegisterUser();
ru.execute(id);
}
public static String sendPost(String requestURL,
HashMap<String, String> postDataParams) {
URL url;
String response = " ";
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", "" +
Integer.toString(getPostDataString(postDataParams).
getBytes().length));
conn.setRequestProperty("Content-Language", "en-US");
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
System.out.println("wei" + getPostDataString(postDataParams));
Log.d(getPostDataString(postDataParams), "onCreate()");
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br=new BufferedReader(new
InputStreamReader(conn.getInputStream()));
response = br.readLine();
}
else {
response="Error Registering";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
private static String getPostDataString(HashMap<String, String> params)
throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, String> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
im使用手機。這個IP被測試沒有問題,因爲有其他功能使用這個IP,它的工作原理。 Android日誌中的 –
只顯示我傳遞的值。但它不顯示成功 –
您可以發佈輸出嗎?服務器也可以從客戶端獲得任何連接,或者什麼都沒有?我沒有看到任何不適合你的代碼的地方,但是如果沒有關於輸出的更多信息或者更多關於行爲的信息,就很難進行調試。 –