Reputation: 330
i did a simple GET request, who return 1 or 0 if my login & password are correct. I do my connection on an other thread .
Like this :
public void getConnection(){
String url = null;
url = NOM_HOTE + PATH_METHODE + "identifiant="+ identifiant.getText().toString() + "&password="+ password.getText().toString();
HttpClient httpClient = new DefaultHttpClient();
try{
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
if(httpEntity != null){
InputStream inputStream = httpEntity.getContent();
//Lecture du retour au format JSON
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String ligneLue = bufferedReader.readLine();
while(ligneLue != null){
stringBuilder.append(ligneLue + " \n");
ligneLue = bufferedReader.readLine();
}
bufferedReader.close();
JSONObject jsonObject = new JSONObject(stringBuilder.toString());
Log.i("Chaine JSON", stringBuilder.toString());
JSONObject jsonResultSet = jsonObject.getJSONObject("nb"); <--it's here where the error occured
// int nombreDeResultatsTotal = jsonResultSet.getInt("nb");
// Log.i(LOG_TAG, "Resultats retourne" + nombreDeResultatsTotal);
}// <-- end IF
}catch (IOException e){
Log.e(LOG_TAG+ "1", e.getMessage());
}catch (JSONException e){
Log.e(LOG_TAG+ "2", e.getMessage());
}
}
i have a JSON return like this : {"nb":"1"}
or {"nb":"0"}
So my JSON is correct. but i have this error on catch(JSONException
) when i submit my form :
11-07 17:01:57.833: E/ClientJSON2(32530): Value 1 at nb of type java.lang.String cannot be converted to JSONObject
i don't understand why , whereas my syntax, connection are correct and on the Log with tag "Chaine JSON" , i have {"nb:"1"}
in the response..
Upvotes: 2
Views: 4690
Reputation: 3373
Use
String myReturnValue = jsonObject. getString("nb");
instead of
JSONObject jsonResultSet = jsonObject.getJSONObject("nb");
This will return you a String ;)
Upvotes: 0
Reputation: 18112
value represented by "nb" is a string not object.
use jsonObject.getString("nb");
even jsonObject.getInt("nb");
would work if value is a numerical
Upvotes: 0
Reputation: 157467
nb is a String
, not a JSONObject
. Change
JSONObject jsonResultSet = jsonObject.getJSONObject("nb");
in
String result = jsonObject.getString("nb");
Upvotes: 5