Aller au contenu
View in the app

A better way to browse. Learn more.

Forum de Frandroid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

parsing JSON+listview

Featured Replies

Posté(e)

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

Modifié par etud21

Posté(e)

Salut,

est ce que tu peux mettre l'erreur que tu obtiens dans le logcat android, ca pourrait aider a voir ton erreur.

a+

  • 2 weeks later...
Posté(e)
  • Auteur

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.

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

Account

Navigation

Rechercher

Rechercher

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.