需要在异步任务中使用'onPostExecute'

时间:2014-06-17 08:15:27

标签: android android-asynctask

我有一段代码在Async Task中运行,支持php脚本。我只是想检查我的数据库中的电子邮件ID是否存在。我想要显示吐司:"电子邮件已经存在"如果是在数据库中。否则是另一个吐司:"注册成功"。我在下面给出的方式只是处理其他条件。我会实现这个吗?请帮助我。

这是我的.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);
}
?>

2 个答案:

答案 0 :(得分:1)

因为message中的onPostExecute始终为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();
                }
               }

答案 1 :(得分:0)

因为您总是在null中返回doInBackground ... 它应该是:

protected String doInBackground(Void... params) {
      return postData(first,last,eid,pword,conp,mob);
}

因此,您需要更改postData方法以返回String

注意,您无需在runOnUiThread中调用onPostExecute,因为它已在UI线程中调用