Aller au contenu

Crash application si pas connecté au serveur (Mauvais Try/Catch?)


Recommended Posts

Bonjour,

J'envoie des données au serveur en appuyant sur un bouton à l'aide d'un httpGet ou httpPost. Cela marche parfaitement si je suis connecté au serveur mais l'application crash sinon.

Voilà mon code, le try catch me semble pourtant bon..

    ((ImageButton)findViewById(R.id.pickSurnom)).setOnClickListener(new OnClickListener(){

			public void onClick(View v) {


				AlertDialog.Builder alert = new AlertDialog.Builder(Postit.this);

				alert.setTitle("Modifier votre surnom");
				//alert.setMessage("Message");

				// Set an EditText view to get user input 
				final EditText input = new EditText(Postit.this);
				alert.setView(input);

				alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {

					String surnom = input.getText().toString(); 					


			        //Recuperation "id" du user dans les préférences partagées
					SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
					String id = prefs.getString("id", null);



                               //------------------------ENVOI----------------------------

			        StringBuffer stringBuffer = new StringBuffer("");
			        BufferedReader bufferedReader = null;

					try {
						HttpClient httpClient = new DefaultHttpClient();
			        	HttpGet httpGet = new HttpGet();

			        	//httpGet.setURI(new URI("http://192.168.0.1:8888/user/public"+"?"+"surnom="+((TextView) findViewById(R.id.surnom)).getText().toString()));
			        	httpGet.setURI(new URI("http://192.168.0.1:8888/user"+"?"+"id="+id+"surnom="+ surnom ));

			        	HttpResponse httpResponse = httpClient.execute(httpGet);
			        	bufferedReader = new BufferedReader(
			        					new InputStreamReader(httpResponse.getEntity().getContent()));

			        			String ligneLue = bufferedReader.readLine();

			        			Toast.makeText(getApplicationContext(), "id="+bufferedReader.readLine() , Toast.LENGTH_LONG).show();

			        			while (ligneLue!=null){
			        				stringBuffer.append(ligneLue);
			        				ligneLue = bufferedReader.readLine(); 

			        				//Si la réponse = True
			        				//Remplir le bon surnom dans le TextView
			        				((TextView) findViewById(R.id.surnom)).setText(surnom);

	                }


			        } catch (Exception e){
			        	Toast.makeText(getApplicationContext(), "Connection error", Toast.LENGTH_LONG).show();
			        	Log.e("Exception1", e.getMessage());

			        } finally {
			        	if (bufferedReader !=null){
			        		try{
			        			bufferedReader.close();
			        			Toast.makeText(getApplicationContext(), "Serveur indisponible" + surnom, Toast.LENGTH_LONG).show();

			        		} catch (IOException e){
			        			Log.e("Exception2", e.getMessage());
			        		}
			        	}
			        }

			        Log.i("Exception3", stringBuffer.toString());

Vous avez une idée?

Florent

Lien vers le commentaire
Partager sur d’autres sites

que dit le logcat?

tu peu debug ton appli pour détecter à quel endroit ton appli plante mais normalement le logcat devrait te le dire +/- approximativement

01-17 15:45:00.801: ERROR/ActivityManager(58): ANR in com.branchu1 (com.branchu1/.Postit)
01-17 15:45:00.801: ERROR/ActivityManager(58): Reason: keyDispatchingTimedOut
01-17 15:45:00.801: ERROR/ActivityManager(58): Load: 0.5 / 0.18 / 0.08
01-17 15:45:00.801: ERROR/ActivityManager(58): CPU usage from 32503ms to 56ms ago:
01-17 15:45:00.801: ERROR/ActivityManager(58):   system_server: 19% = 15% user + 3% kernel / faults: 2075 minor
01-17 15:45:00.801: ERROR/ActivityManager(58):   ndroid.launcher: 3% = 2% user + 0% kernel / faults: 1554 minor
01-17 15:45:00.801: ERROR/ActivityManager(58):   adbd: 1% = 0% user + 0% kernel
01-17 15:45:00.801: ERROR/ActivityManager(58):   ndroid.settings: 0% = 0% user + 0% kernel / faults: 124 minor
01-17 15:45:00.801: ERROR/ActivityManager(58):   m.android.phone: 0% = 0% user + 0% kernel / faults: 56 minor
01-17 15:45:00.801: ERROR/ActivityManager(58):   ronsoft.openwnn: 0% = 0% user + 0% kernel / faults: 43 minor
01-17 15:45:00.801: ERROR/ActivityManager(58):   logcat: 0% = 0% user + 0% kernel / faults: 1 minor
01-17 15:45:00.801: ERROR/ActivityManager(58):   events/0: 0% = 0% user + 0% kernel
01-17 15:45:00.801: ERROR/ActivityManager(58):   android.protips: 0% = 0% user + 0% kernel / faults: 23 minor
01-17 15:45:00.801: ERROR/ActivityManager(58):   zygote: 0% = 0% user + 0% kernel / faults: 135 minor
01-17 15:45:00.801: ERROR/ActivityManager(58):   .quicksearchbox: 0% = 0% user + 0% kernel / faults: 27 minor
01-17 15:45:00.801: ERROR/ActivityManager(58):   id.defcontainer: 0% = 0% user + 0% kernel / faults: 24 minor
01-17 15:45:00.801: ERROR/ActivityManager(58):   com.android.mms: 0% = 0% user + 0% kernel / faults: 25 minor
01-17 15:45:00.801: ERROR/ActivityManager(58):   m.android.music: 0% = 0% user + 0% kernel / faults: 23 minor
01-17 15:45:00.801: ERROR/ActivityManager(58):   d.process.media: 0% = 0% user + 0% kernel / faults: 25 minor
01-17 15:45:00.801: ERROR/ActivityManager(58):   m.android.email: 0% = 0% user + 0% kernel / faults: 34 minor
01-17 15:45:00.801: ERROR/ActivityManager(58):   com.svox.pico: 0% = 0% user + 0% kernel / faults: 23 minor
01-17 15:45:00.801: ERROR/ActivityManager(58):  +com.branchu1: 0% = 0% user + 0% kernel
01-17 15:45:00.801: ERROR/ActivityManager(58):  -com.branchu1: 0% = 0% user + 0% kernel
01-17 15:45:00.801: ERROR/ActivityManager(58): TOTAL: 33% = 25% user + 8% kernel + 0% softirq

Apparement je dois faire les requêtes dans un thread AsyncTask exterieur au onCreate()...je vais essayer de voir à quoi ça ressemble.

Lien vers le commentaire
Partager sur d’autres sites

Archivé

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

×
×
  • Créer...