android - 需要在异步任务中工作 'onPostExecute'

标签 android android-asynctask

在 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/

相关文章:

java - 显示ListView时,使用item内容查询服务器

java - 关于任务 onPostExecute

安卓市场应用链接

java - Android:如何从图库/相机更改 OpenGLES 纹理数据/位图

android - 查看 Recycler 的模型查看项目

android - Espresso 测试被后台线程阻塞。应用程序未空闲异常 : "AppNotIdleException."

android - 没有网络调用时如何使用async task查看后台数据

java - 使用相同的 ffmpeg 命令在不同环境(AWS Lambda 与本地)中的不同结果

Android - 已编译的 APK 版本更改

java - AsyncTask doInBackground() 方法中的空指针异常