Dharmik Patel Dharmik Patel - 3 months ago 17
Android Question

JSON parsing volley in android

I want to parsing JSON in android using volleylibrary for login.In Login activity,send two parameters(username,password) via post and response like below.

parameters : username,password

for success:

{
"status": 1,
"message": "successfully login" ,
"result": [
{
"name": "abc",
"email": "abc@gmail.com",
"Img": "http://img.com/img.png"
}
]
}


for login:

{
"status": 0,
"message": "email or password doesn’t exist"
}

Answer

It is how to do it.

        public void onResponse(JSONObject response) {
            try {
                int status = response.getInt("status");
                if (status == 1) {
                  String message = response.getString("message");
                  JSONArray jsonArray = response.getJSONArray("result");
                  JSONObject jsonObject = jsonArray.getJSONObject(0);
                  String name = jsonObject.getString("name");
                  String email = jsonObject.getString("email");
                  String imgLink = jsonObject.getString("Img"); 
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }