- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建我的第一个 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/
是否可以使用 JHipster 5(带有 Spring Boot 2)?我注意到在发行说明中提到了 JHipster 5 的工作,但看不到任何关于如何安装/运行 5 的“预览”版本的信息。 最佳答案
使用多个显示器时,有没有办法拆分 Xcode 界面? 我有 2 台显示器,但仍然没有找到一种简单的方法在超过 1 台显示器上使用它(左侧的 Storyboard,右侧的代码) - 就像在 Androi
我正在尝试处理 uwp 应用程序中的 xml,我添加了引用和用法: using System.Xml; using System.Xml.XPath; 但是 private XmlDocument d
JSunit 真的是 Junit 到 Javascript 的直接端口吗?所以我听说过,但我想知道为什么我的公司没有使用它。我的公司有很多聪明人,所以如果我们不使用它,我想知道 Jsunit 作为 j
当使用原始 TCP 套接字时,需要进行消息帧化,如 here 所述,带有长度前缀或分隔符。 我今天遇到了“SOCK_SEQPACKET”套接字选项,它显然可以几乎透明地进行消息框架。 目前“SOCK_
我在一个小型开发小组工作。我们正在构建和改进我们的产品。 半年前我们无法考虑更高的特性,比如可用性,因为我们的产品有很多问题。许多错误、高技术债务、低性能和其他问题使我们无法专注于可用性。 随着时间的
是否可以通过编程方式控制@RestController来启用或禁用它?我不想只在每个 @RequestMapping 方法中编写代码来执行某种 if (!enabled) { return 404Ex
azure 的 blob 在被新版本覆盖时是否可供下载? 根据我使用 Cloud Storage Studio 的测试,下载被阻止,直到覆盖完成,但是我的测试来自同一台计算机,因此我无法确定这是正确的
我一直认为(但出于某种原因从未实践过)动态生成且仅包含一个项目的下拉菜单应该自动选择该项目。这与我观察到的典型方法相反,即在顶部创建一个空白条目,您仍然必须与菜单交互以进行单个可用选择。 例如,当我登
我正在为我的兄弟编写一个基于 Web 的小实用程序,他需要合并 CSV 文件中的列。我知道这样的事情肯定存在于某个地方,但这很大程度上是因为这是一个有趣的小练习。 无论如何,我正在尝试找出最好/最简洁
如您所知,pinterest api 现在似乎已关闭。( api.pinterest.com )在此站点中:http://tijn.bo.lt/pinterest-api ,它表示只读访问仍然有效。到
我想为公寓可用性应用程序制作日历 View 。我的想法是从 MySQL 数据库获取日期范围(不可用日期)并将其显示在日历 View 中。我的日历 View 应该有 3 种不同的样式: 可用日期 不可用
我究竟应该如何配置我的广播接收器以使我的应用程序响应 wifi 可用性的变化。不过,当应用程序未运行时,我并不真正关心对此使用react。 我在这里试图完成的是,当应用程序运行时,我需要使本地数据库与
在我的网络应用程序中,我有 parent 和保姆。 保姆将声明工作时间(时间间隔),该工作时间将存储在工作时间表中。 家长也会每隔一段时间表达自己的需求(暂时不需要存储)。 所以我设计了这样的可用性表
概览 我希望在 CloudKit 无法访问时收到通知。 CloudKit 可能无法访问的可能场景: CloudKit 服务器不可访问 互联网连接不可用。 迄今为止所做的尝试: 观察 CKAccount
好的,我有我为核心数据创建的类 LoginPass.h 然后我有头等舱 FirstClass.h 然后我需要在 SecondClass 中使用这些类,我在其中使用 @class 声明它们。头文件 Se
例如 linux kernel-2.6.32-279 没有在/usr/include/asm-generic/socket.h 中定义 SO_REUSEPORT 但 kernel-2.6.32-431
我正在尝试安装的工具说: MySQL server data directory needs to reside on an LVM volume 如何检查我的数据目录是否在 LVM 上? 最佳答案
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Programmatically find device support GPS or not? 如何检查设
我听说汽车行业有一种叫做 MISRA C 的东西。 其他高可靠性/可用性/安全性行业的相关标准是什么,例如 空间 飞机 银行/金融 汽车 医疗 国防/军事 ??? -亚当 最佳答案 查看戈达德太空飞行
我是一名优秀的程序员,十分优秀!