Tho*_*lsi 2 connection android json get http
我做了一个简单的GET请求,如果我的登录名和密码是正确的,则返回1或0.我在另一个线程上建立连接.
像这样 :
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());
}
}
Run Code Online (Sandbox Code Playgroud)
我有这样的JSON返回:{"nb":"1"}或者{"nb":"0"}
所以我的JSON是正确的.但是catch(JSONException当我提交表单时,我有这个错误:
11-07 17:01:57.833:E/ClientJSON2(32530):类型为java.lang.String的nb处的值1无法转换为JSONObject
我不明白为什么,虽然我的语法,连接是正确的,并在带有标签"Chaine JSON"的日志,我{"nb:"1"}在响应..
nb是a String,而不是a JSONObject.更改
JSONObject jsonResultSet = jsonObject.getJSONObject("nb");
Run Code Online (Sandbox Code Playgroud)
在
String result = jsonObject.getString("nb");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4666 次 |
| 最近记录: |