Bonjour à tous,
Je développe une application Android qui interroge une base de donnée MySQL, j'ai donc fait un web service qui renvoi les réponses au format json grâce à vos bons conseils.
Le Json ressemble à ceci :
J'ai une classe qui recupère le Json et le convertit en Objet UnArticle :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 ["Concours DEsign","D\u00e9roulement des \u00e9preuves","Test BLoc"]
Ma classe UnArticle est celle-ci :
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 package com.example.myipsa; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import com.google.gson.Gson; import android.util.Log; public class ServerAccess { private Gson gson; //Convert an inputstream to a string public static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { Log.e("PHP Client","Error : "+e.getMessage()); } finally { try { is.close(); } catch (IOException e1) { Log.e("PHP Client","Error : "+e1.getMessage()); } } return sb.toString(); } //Get the response form the server as an object public Object getResponseObject(ArrayList Parameters,Class c) { try{ //Create a HTTP Client HttpClient httpclient = new DefaultHttpClient(); //Create and object to Post values to the server //The url is specified in the Constants class to increase modifiability HttpPost httppost = new HttpPost(Constants.SERVICE_URL); //Set the attributes to be posted as Parameters httppost.setEntity(new UrlEncodedFormEntity(Parameters)); //Execute the post and get the response HttpResponse response = httpclient.execute(httppost); //Get the response as ans entity HttpEntity entity = response.getEntity(); //Get the content of the response as a stream InputStream stream=entity.getContent(); //Convert the stream to a GSON object String result= convertStreamToString(stream); //Log.e("MyIPSA", result); gson = new Gson(); //Convert the respose string to a object of the given type //Here Object class is used so that we can use the same method to get any //class's object as response Object responseObject=gson.fromJson(result, c); //Return the object return responseObject; }catch(Exception e){ Log.e("MyIPSA", "Error in http connection"+e.toString()); return null; } } //Get the employees public UnArticle[] getTitreArticle() { //Create the arraylist to set the parameters of the post ArrayList<BasicNameValuePair> parameters=new ArrayList<BasicNameValuePair>(); //Set the method name parameters.add(new BasicNameValuePair("method","getTitreArticle")); //Get the array as the result UnArticle[] o= (UnArticle[])getResponseObject(parameters,UnArticle[].class); return o; } }
Mais la fonction "gson.fromJson(result, c)" me renvoi un :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 package com.example.myipsa; public class UnArticle { public String nom; }
Expected BEGIN_OBJECT but was STRING
Alors que si j'ai bien compris elle est censé prendre un string en entré et donnée un objet en sortit.
Merci d'avance pour votre aide précieuse
Partager