2 years ago
#47235
Javierungo
How to get data when it is in a JSONObject in the position [0] of a JSONArray?
I am trying that my app send two parameters to PHP file, where that file get data from mySQL database and send back to my app an array with the data.
Everything works but the problem is that I cannot get every row of the response separately because the data is a JSONObject in the position [0] of a JSONArray. I have tried a loop FOR, but it takes the values of the first registry in every iteration and it cannot reach the next.
I have tried every related post in here and I cannot solve it. I appreciate any help. Thanks in advance.
This the code I have in my android app:
private void llamadovolley() {
final String userPlayer = etUser.getText().toString();
final String passwordPlayer = etPassword.getText().toString();
String url = "http://192.168.1.129/studyBonusGame/3CheckUserNew.php";
StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
JSONObject playerJson=jsonArray.getJSONObject(0);
d.deleteTables();
for(int i=0;i<playerJson.length();i++)
{
ObjectPlayer player = new ObjectPlayer();
player.setPlayerName(playerJson.getString("NOM_JUG"));
player.setPlayerVirtualDate(playerJson.getString("VIRT_DATE"));
player.setAccountMoney(playerJson.getDouble("ACCOUNT_MONEY"));
player.setWeekTournamentMode(playerJson.getInt("WEEK_TOURNAMENT_MODE"));
player.setWeekTournamentRound(playerJson.getInt("WEEK_TOURNAMENT_ROUND"));
player.setButtonFinancesEnabled(playerJson.getInt("BUTTON_FINANCES_ENABLED"));
player.setButtonQAManagerEnabled(playerJson.getInt("BUTTON_QAMANAGER_ENABLED"));
player.setButtonSuccessErrorGameEnabled(playerJson.getInt("BUTTON_SUCCESS_ERROR_GAME_ENABLED"));
player.setCurrentPack(playerJson.getString("CURRENT_PACK"));
player.setCorrectQueVirtualDate(playerJson.getInt("CORRECT_QUE_VIRT_DATE"));
player.setWeekTournamentLevel(playerJson.getInt("WEEK_TOURNAMENT_LEVEL"));
player.setTokenX2(playerJson.getInt("TOKEN_X2"));
player.setTokenX3(playerJson.getInt("TOKEN_X3"));
player.setLotteryTickets(playerJson.getDouble("LOTTERY_TICKETS"));
player.setCurrentLivingPlace(playerJson.getString("CURRENT_LIVING_PLACE"));
player.setTotalLifeScore(playerJson.getDouble("TOTAL_LIFE_SCORE"));
player.setMusicMenusOnOff(playerJson.getInt("MUSIC_MENUS_ON_OFF"));
player.setRandomMarket(playerJson.getInt("RANDOM_MARKET"));
player.setAfterPacksScreen(playerJson.getInt("AFTER_PACKS_SCREEN"));
player.setTextEditorQue(playerJson.getString("TEXT_EDITOR_QUE"));
player.setTextEditorAns(playerJson.getString("TEXT_EDITOR_ANS"));
player.setIdQuestion(playerJson.getInt("ID_QUESTION"));
player.setCoins(playerJson.getInt("COINS"));
d.importOnlinePlayersTable(player);
}
Toast.makeText(userPass2Activity.this, "Imported correctly", Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new Hashtable<>();
params.put("userPlayer", userPlayer);
params.put("passwordPlayer", passwordPlayer);
return params;
}
};
RequestQueue rq = Volley.newRequestQueue(this);
rq.add(sr);
}
And this is the PHP, just in case it helps:
<?php
try{
$base=new PDO("mysql:host=localhost; dbname=database" , "root", "");
$base->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql="SELECT * FROM table_players WHERE userPlayer= :userPlayer AND passwordPlayer= :passwordPlayer";
$resultado=$base->prepare($sql);
$userPlayer = $_POST['userPlayer'];
$passwordPlayer = $_POST['passwordPlayer'];
$userPlayer=htmlentities(addslashes($userPlayer));
$passwordPlayer=htmlentities(addslashes($passwordPlayer));
$resultado->bindValue(":userPlayer", $userPlayer);
$resultado->bindValue(":passwordPlayer", $passwordPlayer);
$resultado->execute();
$res = $resultado -> fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($res);
echo $json;
}catch(Exception $e){
die("Error: " . $e->getMessage());
}
?>
php
android
android-studio
android-volley
0 Answers
Your Answer