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:
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:
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();
}
} |
android json (login et mot de passe)
Citation:
Envoyé par
MrDuChnok
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:
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:
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();
}
} |