我正在创建我的第一个 android,它将从注册表单中获取数据并将其发送到 php 后端。
php 后端将获取数据并保存在数据库中,并给出一个 jason 编码的消息,告诉它是否成功。
现在我想消除重复用户名的可能性,所以当 android 应用程序将数据发送到 php 后端时,我将首先检查,如果它是重复的,我将抛出这样的错误消息
$response["error"] = true;
$response["message"] = "Username Already taken";
echoRespnse(400,$response);
成功后后端会发送这样的内容
$response["error"] = false;
$response["message"] = "Successfuly Registered";
echoRespnse(201,$response);
如何让 Android 应用程序读取此信息并了解用户是已创建还是发生了错误。
我当前的 Android signup.java 代码如下所示
public void post() throws UnsupportedEncodingException
{
// Get user defined values
uname = username.getText().toString();
email = mail.getText().toString();
password = pass.getText().toString();
confirmpass = cpass.getText().toString();
phone = phn.getText().toString();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.rgbpallete.in/led/api/signup");
if (password.equals(confirmpass)) {
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("uname", uname));
nameValuePairs.add(new BasicNameValuePair("pass", password));
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("phone", phone));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
//Code to check if user was successfully created
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else
{
Toast.makeText(getBaseContext(), "Password mismatch", Toast.LENGTH_SHORT).show();
//Reset password fields
pass.setText("");
cpass.setText("");
}
}
最佳答案
我想您需要帮助来获取和读取您的服务提供的 JSON 数据,对吗? 在您的 SignUp Activity 中创建一个 AsyncTask,因为您无法在主线程上执行此操作。
private class DownloadOperation extends AsyncTask<Void, Void, String> {
String uname = "";
String email = "";
String password = "";
String confirmpass = "";
String phone = "";
@Override
protected void onPreExecute() {
super.onPreExecute();
// Get user defined values
uname = username.getText().toString();
email = mail.getText().toString();
password = pass.getText().toString();
confirmpass = cpass.getText().toString();
phone = phn.getText().toString();
}
@Override
protected String doInBackground(Void... params) {
String response = "";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.rgbpallete.in/led/api/signup");
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("uname", uname));
nameValuePairs.add(new BasicNameValuePair("pass", password));
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("phone", phone));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
httpResponse = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
return response;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.d("tag", "Result:\n" + result);
}}
然后调用
// Calling async task to get json
new DownloadOperation().execute();
然后您将在您的控制台上看到打印的 json 字符串 :)
使用响应字符串获取 JSONObject:
JSONObject jsonObj = new JSONObject(STRING);
希望对您有所帮助。
关于java - 在android中创建用户时检查用户名可用性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27553154/