这是我的php脚本。我一直在尝试使用php脚本将数据发布到mysql服务器。数据已成功添加到寄存器函数中。我的url连接也没有问题。问题出在哪里?
<?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();
}
您是在模拟器上运行还是在设备上运行?无论哪种方式,我认为确认设备在正确的网络上以查看您的内部ip 192.168.43.214都会很有帮助,您可能需要使用外部ip,尤其是如果您通过数据连接进行连接。除此之外,android日志的输出是什么?