我正在开发一款应用,我尝试创建一个好友列表。为此我想得到用户的所有朋友并在我的应用程序中显示它。但它只显示1个用户,而不是全部。有人能帮助我吗? 这是我的代码: PHP:
<?php
// array for JSON response
$response = array();
// Datenbankverbindungsklasse einbinden
require_once(dirname(__FILE__).'/db_connect.php');
// Klasseninstanz und -verbindung herstellen
$db = new DB_CONNECT();
$con = $db->connect();
// check for post data
if (isset($_GET["bid"])) {
$bid = intval($_GET['bid']);
$query = sprintf("SELECT Benutzer.benutzername
FROM Benutzer
INNER JOIN Freundesliste
ON benutzer_ID = Freundesliste.Bid_2 and Freundesliste.Bid_1='%d'",
mysql_real_escape_string($bid, $con));
$result = mysql_query($query);
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$benutzer = array();
$benutzer["benutzername"] = $result["benutzername"];
// success
$response["success"] = 1;
// user node
$response["benutzer"] = array();
array_push($response["benutzer"], $benutzer);
// echoing JSON response
echo json_encode($response);
} else {
// no benutzer found
$response["success"] = 0;
$response["message"] = "No benutzer found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no benutzer found
$response["success"] = 0;
$response["message"] = "No benutzer found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
$db->close();
?>
机器人:
/** Vorhandene Freunde laden */
class GetFriends extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(FriendlistActivity.this);
pDialog.setMessage("Freunde werden geladen...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Freunde im Hintergrund laden
* */
protected String doInBackground(String... params) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("bid", bid));
// getting user data by making HTTP request
// Note that user data url will use GET request
JSONObject json = jsonParser.makeHttpRequest("LINK", "GET", params);
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received user data
JSONArray userObj = json.getJSONArray(TAG_USER); // JSON Array
// get all user object from JSON Array
for(int i=0;i<userObj.length();i++){
json_data = userObj.getJSONObject(i);
String friend = json_data.get(TAG_NAME).toString();
listadapter.add(friend);
}
friendlistview.setAdapter(listadapter);
}else{
// user with bid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all data
pDialog.dismiss();
}
}
答案 0 :(得分:0)
以这种方式试试,以便遍历结果中的每一行
$benutzer = array();
$response["benutzer"] = array();
while($result = mysql_fetch_array($result)) {
$benutzer["benutzername"] = $result["benutzername"];
$response["success"] = 1;
array_push($response["benutzer"], $benutzer);
}
答案 1 :(得分:0)
您需要循环结果以获取所有行:
while($row = mysql_fetch_assoc($result)) {
$response["benutzer"][] = $row["benutzername"];
}
$response["success"] = 1;
离开MySQL扩展并使用PDO或MySQLI。