Je suis un grand novice en android et je suis en train de faire une petite application pour se loger. J'ai trouvé un bon tutoriel ici, mais il ne fonctionne pas parce que je dois le faire en asynk task.J'ai essayé de faire quelques modifications mais rien ne fonctionne .voilà mon code:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import com.example.androidhive.library.DatabaseHandler; import com.example.androidhive.library.UserFunctions; 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) { new LogTask().execute(); } }); // Link to Register Screen btnLinkToRegister.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent i = new Intent(getApplicationContext(),RegisterActivity.class); startActivity(i); finish(); } }); } private class LogTask extends AsyncTask<String, String, String> { protected String doInBackground(String... args) { String email = inputEmail.getText().toString(); String password = inputPassword.getText().toString(); UserFunctions userFunction = new UserFunctions(); Log.d("Button", "Login"); 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)); }else{ // Error in login return null; } } } catch (JSONException e) { e.printStackTrace(); } return "done"; } protected void onPostExecute(String file_url) { if(file_url==null) { loginErrorMsg.setText("Incorrect username/password"); } else if(file_url.equals("done")) { Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class); dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(dashboard); finish(); } } } }
Partager