0

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"    
}
  • Possible duplicate of [How to parse JSON in Android](http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android) – Rohit Arya Aug 30 '16 at 05:03
  • 1
    you should try and do this on your own it's too simple ,don't be lazy and read the docs.Hint: it's a json object contaning json array – Pavneet_Singh Aug 30 '16 at 05:04

1 Answers1

2

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();
            }
        }
LUshPUsh
  • 31
  • 2
  • 1
    It is working for me too ... But i recommend to use GSON library for parsing JSON response and it convert it into Java Object here is link of library [link](https://github.com/google/gson) and exmaple to how work with Volley is here [link](http://icetea09.com/blog/2014/11/02/android-parse-json-request-using-volley-gson/) – Zohaib Akram Aug 30 '16 at 05:32
  • 2
    Yes, Zohaib you are right. I also use it. But problem is that it seems like he is a naive developer so I thought it would be better to keep thing simple for the sake of better understanding. – LUshPUsh Aug 30 '16 at 05:34
  • 1
    I agree with you, for new developer it is fine to parse JSON with native JSON Api – Zohaib Akram Aug 30 '16 at 05:36
  • Please,give me full code of how to send parameters via post method and response from above answer... – Dharmik Patel Aug 31 '16 at 04:39
  • [AndroidHive](http://www.androidhive.info/2014/05/android-working-with-volley-library-1/) it contains full example about how to send params with post using volley – Zohaib Akram Aug 31 '16 at 05:15