etud21 Posted May 19, 2011 Share Posted May 19, 2011 salut tout le monde, salut chpil ;) mon probleme consiste à recuperer un fichier JSON dans mon application et afficher mes info dans list view je n'ai aucune erreur en code sauf que j'ai force close...voiçi mon code et priere de m'aider... public class mespharmacies extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listplaceholder); ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); JSONObject json = JSONfunctions.getJSONfromURL("mon url"); try{ JSONArray pharmacie = json.getJSONArray("pharmacie"); for(int i=0;i<pharmacie.length();i++){ HashMap<String, String> map = new HashMap<String, String>(); JSONObject e = pharmacie.getJSONObject(i); map.put("id", String.valueOf(i)); map.put("nomph", "name:" + e.getString("nomph")); map.put("lat", "latti: " + e.getString ("lat")); mylist.add(map); } }catch(JSONException e) { Log.e("log_tag", "Error parsing data "+e.toString()); } ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main1, new String[] { "nomph", "lat" }, new int[] { R.id.item_title, R.id.item_subtitle }); setListAdapter(adapter); final ListView lv = getListView(); lv.setTextFilterEnabled(true); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { @SuppressWarnings("unchecked") HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position); Toast.makeText(mespharmacies.this, "ID '" + o.get("id") + "'was clicked.", Toast.LENGTH_SHORT).show(); } }); } } ma classe JSONfunctions public class JSONfunctions { public static JSONObject getJSONfromURL(String url){ InputStream is = null; String result = ""; JSONObject jArray = null; //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); }catch(Exception e){ Log.e("log_tag", "Error in http connection "+e.toString()); } //convert response to string 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(); result=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error converting result "+e.toString()); } try{ jArray = new JSONObject(result); }catch(JSONException e){ Log.e("log_tag", "Error parsing data "+e.toString()); } return jArray; } } merci d'avance j'attend votre aide Link to comment Share on other sites More sharing options...
etud21 Posted May 19, 2011 Author Share Posted May 19, 2011 personne pour me repondre?! Link to comment Share on other sites More sharing options...
frozenscar Posted May 23, 2011 Share Posted May 23, 2011 Salut, est ce que tu peux mettre l'erreur que tu obtiens dans le logcat android, ca pourrait aider a voir ton erreur. a+ Link to comment Share on other sites More sharing options...
etud21 Posted May 31, 2011 Author Share Posted May 31, 2011 Salut, est ce que tu peux mettre l'erreur que tu obtiens dans le logcat android, ca pourrait aider a voir ton erreur. a+ merci fort Romain j'ai résolu le probleme....j'ai reglé le fichier JSON car il doit commencé par { et c'etait [ donc il n'a pas pu accédé et faire le parsing. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.