- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 php 脚本的支持下,我有一段代码在 Async Task 中运行。我只想检查我的数据库中是否存在电子邮件 ID。我想显示 toast :“电子邮件已存在”(如果在数据库中)。否则另一个 toast :“注册成功”。我在下面给出的方法仅适用于其他条件。我将如何实现这一目标?请帮助我。
这是我的.java
class SummaryAsyncTask extends AsyncTask<Void, Boolean, String> {
private void postData(String fname,String lname,String email,String pass,String cpass,String mobile) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xxxx/registration.php");
try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);
nameValuePairs.add(new BasicNameValuePair("fname", fname));
nameValuePairs.add(new BasicNameValuePair("lname", lname));
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("pass", pass));
nameValuePairs.add(new BasicNameValuePair("cpass", cpass));
nameValuePairs.add(new BasicNameValuePair("mobile", mobile));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
httpEntity = response.getEntity();
InputStream is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8); // From here you can extract the data that you get from your php file..
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line + "\n");
}
is.close();
json = new JSONObject(builder.toString());// Here you are converting again the string into json object. So that you can get values with the help of keys that you send from the php side.
message = json.getString("message");
}
catch(Exception e)
{
Log.e("log_tag", "Error: "+e.toString());
}
}
@Override
protected void onPostExecute(final String message) {
super.onPostExecute(message);
runOnUiThread(new Runnable() {
public void run() {
if (message!=null) {
Toast.makeText(Response.this,"Email Id already exist", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(Response.this, "Thank You!You have registered successfully", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected String doInBackground(Void... params) {
postData(first,last,eid,pword,conp,mob);
return null;
}
}
我的 .php 文件
<?php
$con=mysql_connect("localhost","root","");
$sel=mysql_select_db("xxx",$con);
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$email=$_POST['email'];
$pass=$_POST['pass'];
$cpass=$_POST['cpass'];
$mobile=$_POST['mobile'];
$query=mysql_query("SELECT * FROM user WHERE email='$email'");
if(mysql_num_rows($query)>0){
$response["message"] = "Email Id already exist.";
echo json_encode($response);
}
else{
mysql_query("insert into member(fname,lname,email,pass,cpass,mobile) values ('$fname','$lname', '$email', '$pass','$cpass','$mobile')", $con);
}
?>
最佳答案
因为您在onPostExecute
中的message
总是null
所以试试
protected String doInBackground(Void... params) {
return postData(first,last,eid,pword,conp,mob);
}
和 postData
方法应该返回 String
。即将 postData
函数重写为
private String postData(String fname,String lname,String email,String pass,String cpass,String mobile) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xxxx/registration.php");
try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);
nameValuePairs.add(new BasicNameValuePair("fname", fname));
nameValuePairs.add(new BasicNameValuePair("lname", lname));
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("pass", pass));
nameValuePairs.add(new BasicNameValuePair("cpass", cpass));
nameValuePairs.add(new BasicNameValuePair("mobile", mobile));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
httpEntity = response.getEntity();
InputStream is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8); // From here you can extract the data that you get from your php file..
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line + "\n");
}
is.close();
json = new JSONObject(builder.toString());// Here you are converting again the string into json object. So that you can get values with the help of keys that you send from the php side.
message = json.getString("message");
}
catch(Exception e)
{
Log.e("log_tag", "Error: "+e.toString());
}
return message;
}
和onPostExcute
方法一样
@Override
protected void onPostExecute(final String message) {
super.onPostExecute(message);
if (message!=null) {
Toast.makeText(Response.this,"Email Id already exist", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(Response.this, "Thank You!You have registered successfully", Toast.LENGTH_LONG).show();
}
}
关于android - 需要在异步任务中工作 'onPostExecute',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24258865/
当我使用 new GiveMeJson2.execute(ref_id).get() 调用异步类时, 我的应用程序进入 doInBackground,创建一个 JSON 字符串并返回它。 此时,我认为
我正在尝试在后台下载一些文件,为此我使用了下载管理器并制作了此方法: /*** media download ***/ public long mediaDownload(ArrayList
我在尝试使用 onPostExecute 来触发这段代码时遇到问题,我对 java 尤其是多线程很陌生,所以如果我正在做的事情完全错误,请随时纠正我...需要学习. 所以问题是这样的: public
以下代码是我出于 Intent 而添加的代码,结果是从我的login.php 返回的内容。但它无法比较成功与否。在任何情况下它都不会进入 if 循环。但它在警报对话框中显示“登录成功”。 @Overr
字符串 descript 的内容显示在 logcat 中,没有任何问题,但不会显示在 OnPostExecute 对话框中的 TextView 上。这是一个空指针异常。我不知道为什么,因为 episo
当我运行我的应用程序时,onPostExecute似乎没有被调用。它没有像应有的那样填充到 UI 中。另外,关于DoInBackground ,任何超过 for 循环的日志消息: for (int i
我有一个单独的类(不是 Activity)来管理图像及其属性。 在这个类(Image.class)中,我还创建了一个 AsynkTask 来从网络下载位图。一切正常,除了 AsyncTask 继续运行
在 php 脚本的支持下,我有一段代码在 Async Task 中运行。我只想检查我的数据库中是否存在电子邮件 ID。我想显示 toast :“电子邮件已存在”(如果在数据库中)。否则另一个 toas
所以我创建了一个 web 服务调用类,它调用我在 AsyncTask 上扩展的 web 服务: public class WebServiceLoginCall extends AsyncTask{
我有一个扩展 AsyncTask 的类,它旨在用作我的应用程序的通用任务管理器类。 问题:奇怪的行为是进度对话框出现,但从未被关闭。 我确信每个任务实例都会调用 onPostExecute(),因为任
更新,请见下文。 如何将我的类 LocationData 和我的 ArrayList listOfObjects 返回到 onPostExecute()?我想在我的 UI 中使用它,现在它在 Asyn
我正在尝试获取 onPostExecute()完成 doInBackground() 后调用的方法方法,但它没有被调用。我该如何解决这个问题? 我已经调试过了,可以看到ArrayList data里面
我有一些控制权,因此我做了一些线程。我想用 asycntask 将一个屏幕的加载栏显示到另一个屏幕。我的问题是加载栏正在快速显示和消失,然后在没有加载的情况下等待同一个屏幕很多bar.And 一段时间
我在 Android 应用程序上做一个登录系统,当我输入错误的帐户时,onPostExecute 方法返回正确的失败消息,但是当我输入一个现有帐户并且登录正确后它仍然是相同的失败消息,我不明白为什么!
public static class TestTask extends AsyncTask { private String stacktrace; public TestTask
我有一个运行良好的 AsyncTask 类,但在这个类中,我在 onPreExecute 中有一个 ProgressDialog,它工作正常,但我需要在完成进程对话框关闭时,我将代码放在 onPost
我不知道如何找出这个空指针异常发生的位置。 按照 Google Play 的建议,崩溃只在用户的物理设备上发生过一次(到目前为止)——我无法在调试环境中重现它。 Google Play 给我的 Sta
我尝试使用此函数返回对象数组: public static JSONEvent[] invokeFunction(String funName, String requestContent) {
我有一个 Activity (RecipiesActivity),通过单击我的应用程序主 Activity 的“下一步”按钮打开。 在 RecipiesActivity onCreate 中,我想在
我有一个看起来像这样的类: internal class MyClass : AsyncTask>>> { protected override void OnPostExec
我是一名优秀的程序员,十分优秀!