Bonjour, je suis sur un projet dans lequel je récupère un JSON (de mon webservice) et je le parse en retournant un JSONArray.

J'ai des soucis de performance et cela plante sous android 4.0 (ne plante pas sur 2.3.3 mais est un peu long).

Pouvez vous m'aider quant a l'optimisation de mon code et pourquoi pas d'autres methodes pour recuperer un JSON dans un JSONArray.

Voici mon parser:
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
public class JSONParser 
{
 
    static InputStream is = null;
    static JSONObject jObj = null;
    static String jsonstr = "";
 
    public JSONParser() {}
 
    public JSONArray getJSONFromUrl(String url) 
    {
 
        // Creation de la requete HTTP
        try {
 
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
 
            HttpResponse httpResponse = httpClient.execute(httpGet);
            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();
            jsonstr = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        JSONArray jArray = null;
 
        // Parsing de la chaine pour la convertir en JSONArray
        try {
            jArray = new JSONArray(jsonstr);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing de Mon fichier: " + e.toString());
        }
 
        return jArray;
    }
}
Voici mon activite:

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
public class TabNewsJSONParsingActivity extends ListActivity 
{
 
	// url to make request
	private static String url = "http://www.example.com/GetEvents?zone=8";
 
	//JSON names
	private static final String TAG_content = "content";
	private static final String TAG_zone = "zone";
	private static final String	TAG_id = "id";
	private static final String	TAG_area = "area";
	private static final String	TAG_title = "title";
	private static final String	TAG_date = "date";
	private static final String TAG_author = "author";
 
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.onglet_news);
 
		// Hashmap for ListView
		ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();
 
		// Creating JSON Parser instance
		JSONParser jParser = new JSONParser();
 
		// getting JSON string from URL
		JSONArray json = jParser.getJSONFromUrl(url);
 
		try {
				for(int i=0; i < json.length(); i++)
				{
					JSONObject child = json.getJSONObject(i);
 
					String id = child.getString(TAG_id);
					String title = child.getString(TAG_title);
					String content = child.getString(TAG_content);
					String date = child.getString(TAG_date);
					String author = child.getString(TAG_author);
					String zone = child.getString(TAG_zone);
					String area = child.getString(TAG_area);
 
 
					// creating new HashMap
					HashMap<String, String> map = new HashMap<String, String>();
 
					// adding each child node to HashMap key => value
					map.put(TAG_content, content);
					map.put(TAG_title, title);
					map.put(TAG_author, author);
 
					// adding HashList to ArrayList
					newsList.add(map);
				}
			}
		catch (JSONException e) {
			e.printStackTrace();
		}
		/**
                 * Updating parsed JSON data into ListView
                 * */
		ListAdapter adapter = new SimpleAdapter(this, newsList,R.layout.onglet_news_listitem,new String[] { TAG_content, TAG_title, TAG_author }, new int[] {R.id.name, R.id.email, R.id.mobile });
		setListAdapter(adapter);
 
		// selecting single ListView item
		ListView lv = getListView();
 
		// Launching new screen on Selecting Single ListItem
		lv.setOnItemClickListener(new OnItemClickListener() 
		{
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
				// getting values from selected ListItem
				String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
				String cost = ((TextView) view.findViewById(R.id.email)).getText().toString();
				String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString();
 
				// Starting new intent
				Intent in = new Intent(getApplicationContext(), TabNewsSingleMenuItemActivity.class);
				in.putExtra(TAG_content, name);
				in.putExtra(TAG_title, cost);
				in.putExtra(TAG_author, description);
				startActivity(in);
 
			}
		});
	}
 
	public boolean onOptionsItemSelected(MenuItem item) 
	{	
	   //On regarde quel item a été cliqué grâce à son id et on déclenche une action
	   switch (item.getItemId()) 
	   {
	      case R.id.credits:
	    	 //pop up
	        Toast.makeText(TabNewsJSONParsingActivity.this, "Un delire", Toast.LENGTH_SHORT).show();
	         return true;
	      case R.id.quitter:
	         //Pour fermer l'application il suffit de faire finish()
	         finish();
	         return true;
	   }
	 return false;  
}
}