IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

problem android JSONException e


Sujet :

Android

  1. #1
    Membre du Club
    Inscrit en
    Mars 2011
    Messages
    62
    Détails du profil
    Informations forums :
    Inscription : Mars 2011
    Messages : 62
    Points : 55
    Points
    55
    Par défaut problem android JSONException e
    salut tout le monde j'ai un problème au niveau mon code android , mon objectif c'est de connecter avec un login et un mot de passe, avec http et json

    mon code :

    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
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    package com.instropy.androiddexamples.net;
     
    import org.json.JSONException;
    import org.json.JSONObject;
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.Window;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
     
    import com.instropy.androiddexamples.net.RestJsonClient;
     
    // implements OnClickListener so we don´t have to create a nested class << #ugly
     
    public class HelloAndroid extends Activity implements OnClickListener {
     
    private static final int SERVER_PORT = 80;
     
    private static final String DEB_TAG = "Json_Android";
     
    private String SERVER_HOST="instropy.com";
     
    public static final String PREFS_NAME = "HelloAndroidPREFS";
     
    private SharedPreferences settings;
     
    private ProgressDialog progress;       
     
    /** Called when the activity is first created. */
     
    @Override
     
    public void onCreate(Bundle icicle) {
     
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
     
    super.onCreate(icicle);
     
    // Restore preferences
     
    settings = getSharedPreferences(PREFS_NAME, 0);
     
    // load up the layout
     
    setContentView(R.layout.main);
     
    // get the button resource in the xml file and assign it to a local variable of type Button
     
    Button login = (Button)findViewById(R.id.login_button);
     
    Log.i(DEB_TAG,"onCreate");
     
    login.setOnClickListener(this);
     
    setUserNameText(settings.getString("Login", ""));
     
    setPasswordText(settings.getString("Password", ""));
     
    }
     
    public void setUserNameText(String $username){
    EditText usernameEditText = (EditText) findViewById(R.id.txt_username);
    usernameEditText.setText($username);
    }
     
    public void setPasswordText(String $username){
    EditText passwordEditText = (EditText) findViewById(R.id.txt_password);
    passwordEditText.setText($username);
     
    }
     
    /*
     
    * (non-Javadoc)
     
    * @see android.view.View.OnClickListener#onClick(android.view.View)
     
    */
     
    public void onClick(View v) {
     
    //Handle based on which view was clicked.
     
    Log.i(DEB_TAG+" onClick ","onClick");
     
    // this gets the resources in the xml file
     
    //and assigns it to a local variable of type EditText
     
    EditText usernameEditText = (EditText) findViewById(R.id.txt_username);
     
    EditText passwordEditText = (EditText) findViewById(R.id.txt_password);
     
    // the getText() gets the current value of the text box
     
    // the toString() converts the value to String data type
     
    // then assigns it to a variable of type String
     
    String sUserName = usernameEditText.getText().toString();
     
    String sPassword = passwordEditText.getText().toString();
     
    //call the backend using Get parameters (discouraged but works good for this exampl <img src="http://www.instropy.com/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley">  )
     
    String address = "http://"+SERVER_HOST+":"+SERVER_PORT+"/jbackend.php?action=login&Login="+sUserName+"&Password="+sPassword+"";
     
    if(usernameEditText == null || passwordEditText == null){
    // show some warning
    }else{
    // display the username and the password in string format
    try {
     
    showBusyCursor(true);
     
    progress = ProgressDialog.show(this,
     
    "Please wait...", "Login in process", true);
     
    Log.i(DEB_TAG, "Username: " + sUserName + "nPassword: " + sPassword);
     
    Log.i(DEB_TAG, "Requesting to "+address);
     
    JSONObject json = RestJsonClient.connect(address);
     
    } catch (JSONException e) {// L'ERREUR'
     
    // TODO Auto-generated catch block
     
    e.printStackTrace();
     
    showBusyCursor(false);
     
    }//end try
     
    progress.dismiss();
     
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("Login", sUserName);
    editor.putString("Password", sPassword);
    editor.commit();
    showBusyCursor(false);
     
    next();
     
    }//end else
     
    }//end OnClick
     
    /*
     
    *
     
    */
     
    private void showBusyCursor(Boolean $show){
    setProgressBarIndeterminateVisibility($show);
    }
     
    private void next(){
    // you can call another activity by uncommenting the above lines
    //Intent myIntent = new Intent( this.getBaseContext() , LoggedActivity.class);
    //startActivityForResult(myIntent, 0);
    }
     
    }
    l'autre class

    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
    package com.instropy.androiddexamples.net;
     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
     
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.json.JSONException;
    import org.json.JSONObject;
     
    public class RestJsonClient {
     
        public static JSONObject connect(String url)
        {
     
            HttpClient httpclient = new DefaultHttpClient();
     
            // Prepare a request object
            HttpGet httpget = new HttpGet(url); 
     
            // Execute the request
            HttpResponse response;
     
            JSONObject json = new JSONObject();
     
            try {
                response = httpclient.execute(httpget);
     
                HttpEntity entity = response.getEntity();
     
                if (entity != null) {
     
                    // A Simple JSON Response Read
                    InputStream instream = entity.getContent();
                    String result= convertStreamToString(instream);
     
                    json=new JSONObject(result);
     
                    instream.close();
                }
     
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
     
            return json;
        }
        /**
         *
         * @param is
         * @return 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) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return sb.toString();
        }
     
    }

  2. #2
    Rédacteur
    Avatar de MrDuChnok
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2002
    Messages
    2 112
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 112
    Points : 4 240
    Points
    4 240
    Par défaut
    Salut,
    C'est déjà pas mal d'avoir un objectif
    Mais si on a pas de messages d'erreurs, de log, d'exceptions qui ont étés déclenchées, de ce que tu as essayé, ça va être difficile de t'aider.
    Donc merci de préciser un peu plus ce que tu attends de nous.
    Si vous jugez mon post utile dans la résolution de votre problème, n'hésitez pas à utiliser le système de vote afin d'améliorer la qualité du forum

  3. #3
    Membre du Club
    Inscrit en
    Mars 2011
    Messages
    62
    Détails du profil
    Informations forums :
    Inscription : Mars 2011
    Messages : 62
    Points : 55
    Points
    55
    Par défaut android json (login et mot de passe)
    Citation Envoyé par MrDuChnok Voir le message
    Salut,
    C'est déjà pas mal d'avoir un objectif
    Mais si on a pas de messages d'erreurs, de log, d'exceptions qui ont étés déclenchées, de ce que tu as essayé, ça va être difficile de t'aider.
    Donc merci de préciser un peu plus ce que tu attends de nous.
    j'ai une erreur au niveau JSON Exception e qui coloré en rouge au classe Hello adroid si tu as une idée aidez moi

    Hello android.java :

    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
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    package com.instropy.androiddexamples;
    
    import org.json.JSONException;
    import org.json.JSONObject;
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.Window;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    
    import com.instropy.androiddexamples.net.RestJsonClient;
    
    // implements OnClickListener so we don´t have to create a nested class << #ugly
    
    public class HelloAndroid extends Activity implements OnClickListener {
    
    private static final int SERVER_PORT = 80;
    
    private static final String DEB_TAG = "Json_Android";
    
    private String SERVER_HOST="instropy.com";
    
    public static final String PREFS_NAME = "HelloAndroidPREFS";
    
    private SharedPreferences settings;
    
    private ProgressDialog progress;       
    
    /** Called when the activity is first created. */
    
    @Override
    
    public void onCreate(Bundle icicle) {
    
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    
    super.onCreate(icicle);
    
    // Restore preferences
    
    settings = getSharedPreferences(PREFS_NAME, 0);
    
    // load up the layout
    
    setContentView(R.layout.main);
    
    // get the button resource in the xml file and assign it to a local variable of type Button
    
    Button login = (Button)findViewById(R.id.login_button);
    
    Log.i(DEB_TAG,"onCreate");
    
    login.setOnClickListener(this);
    
    setUserNameText(settings.getString("Login", ""));
    
    setPasswordText(settings.getString("Password", ""));
    
    }
    
    public void setUserNameText(String $username){
    EditText usernameEditText = (EditText) findViewById(R.id.txt_username);
    usernameEditText.setText($username);
    }
    
    public void setPasswordText(String $username){
    EditText passwordEditText = (EditText) findViewById(R.id.txt_password);
    passwordEditText.setText($username);
    
    }
    
    /*
    
    * (non-Javadoc)
    
    * @see android.view.View.OnClickListener#onClick(android.view.View)
    
    */
    
    public void onClick(View v) {
    
    //Handle based on which view was clicked.
    
    Log.i(DEB_TAG+" onClick ","onClick");
    
    // this gets the resources in the xml file
    
    //and assigns it to a local variable of type EditText
    
    EditText usernameEditText = (EditText) findViewById(R.id.txt_username);
    
    EditText passwordEditText = (EditText) findViewById(R.id.txt_password);
    
    // the getText() gets the current value of the text box
    
    // the toString() converts the value to String data type
    
    // then assigns it to a variable of type String
    
    String sUserName = usernameEditText.getText().toString();
    
    String sPassword = passwordEditText.getText().toString();
    
    //call the backend using Get parameters (discouraged but works good for this exampl <img src="http://www.instropy.com/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley">  )
    
    String address = "http://"+SERVER_HOST+":"+SERVER_PORT+"/jbackend.php?action=login&Login="+sUserName+"&Password="+sPassword+"";
    
    if(usernameEditText == null || passwordEditText == null){
    // show some warning
    }else{
    // display the username and the password in string format
    try {
    
    showBusyCursor(true);
    
    progress = ProgressDialog.show(this,
    
    "Please wait...", "Login in process", true);
    
    Log.i(DEB_TAG, "Username: " + sUserName + "nPassword: " + sPassword);
    
    Log.i(DEB_TAG, "Requesting to "+address);
    
    JSONObject json = RestJsonClient.connect(address);
    
    } catch (JSONException e) {
    
    // TODO Auto-generated catch block
    
    e.printStackTrace();
    
    showBusyCursor(false);
    
    
    }//end try
    
    progress.dismiss();
    
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("Login", sUserName);
    editor.putString("Password", sPassword);
    editor.commit();
    showBusyCursor(false);
    
    next();
    
    }//end else
    
    }//end OnClick
    
    /*
    
    *
    
    */
    
    private void showBusyCursor(Boolean $show){
    setProgressBarIndeterminateVisibility($show);
    }
    
    private void next(){
    // you can call another activity by uncommenting the above lines
    //Intent myIntent = new Intent( this.getBaseContext() , LoggedActivity.class);
    //startActivityForResult(myIntent, 0);
    }
    
    }
    class ResJsonClient.java

    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
    package com.instropy.androiddexamples.net;
     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
     
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.json.JSONException;
    import org.json.JSONObject;
     
    public class RestJsonClient {
     
        public static JSONObject connect(String url)
        {
     
            HttpClient httpclient = new DefaultHttpClient();
     
            // Prepare a request object
            HttpGet httpget = new HttpGet(url); 
     
            // Execute the request
            HttpResponse response;
     
            JSONObject json = new JSONObject();
     
            try {
                response = httpclient.execute(httpget);
     
                HttpEntity entity = response.getEntity();
     
                if (entity != null) {
     
                    // A Simple JSON Response Read
                    InputStream instream = entity.getContent();
                    String result= convertStreamToString(instream);
     
                    json=new JSONObject(result);
     
                    instream.close();
                }
     
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
     
            return json;
        }
        /**
         *
         * @param is
         * @return 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) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return sb.toString();
        }
     
    }

  4. #4
    Rédacteur
    Avatar de MrDuChnok
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2002
    Messages
    2 112
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 112
    Points : 4 240
    Points
    4 240
    Par défaut
    Oui, mais il nous faut le message d'erreur au complet de l'exception. Sans ça, il faut créer nous même le projet et le debuguer. Beaucoup de gens ne peuvent pas faire ça.
    Si tu veux que des personnes t'aident, il faut donner le maximum d'information.
    Si vous jugez mon post utile dans la résolution de votre problème, n'hésitez pas à utiliser le système de vote afin d'améliorer la qualité du forum

Discussions similaires

  1. probleme android studio
    Par kapac dans le forum Android Studio
    Réponses: 0
    Dernier message: 28/04/2014, 11h38
  2. probleme android market
    Par celine1007 dans le forum Android
    Réponses: 1
    Dernier message: 18/05/2011, 18h10
  3. Android => Web Service : Probleme
    Par sadjira dans le forum API standards et tierces
    Réponses: 0
    Dernier message: 12/05/2011, 12h55
  4. probleme avec eclipse sous android
    Par bennour.mohamed dans le forum Android
    Réponses: 5
    Dernier message: 09/03/2011, 14h06
  5. [HTML 5] Probleme de lien sur Iphone / Android
    Par Fooshi dans le forum Balisage (X)HTML et validation W3C
    Réponses: 0
    Dernier message: 09/02/2011, 12h14

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo