Bonjour à tous,
J'ai un problème avec une application Android que je développe.
Après de multiples recherches sur Internet et aucune des solutions testées ne m'a apporté satisfaction.
Voici le dossier contenant les fonctions utilisateurs (qui je dois l'avouer et tirer en majeur partie de tuto) :
Toutes les fonctions marches sauf celle entourée par les commentaires.
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 package fr.xquizit.lib; import java.util.ArrayList; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.json.JSONObject; import android.content.Context; import android.util.Log; public class UserFunctions { private JSONParser jsonParser; private static String loginURL = "http://10.0.2.2/android_login_api/"; private static String registerURL = "http://10.0.2.2/android_login_api/"; private static String enregistre_motURL = "http://10.0.2.2/android_login_api/"; private static String login_tag = "login"; private static String register_tag = "register"; private static String enregistre_mot = "enregistrer"; // constructor public UserFunctions(){ jsonParser = new JSONParser(); } /**** NOUVELLE FONCTION ****/ public JSONObject enregistreMot(String mot) { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("tag", enregistre_mot)); params.add(new BasicNameValuePair("mot",mot)); JSONObject json = jsonParser.getJSONFromUrl(enregistre_motURL, params); return json; } /*************************/ /** * function make Login Request * @param email * @param password * */ public JSONObject loginUser(String email, String password){ // Building Parameters List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("tag", login_tag)); params.add(new BasicNameValuePair("email", email)); params.add(new BasicNameValuePair("password", password)); JSONObject json = jsonParser.getJSONFromUrl(loginURL, params); // return json // Log.e("JSON", json.toString()); return json; } /** * function make Login Request * @param name * @param email * @param password * */ public JSONObject registerUser(String name, String email, String password){ // Building Parameters List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("tag", register_tag)); params.add(new BasicNameValuePair("name", name)); params.add(new BasicNameValuePair("email", email)); params.add(new BasicNameValuePair("password", password)); // getting JSON Object JSONObject json = jsonParser.getJSONFromUrl(registerURL, params); // return json return json; } /** * Function get Login status * */ public boolean isUserLoggedIn(Context context){ DatabaseHandler db = new DatabaseHandler(context); int count = db.getRowCount(); if(count > 0){ // user logged in return true; } return false; } /** * Function to logout user * Reset Database * */ public boolean logoutUser(Context context){ DatabaseHandler db = new DatabaseHandler(context); db.resetTables(); return true; } }
Maintenant le fichier php appelé par la fonction :
Donc je renvoi le code que j'encode en JSON.
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 <?php /** * File to handle all API requests * Accepts GET and POST * * Each request will be identified by TAG * Response will be JSON data /** * check for POST request */ if (isset($_POST['tag']) && $_POST['tag'] != '') { // get tag $tag = $_POST['tag']; // include db handler require_once 'include/DB_Functions.php'; $db = new DB_Functions(); // response Array $response = array("tag" => $tag, "success" => 0, "error" => 0); // check for tag type if ($tag == 'login') { // Request type is check Login $email = $_POST['email']; $password = $_POST['password']; // check for user $user = $db->getUserByEmailAndPassword($email, $password); if ($user != false) { // user found // echo json with success = 1 $response["success"] = 1; $response["uid"] = $user["unique_id"]; $response["user"]["name"] = $user["name"]; $response["user"]["email"] = $user["email"]; $response["user"]["created_at"] = $user["created_at"]; $response["user"]["updated_at"] = $user["updated_at"]; echo json_encode($response); } else { // user not found // echo json with error = 1 $response["error"] = 1; $response["error_msg"] = "Incorrect email or password!"; echo json_encode($response); } } if ($tag == 'register') { // Request type is Register new user $name = $_POST['name']; $email = $_POST['email']; $password = $_POST['password']; // check if user is already existed if ($db->isUserExisted($email)) { // user is already existed - error response $response["error"] = 2; $response["error_msg"] = "User already existed"; echo json_encode($response); } else { // store user $user = $db->storeUser($name, $email, $password); if ($user) { // user stored successfully $response["success"] = 1; $response["uid"] = $user["unique_id"]; $response["user"]["name"] = $user["name"]; $response["user"]["email"] = $user["email"]; $response["user"]["created_at"] = $user["created_at"]; $response["user"]["updated_at"] = $user["updated_at"]; echo json_encode($response); } else { // user failed to store $response["error"] = 1; $response["error_msg"] = "Error occured in Registartion"; echo json_encode($response); } } } else { echo "Invalid Request"; } /***** NOUVELLE FONCTION ****/ if($tag == 'enregistrer') { $response["success"] = 1; $response["first"]["elm1"] = "Trucs"; $response["first"]["elm2"] = "Bidouille"; echo json_encode($response); } else { echo "Invalid Request"; } /*************************/ } else { echo "Access Denied"; } ?>
Le problème est que lors de l’exécution du programme j'ai 2 erreurs.
La première : Invalid Request{"tag":"enregistrer","success":1,"error":0,"first":{"elm1":"Trucs","elm2":"Bidouille"}} qui semble presque "normale" car elle est présente lors de l’exécution de toutes les fonctions
La deuxième : Error parsing data org.json.JSONException: Value Invalid of type java.lang.String cannot be converted to JSONObject qui celle là n'apparait que sur cette fonction.
Voilà, si quelqu'un avez une solution ou une explication concernant cette erreur se serez bien aimable.
Merci d'avance
Partager