0

I am new to Java... But I have tried to achieve this in JS successfully, but I don't know how to do same in Java.

Question

I am building an android app in Android Studio, and I have this code that sends a POST request to a PHP page in my local server.

String url = "http://localhost/inc/signUp.php";
StringRequest rqst = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        //get my response in an Array
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
    }
});

And in my signUp.php I am outputting the response JSON_ENCODE

$data = array('state', 'value');
echo json_encode($data);

Now on the Java code, I want to get the response in an array but I don't know how to do that

1
  • If you can read the raw response inside the onResponse method you can decode it using a library like Gson. Commented Feb 10, 2020 at 18:25

1 Answer 1

1

Okay... I got this code, and it works for me

String url = "http://localhost/inc/signUp.php";
StringRequest rqst = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        //get my response in an Array
        try{
            JSONObject obj = new JSONObject(response);
        } catch (JSONExeption e) {
            e.printStackTrace();
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.