2013-04-17 57 views
0

我創建一個AsyncTask來登錄用戶到服務器。登錄工作正常,但如果他離開空字段的用戶可以登錄..我怎樣才能改變這樣的空白登錄也是錯誤login..any幫助將是不錯的感謝AsyncTask用mysql登錄

package com.app.app; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 
import com.app.app.library.DatabaseHandler; 
import com.app.app.library.JSONParser; 
import com.app.app.library.UserFunctions; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONException; 
import org.json.JSONObject; 

import com.app.app.DashboardActivity; 
import com.app.app.R; 
import com.app.app.R.id; 

import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.AsyncTask; 
import android.preference.PreferenceManager; 
import android.util.Log; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class LoginTask extends AsyncTask<String, Void, Integer> { 

    private ProgressDialog progressDialog; 
    private LoginActivity activity; 
    private int id = -1; 
    private com.app.app.library.JSONParser jsonParser; 
    private static String loginURL = "http://10.0.2.2/android/"; 
    private static String registerURL = "http://10.0.2.2/android/"; 
    private static String KEY_SUCCESS = "success"; 
    private static String KEY_ERROR = "error"; 
    private static String KEY_ERROR_MSG = "error_msg"; 
    private static String KEY_UID = "uid"; 
    private static String KEY_NAME = "name"; 
    private static String KEY_EMAIL = "email"; 
    private static String KEY_CREATED_AT = "created_at"; 
    private int responseCode = 0; 

    public LoginTask(LoginActivity activity, ProgressDialog progressDialog) 
    { 
     this.activity = activity; 
     this.progressDialog = progressDialog; 
    } 

    @Override 
    protected void onPreExecute() 
    { 
     progressDialog.show(); 
    } 

    protected Integer doInBackground(String... arg0) { 
     EditText userName = (EditText)activity.findViewById(R.id.loginEmail); 
     EditText passwordEdit = (EditText)activity.findViewById(R.id.loginPassword); 
     String email = userName.getText().toString(); 
     String password = passwordEdit.getText().toString(); 
     UserFunctions userFunction = new UserFunctions(); 
     JSONObject json = userFunction.loginUser(email, password); 

     // check for login response 
     try { 
      if (json.getString(KEY_SUCCESS) != null) { 
       String res = json.getString(KEY_SUCCESS); 

       if(Integer.parseInt(res) == 1){ 
        //user successfully logged in 
        // Store user details in SQLite Database 
        DatabaseHandler db = new DatabaseHandler(activity.getApplicationContext()); 
        JSONObject json_user = json.getJSONObject("user"); 
        //Log.v("name", json_user.getString(KEY_NAME)); 
        // Clear all previous data in database 
        userFunction.logoutUser(activity.getApplicationContext()); 
        db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), 
          json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));       

        responseCode = 1; 
        // Close Login Screen 
        //finish(); 

       }else{ 
        responseCode = 0; 
        // Error in login 
       } 
      } 

     } catch (NullPointerException e) { 
      e.printStackTrace(); 

     } 
     catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     return responseCode; 
    } 

    @Override 
    protected void onPostExecute(Integer responseCode) 
    { 
     EditText userName = (EditText)activity.findViewById(R.id.loginEmail); 
     EditText passwordEdit = (EditText)activity.findViewById(R.id.loginPassword); 


     if (responseCode == 1) { 
      progressDialog.dismiss(); 
      Intent i = new Intent(); 
      i.setClass(activity.getApplicationContext(), DashboardActivity.class); 
      activity.startActivity(i); 
      //activity.loginReport(responseCode); 
     } 
     else { 
      progressDialog.dismiss(); 
      activity.showLoginError(responseCode); 

     } 

    } 
} 
+0

'loginUser'方法的代碼在哪裏?在用戶名/電子郵件和密碼上清理輸入... – hovanessyan

回答

0

檢查這樣

String email = userName.getText().toString(); 
String password = passwordEdit.getText().toString(); 

if(email.length()!=0 && password.length()!=0) 
{ 

// do stuff 
} 
+0

同意,但這種邏輯會更好地把'loginUser'方法內的第一件事。 – hovanessyan

+0

@hovanessyan是的......你是對的...... – Pragnani

+0

是的,我把你的IF消化..我沒有記住它..感謝:) – user2241725