Bonjour tout le monde,
Je suis entréain de développer une application qui se connecte sur un serveur mysql, en utilisant JSON, j'ai commencé par créer mon webservice php et ça marche bien.
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 <?php // array for JSON response $response = array(); // check for required fields if (isset($_POST['num'])){ $numsim = $_POST['num']; // Connecting to mysql database $con = mysql_connect("localhost", "root", "") ; // Selecing database $db = mysql_select_db("test") ; // mysql inserting a new row $result=mysql_query("INSERT INTO backup(num) VALUES('$num')"); if ($result) { // successfully inserted into database $response["success"] = 1; $response["message"] = "successfully created."; // echoing JSON response echo json_encode($response); } else { // failed to insert row $response["success"] = 0; $response["message"] = "Oops! An error occurred."; // echoing JSON response echo json_encode($response); } } else { // required field is missing $response["success"] = 0; $response["message"] = "Required field(s) is missing"; // echoing JSON response echo json_encode($response); } ?>
Puis j'ai crée mon application android, quand j'essaie de me connecter sur le webservice, j'écris le code suivant :
la fonction getJSONFromUrl
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 private static String url = "http://localhost/android_connect/create_new_backup.php"; List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("num", num)); // getting JSON Object JSONObject json = jsonParser.getJSONFromUrl(url, params);
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 public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) { // Making HTTP request try { // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = null; httpPost = new HttpPost (url); Log.d("Create Response", httpPost.toString()); httpPost.setEntity(new UrlEncodedFormEntity(params)); Log.d("Create Response", httpPost.toString()); 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 { JSONArray jsonArray = new JSONArray(json); for(int i=0;i<jsonArray.length();i++) { jObj = jsonArray.getJSONObject(i); Log.i("testing", jObj.getString("tag")); Log.i("testing", jObj.getString("success")); Log.i("testing", jObj.getString("error")); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } // return JSON String return jObj; }
sachant que j'ai ajouté dans le manifest.xml la permission accés à l'internet
mais au moment ou j'essaie de connecter il m'affiche une erreur
unfortunately, application has stopped
d'aprés le LogCat, l'erreur c au niveau
HttpResponse httpResponse = httpClient.execute(httpPost);
pouvez vous m'aider à résoudre le probléme
j'ai vérifié bien que mon serveur wamp est online
j'ai beaucoup perdu du temps dans ce probléme
merci de m'aider
Partager