当我直接通过浏览器访问PHP链接时,它会在数据库中创建一个空记录。但是,当我使用app发送数据时,它会失败。
爪哇:
protected String doInBackground(String... params) {
String reg_url = "http://www.minigameserver.square7.ch/register.php";
String user_name = params[0];
String user_ac = params[1];
String user_pw = params[2];
try {
URL url = new URL(reg_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
String data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"+
URLEncoder.encode("user_ac","UTF-8")+"="+URLEncoder.encode(user_ac,"UTF-8")+"&"+
URLEncoder.encode("user_pw","UTF-8")+"="+URLEncoder.encode(user_pw,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
httpURLConnection.disconnect();
return "done!";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "fail!";
}
当我运行应用程序时,按摩完成了!"显示。
所以,没有异常发生。但是,为什么没有新记录?
我看到有人厌倦了使用teset的用户名注册。他是否想知道是谁这样做我可以追查这个帖子。
当我尝试Arshak提供的代码时。出现以下错误。 E / Surface:getSlotFromBufferLocked:未知缓冲区:0x7f7d9eaad530
register.php:
<?php
require "db_connect.php";
$user_name = $_POST["user_name"];
$user_ac = $_POST["user_ac"];
$user_pw = $_POST["user_pw"];
$user_pw_enc = md5($user_pw);
$sql = "INSERT INTO user(user_name, user_ac, user_pw) VALUES ('$user_name', '$user_ac', '$user_pw_enc')";
$sql2 = "SELECT * FROM user WHERE user_name = '$user_name'";
$result2 = $conn->query($sql2);
$sql3 = "SELECT * FROM user WHERE user_ac = '$user_ac'";
$result3 = $conn->query($sql3);
if($result2->num_rows == 0 && $result3->num_rows == 0){
if ($conn->query($sql) === TRUE)
echo "Your account is created successfully!\nYou can login now!";
else
echo "Error: " . $sql . "<br>" . $conn->error;
}
else {
if($result2->num_rows > 0)
echo "The user name is used!<br>";
if($result3->num_rows > 0)
echo "The user account is used!<br>";
}
?>
答案 0 :(得分:1)
我已更新您的代码,看看数据是否已插入:
protected String doInBackground(String... params) {
String reg_url = "http://www.minigameserver.square7.ch/register.php";
String user_name = params[0];
String user_ac = params[1];
String user_pw = params[2];
try {
URL url = new URL(reg_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.connect();
DataOutputStream OS = new DataOutputStream(httpURLConnection.getOutputStream());
String data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"+
URLEncoder.encode("user_ac","UTF-8")+"="+URLEncoder.encode(user_ac,"UTF-8")+"&"+
URLEncoder.encode("user_pw","UTF-8")+"="+ URLEncoder.encode(user_pw,"UTF-8");
OS .writeBytes(data);
OS .flush();
OS.close();
int responseCode=httpURLConnection.getResponseCode();
String messageFromServer;
if (responseCode==200) {
messageFromServer=fromInputStream(httpURLConnection.getInputStream());
Log.v("RegistrationMessage ",messageFromServer);
httpURLConnection.disconnect();
return messageFromServer;
}
httpURLConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
return "fail!";
}
//Add this method inside your AsyncTask
public String fromInputStream(InputStream stream) throws IOException {
InputStreamReader streamReader = new
InputStreamReader(stream);
BufferedReader bufferedReader = new BufferedReader(streamReader);
StringBuilder builder = new StringBuilder();
String tempString = "";
while ((tempString = bufferedReader.readLine()) != null) {
builder.append(tempString);
}
return builder.toString();
}
答案 1 :(得分:0)
@Override
protected String doInBackground(String... params) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.minigameserver.square7.ch/register.php");
try {
//add data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("user_name", params[0]));
nameValuePairs.add(new BasicNameValuePair("user_ac", params[1]));
nameValuePairs.add(new BasicNameValuePair("user_pw", params[2]));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//execute http post
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
}
您可以使用Volley Android Networking Library发布您的数据。官方文件是https://developer.android.com/training/volley/simple.html。
答案 2 :(得分:0)
我尝试测试网址,是的,它正在运行。由于已经有帖子显示您的代码有一些修复,我只是给你一个提示。
首先,因为您不确定可能会出现什么异常。尽量使它一般。删除一个catch()
语句,然后使用:
catch(Exception e){
e.printStackTrace();
}
通过这种方式,所有异常都将被捕获,您可以轻松找到导致代码出现问题的原因。
接下来是,因为您正在与服务器通信,所以它将是一个请求响应过程。实际上,自从发送了一些请求后,你就做了正确的事,我能够收到回应。但为了提高效率,您的服务器也应该像布尔指示器那样做出响应。假设它已成功注册,它将响应1,如果没有,则返回0。然后您的移动应用程序将处理结果。
此外,您很可能无法获得该失败消息,因为它在try-catch之外。