The problem is that i dont know how to view "username" from friend array:
This is the json i'm recieving from php:
08-30 16:44:08.485
1619-2810/com.gcator E/JSON﹕
{"tag":null,
"success":1,
"error":0,
"friend":{
"username":"Kerrigan",
"latitude":"52.75315",
"longitude":"15.22528"
}
}
{
"tag":null,
"success":1,
"error":0,
"friend":{
"username":"Zahary",
"latitude":"52.72423",
"longitude":"15.24610"
}
}
as You see array is broken too. This is the case if php recieve tag "getfriends" from android. I need to get friends in one array "friend" this is the code:
case 'getfriends':
$id = $_POST ['id'];
$response = array();
$user = $db->getUserById($id);
if($user){
$friends = $db->getFriendsByUser($user);
if($friends){
for ($i = 0; $i < count($friends) ; $i++) {
$locationf = $db->getLocationByUser($friends[$i]);
$latitude = $locationf->getLatitude();
$longitude = $locationf->getLongitude();
$name = $friends[$i]->getUsername();
$response = new $response;
$response["friend"]['username'] = $name;
$response["friend"]['latitude'] = $latitude;
$response["friend"]['longitude'] = $longitude;
}
$response["success"]= 1;
echo json_encode($response);
} else {
echo $response = "u dont have any Friends<br>";
}
} else {
echo "dupa";
echo var_dump($friends);
}
, then use this friend array in android to List "username".
I did Async task where json is recieved but i dont know how to make loop that extract usernames of friends and put it do listviev, then on click will redirect to map activity
private class GetFriends extends AsyncTask<String, Void, JSONObject>
{
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(FriendList.this);
pDialog.setTitle("Contacting Servers");
pDialog.setMessage("Searching Friends ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
SharedPreferences sharedPrefID = getSharedPreferences("userId", Context.MODE_PRIVATE);
String ID = sharedPrefID.getString("KEY_ID", " dupa");
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.getFriends(ID);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
try {
if (json.getString(KEY_SUCCESS) != null) {
String result = json.getString(KEY_SUCCESS);
if(Integer.parseInt(result) == 1){
pDialog.setMessage("Loading View");
pDialog.setTitle("Getting Friends");
}else{
pDialog.dismiss();
Labe.setText("There are no Friends :( Go to site make some !");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
pDialog.dismiss();
}
}
Any ideas/help ? Thanks :) I've searched but there was no good answer for me.