Partage
  • Partager sur Facebook
  • Partager sur Twitter

[Android] app login avec php et mysql

    6 avril 2013 à 20:46:55

    Bonjour a tous, j'ai un problem dans mon projet de fin d'étude. je dois créer une interface de login et je ne trouve pas comment le faire.

      j'ai un code source de cette app et json

    loginActivity :

    public class LoginActivity extends Activity {
        Button btnLogin;
        Button btnLinkToRegister;
        EditText inputEmail;
        EditText inputPassword;
        TextView loginErrorMsg;
     
        // JSON Response node names
        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";
     
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.login);
     
            // Importing all assets like buttons, text fields
            inputEmail = (EditText) findViewById(R.id.loginEmail);
            inputPassword = (EditText) findViewById(R.id.loginPassword);
            btnLogin = (Button) findViewById(R.id.btnLogin);
            btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
            loginErrorMsg = (TextView) findViewById(R.id.login_error);
     
            // Login button Click Event
            btnLogin.setOnClickListener(new View.OnClickListener() {
     
                public void onClick(View view) {
                    String email = inputEmail.getText().toString();
                    String password = inputPassword.getText().toString();
                    UserFunctions userFunction = new UserFunctions();
                    JSONObject json = userFunction.loginUser(email, password);
     
                    // check for login response
                    try {
                        if (json.getString(KEY_SUCCESS) != null) {
                            loginErrorMsg.setText("");
                            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(getApplicationContext());
                                JSONObject json_user = json.getJSONObject("user");
     
                                // Clear all previous data in database
                                userFunction.logoutUser(getApplicationContext());
                                db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));                       
     
                                // Launch Dashboard Screen
                                Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
     
                                // Close all views before launching Dashboard
                                dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(dashboard);
     
                                // Close Login Screen
                                finish();
                            }else{
                                // Error in login
                                loginErrorMsg.setText("Incorrect username/password");
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
     
            // Link to Register Screen
            btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
     
                public void onClick(View view) {
                    Intent i = new Intent(getApplicationContext(),
                            RegisterActivity.class);
                    startActivity(i);
                    finish();
                }
            });
        }
    }

    json :

    public class JSONParser {
     
        static InputStream is = null;
        static JSONObject jObj = null;
        static String json = "";
     
        // constructor
        public JSONParser() {
     
        }
     
        public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
     
            // Making HTTP request
            try {
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));
     
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
     
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
     
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "n");
                }
                is.close();
                json = sb.toString();
                Log.e("JSON", json);
            } catch (Exception e) {
                Log.e("Buffer Error", "Error converting result " + e.toString());
            }
     
            // try parse the string to a JSON object
            try {
                jObj = new JSONObject(json);
            } catch (JSONException e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }
     
            // return JSON String
            return jObj;
     
        }
    }

    mais je n'ai pas fiche php et xml  & merci


    • Partager sur Facebook
    • Partager sur Twitter

    [Android] app login avec php et mysql

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown