-2

I want to get string values on this one

{"response":"success","Message":"Product Created Successfully"}

final JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {

                JSONObject JO = response.getJSONObject();
                String respond = JO.getString("response");
                String message = JO.getString("Message");
                Toast.makeText(MainActivity.this, respon + message,Toast.LENGTH_SHORT).show();



            } catch (JSONException e) {
                    e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
});
Dan Harms
  • 4,725
  • 2
  • 18
  • 28

2 Answers2

2

you must write this code in your try{...} block

String respond = response.getString("response");
String message = response.getString("Message");
Toast.makeText(MainActivity.this, respond + message,Toast.LENGTH_SHORT).show();
hadi mohammadi
  • 321
  • 1
  • 2
  • 10
0

As a new programmer I recommend you:

A. Read some how you work with Jason.

http://www.vogella.com/tutorials/AndroidJSON/article.html

B. Use the Jason library to make it easy for you down the road.

I'll give you an example of the right use for your needs now.

Build a class that fits your json:

public class MyObject {

private String response;
private String Message;

public MyObject() {
}

public String getResponse() {
    return response;
}

public void setResponse(String response) {
    this.response = response;
}

public String getMessage() {
    return Message;
}

public void setMessage(String message) {
    Message = message;
}

}

Add Gson library to your project:

dependencies {
  implementation 'com.google.code.gson:gson:2.8.5'
}

Sync your project and now you can cast your json easily to class object:

    String json = "{response:success,Message\":Product Created Successfully}";
    MyObject myObject = new Gson().fromJson(json, MyObject.class);

    String a = myObject.getResponse();
    String b = myObject.getMessage();
Guy4444
  • 1,411
  • 13
  • 15