我的代码中存在问题。我只想从我的服务器接收JSONObject。这是我提出请求后我的PHP代码的一部分
$obj = $result->fetchAll(PDO::FETCH_CLASS);
foreach ($obj as $row) {
$trouver = FALSE;
if (stristr($row->type_of_event, $chaine_rechercher) !== FALSE) $trouver = TRUE;
else if (stristr($row->name_of_event, $chaine_rechercher) !== FALSE) $trouver = TRUE;
else if (stristr($row->description_of_event, $chaine_rechercher) !== FALSE) $trouver = TRUE;
if ($trouver == TRUE){
$reponse["id"] = $row->id;
$reponse["type_of_event"] = utf8_encode($row->type_of_event);
$reponse["description_of_event"] = utf8_encode($row->description_of_event);
echo(json_encode($reponse));
}
}
但问题是在我的Android代码中的onSuccess中,只收到第一个JSONObject。这是我的Android代码的一部分
client.get("my_url", params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject json_data) {
try {
int id = json_data.getInt("id");
String type = json_data.getString("type_of_event");
String description = json_data.getString("description_of_event");
id_list.add(id);
type_list.add(type);
description_list.add(description);
((SearchResultAdapter)getListAdapter()).notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
Toast.makeText(SearchActivity.this, "Echec de connexion", Toast.LENGTH_SHORT).show();
}
});
你能帮帮我吗?请。
答案 0 :(得分:0)
试试这个
try {
JSONArray jsonarray = new JSONArray(json_data);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobj = jsonarray.getJSONObject(i);
int id = jsonobj .getInt("id");
String type = jsonobj .getString("type_of_event");
String description = jsonobj .getString("description_of_event");
id_list.add(id);
type_list.add(type);
description_list.add(description);
}
((SearchResultAdapter)getListAdapter()).notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}