Salut j'ai suivi un tuto pour récupérer des données d'un serveur PHP au format json tout se passe très bien mais j'ai vraiment du mal a comprendre pourquoi on doit passer par toutes une procédure pour retrouver la donnée json comme dans ce 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
 
		public static long getOnlineAlarm(long taskID) {
 
		long alarm = -1;
 
		String result = null;
		InputStream is = null;
 
		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
 
		nameValuePairs.add(new BasicNameValuePair("taskID", String.valueOf(taskID)));
 
		try {
			HttpClient httpclient = InternetUtilities.getHttpclient();
			HttpPost httppost = new HttpPost(InternetUtilities.ressources.getString(R.string.serveraddress)+"/getOnlineAlarm.php");
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
 
			HttpEntity entity = response.getEntity();
			is = entity.getContent();
 
		} catch (Exception e) {
			Log.i("taghttppost", "" + e.toString());
 
		}
 
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "UTF-8"));
 
			StringBuilder sb = new StringBuilder();
 
			String line = null;
 
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
 
			is.close();
 
			result = sb.toString();
		} catch (Exception e) {
			Log.i("tagconvertstr", "" + e.toString());
		}
 
		try {
			Log.i("tagconvertstr", "[" + result + "]");
 
			JSONObject jObj = new JSONObject(result);
 
			alarm = jObj.getLong("alarm");
			return alarm;
 
		} catch (JSONException e) {
			Log.i("tagjsonexp", "" + e.toString());
		} catch (ParseException e) {
			Log.i("tagjsonpars", "" + e.toString());
		}
 
		return alarm;
	}
et par exemple niveau PHP je met juste ca :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
$alarm = -2;
echo(json_encode(array("alarm" => $alarm)));
Je ne comprends pas l'intérêt de toutes ces lignes :
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
 
 
HttpEntity entity = response.getEntity();
			is = entity.getContent();
 
		} catch (Exception e) {
			Log.i("taghttppost", "" + e.toString());
 
		}
 
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "UTF-8"));
 
			StringBuilder sb = new StringBuilder();
 
			String line = null;
 
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
 
			is.close();
 
			result = sb.toString();
		} catch (Exception e) {
			Log.i("tagconvertstr", "" + e.toString());
		}
on pourrait pas juste récupérer la variable json directement à partir de :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
 
JSONObject jObj = new JSONObject(result);
 
			alarm = jObj.getLong("alarm");
			return alarm;
???