Issue
How will I get the value inside the inner class in Android Studio 4.0.1. I tried to declare a global variable but still it doesn't work. I also tried this one declaring a final array but I still cannot get the value
public class FetchData {
private Context con;
public FetchData(Context con){
this.con = con;
}
public String getPatientIDFromDatabase(){
final String[] id = {""};
StringRequest strRequest = new StringRequest(Request.Method.POST, "http://192.168.254.199/projects/AndroidServers/TheMedicalGCs/GetPatientID.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject jsonObject = new JSONObject(response);
id[0] = jsonObject.getString("PatientID");
}
catch (Exception e){
Toast.makeText(con, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(con, error.getMessage(), Toast.LENGTH_SHORT).show();
}
}
);
Volley.newRequestQueue(con).add(strRequest);
return id[0];
}
}
I just want that method to return the value comes from the JSON.
Solution
I already fixed the problem by declaring the final String[] id = {""}
into a member variable and declare it as private String id
only. I remembered that you cannot access the variable that is not declared as final inside the inner class.
public class FetchData {
private Context con;
private String id;
public FetchData(Context con){
this.con = con;
}
public String getPatientIDFromDatabase(){
StringRequest strRequest = new StringRequest(Request.Method.POST, "http://192.168.254.199/projects/AndroidServers/TheMedicalGCs/GetPatientID.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject jsonObject = new JSONObject(response);
id = jsonObject.getString("PatientID");
}
catch (Exception e){
Toast.makeText(con, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(con, error.getMessage(), Toast.LENGTH_SHORT).show();
}
}
);
Volley.newRequestQueue(con).add(strRequest);
return id;
}
}
Answered By - Kroi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.