2014-06-17 106 views
1

我有一段代碼運行在異步任務與PHP腳本的支持。我只想檢查電子郵件ID是否存在於我的數據庫中。我想顯示敬酒:「電子郵件已存在」,如果它在數據庫中。否則另一個敬酒:「註冊成功」。我在下面給出的方式只是在其他條件下工作。我會如何實現這一目標?pease幫助我。需要在異步任務'onPostExecute'工作

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

回答

1

因爲你messageonPostExecute總是null所以儘量

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

postData方法應該返回String .ie改寫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(); 
       } 
       } 
+0

感謝您的快速回復。我試試你的答案。@ Nermeen @Giru Bhai – Fayisa

+0

@ user3588920歡迎,檢查並回復它是否正常工作? –

+0

waaaaaw !!!謝謝你,從我心底開始工作。 – Fayisa

0

因爲你總是在doInBackground返回null ... 它應該是這樣的:

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

所以你需要改變postData方法返回String

注意,你不需要調用onPostExecuterunOnUiThread,因爲它是在UI線程已經叫

+0

耶!它is.thankyou Nermeen。 – Fayisa