我只是想在服务器上发布数据。 这不会给出错误,但不会插入数据。 我通过直接访问 php 页面使用 GET 方法进行检查,它可以工作,但是当我运行应用程序时,它不起作用。
PHP Script:
<?php
$conn=mysqli_connect("localhost","my_user","my_password","my_db");
$name=$_POST["name"];
$age=$_POST["age"];
$userName=$_POST["userName"];
$password=$_POST["password"];
$statement=mysqli_prepare($conn,"INSERT INTO User (name,age,UserName,password) VALUES (?,?,?,?)");
mysqli_stmt_bind_param($statement,"siss",$name,$age,$userName,$password);
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);
mysqli_close($conn);
?>
但是当我使用 GET 方法手动测试它时,例如 http://test.com?user=user1&age=11&userName=wer45&password=23ssds
and change the php scripts as :
<?php
$conn=mysqli_connect("localhost","my_user","my_password","my_db");
$name=$_GET["name"];
$age=$_GET["age"];
$userName=$_GET["userName"];
$password=$_GET["password"];
$statement=mysqli_prepare($conn,"INSERT INTO User (name,age,UserName,password) VALUES (?,?,?,?)");
mysqli_stmt_bind_param($statement,"siss",$name,$age,$userName,$password);
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);
mysqli_close($conn);
?>
上面的 GET 有效,任何人都可以检查下面的代码并帮助我找出问题所在。我无法跟踪,因为没有抛出错误。
public class storeUserDataAsyncTask extends AsyncTask<Void,Void,Void>{
User user;
GetUserCallBack userCallBack;
public storeUserDataAsyncTask(User user,GetUserCallBack userCallBack){
this.user=user;
this.userCallBack=userCallBack;
}
@Override
protected Void doInBackground(Void... params) {
HashMap<String,String> dataToSend=new HashMap<>();
dataToSend.put("name", user.name);
dataToSend.put("age",user.age+"");
dataToSend.put("userName",user.userName);
dataToSend.put("password",user.password);
HttpURLConnection httpURLConnection=null;
try {
URL url = new URL("http://nishantapp11.esy.es/Register.php");
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(10000);
httpURLConnection.setReadTimeout(10000);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
//httpURLConnection.setChunkedStreamingMode(0);
int serverResponseCode=httpURLConnection.getResponseCode();
if(serverResponseCode==HttpURLConnection.HTTP_OK){
}else{
Log.e("TAG","not ok");
}
OutputStreamWriter outputStreamWriter=new OutputStreamWriter(httpURLConnection.getOutputStream());
outputStreamWriter.write(getPostDataString(dataToSend));
outputStreamWriter.flush();
/* OutputStream outputStream=httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
bufferedWriter.write(getPostDataString(dataToSend));
bufferedWriter.flush();
bufferedWriter.close();*/
}catch (Exception e){
e.printStackTrace();
}
finally {
httpURLConnection.disconnect();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
progressDialog.dismiss();
userCallBack.done(null);
super.onPostExecute(aVoid);
}
private String getPostDataString(HashMap<String,String> params) throws UnsupportedEncodingException{
StringBuilder result=new StringBuilder();
boolean first =true;
for(HashMap.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"));
}
//Log.e("POST URL", result.toString());
return result.toString();
}
}
最佳答案
正在查看 sample HTTPURLConnection ,最后有一个 httpURLConnection.connect(); 我在你的代码中没有看到。
请注意,您的代码在异常的 catch 部分中有 httpURLConnection.disconnect();。
免责声明:假设您的代码完整并且我是正确的
如果您确实使用了wireshark,您将不会看到模拟器中没有任何流量。这就是为什么它是一个非常有用的工具。当您的代码没有错误或异常但只是缺少某些内容时,它可以提供信息。
关于java - 无法在 android 中的服务器 mysqli/Php 上发布数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35440053/