您在下面的PHP和JAVA文件中看到的是一个完整的(虽然简单)注册功能。一切正常。
现在的问题是,
如何检测是否已经使用了用户名字段,以及如何让java方知道这一点,以便我可以给用户一个正确的消息。
ORIGINAL register.php:
<?php
define('HOST','X');
define('USER','X');
define('PASS','X');
define('DB','X');
$con = mysqli_connect(HOST,USER,PASS,DB);
$name = $_POST['name'];
$pass = $_POST['pass'];
$sql = "insert into tbl_user (username,password) values ('$name','$pass')";
if(mysqli_query($con,$sql)){
echo 'success';
}
else{
echo 'failure';
}
mysqli_close($con);
?>
register.java的一部分:
private void insertToDatabase(String name, String pass) {
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String name = params[0];
String pass = params[1];
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("pass", pass));
boolean exists = false;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"http://calisapp.esy.es/register.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return "";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Intent intent = new Intent(Register.this, MainActivity.class);
startActivity(intent);
Toast.makeText(Register.this, "Registered successfully", Toast.LENGTH_LONG).show();
finish();
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(name, pass);
}
答案 0 :(得分:1)
private void insertToDatabase(String name, String pass) {
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String name = params[0];
String pass = params[1];
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("pass", pass));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost( "http://calisapp.esy.es/register.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
InputStream entity = response.getEntity().getContent();
InputStreamReader inputStream = new InputStreamReader(entity);
BufferedReader bufferedReader = new BufferedReader(inputStream);
StringBuilder stringBuilder = new StringBuilder();
String bufferedStrChunk = null;
while ((bufferedStrChunk = bufferedReader.readLine()) != null) {
stringBuilder.append(bufferedStrChunk);
}
return stringBuilder.toString();
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return "";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONObject jo = new JSONObject(result);
String status = jo.getString("status");
if (status.equals("0")) {
editTextName.requestFocus();
editTextName.setError("Username already exists.");
} else if (status.equals("1")) {
Intent intent = new Intent(Register.this, MainActivity.class);
startActivity(intent);
Toast.makeText(Register.this, "Registered successfully", Toast.LENGTH_LONG).show();
finish();
}else if (status.equals("2")) {
// failed to register
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(name, pass);
}
答案 1 :(得分:0)
try {
JSONObject jo = new JSONObject(result);
String status = jo.getString("status");
if (status.equals("0")) {
// show the error, username already exists
} else if (status.equals("1")) {
// registration successfull
}else if (status.equals("2")) {
// failed to register
}
} catch (JSONException e) {
e.printStackTrace();
}
在java端,onpostexecute方法中的result参数
sudo apt-get install libcairo2-dev
sudo apt-get install libpango1.0-dev