Skip to content Skip to sidebar Skip to footer

How To Fetch Single Data From Server Into Textview In Android Volley?

i am new to android ,i am working on login and password activity where if you forgot the password then after typing your correct email the server will send the password into textvi

Solution 1:

Your PHP code is sending password in JSON format, first decode the JSON in your code and fetch the password:

newResponse.Listener<String>() {
@OverridepublicvoidonResponse(String response) {
pDialog.dismiss();

try {
JSONObjectobject     = newJSONObject(response);
JSONArray jsonArray   = object.getJSONArray("result");
JSONObject jsonObject = jsonArray.getJSONObject(0);

// fetch password from JSON// UPDATED HEREString password         = jsonObject.getString("password");

femail.setText(password, TextView.BufferType.EDITABLE);

} 
catch (JSONException e) {
Toast.makeText(getContext(), e.toString(), Toast.LENGTH_LONG).show();
}  

},

PHP

<?phpinclude ('config.php');

// updated here, value is mapped $email = $_POST['email'];

$sql = "SELECT * FROM UserInfo WHERE email='".$email."'";



$r = mysqli_query($con,$sql);

//checking if email is valid or notif(mysqli_fetch_array($r) == NULL){
echo"Invalid email";
}
else{
$result = array();

while($row = mysqli_fetch_array($r)){
    array_push($result,array(
        'password'=>$row['password'],

    ));
}
echo json_encode(array('result'=>$result));
}
mysqli_close($con);

?>

Solution 2:

use JsonObjectRequest instead of StringRequest

JsonObjectRequest request = newJsonObjectRequest(Request.Method.POST, url, json, newResponse.Listener<JSONObject>() {
                            @OverridepublicvoidonResponse(JSONObjectresponse) {
                          // you code here
 }
}
, newResponse.ErrorListener() {
                            @OverridepublicvoidonErrorResponse(VolleyError error) {

                            }
                        }) {
                    @OverridepublicMap<String, String> getHeaders() throws AuthFailureError {
                        Map<String, String> headers = newHashMap<String, String>();
                    // add your header herereturn headers;
                    }
                };

Post a Comment for "How To Fetch Single Data From Server Into Textview In Android Volley?"