0

Here is the Layout i am using to set the text :Image Here, please Click

Here is the arraylist i am using :

 myTeacherModelLists.add(new MyTeacherModel(R.drawable.home_google_classroom, "Classroom","sds"));
        myTeacherModelLists.add(new MyTeacherModel(R.drawable.home_news_alerts, "Notice","sds"));
        myTeacherModelLists.add(new MyTeacherModel(R.drawable.home_school_rank, "Announcement","sds"));
        myTeacherModelLists.add(new MyTeacherModel(R.drawable.school_diary, "School Diary","sds"));
        myTeacherModelLists.add(new MyTeacherModel(R.drawable.home_absent_report, "Absent report","sds"));
        myTeacherModelLists.add(new MyTeacherModel(R.drawable.examination, "Examination","sds"));
        myTeacherModelLists.add(new MyTeacherModel(R.drawable.home_events, "Events","sds"));
        myTeacherModelLists.add(new MyTeacherModel(R.drawable.edu_forum_finals, "Edu Forum","sds"));
        myTeacherModelLists.add(new MyTeacherModel(R.drawable.home_resources, "Edu Bank","sds"));
        myTeacherModelLists.add(new MyTeacherModel(R.drawable.home_help, "Help","sds"));

Here is my JSON response :

{
        "Badges": {
            "Notice": 0,
            "Event": 0
        },
        "Response": {
            "ResponseVal": 1,
            "Reason": "Success! Record Found."
        }
    }

Here is the StringRequest code , I am using Volley :

 StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.e("URL : ", URL);

            try {
                JSONObject jsonObject = new JSONObject("Badges");
                Log.e("Response is : ", String.valueOf(jsonObject));
            } catch (JSONException e) {
                e.printStackTrace();
            }


        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

Here is my Model Class :

public class MyTeacherModel {

Integer images;
String text;
String Badges;

public MyTeacherModel(){

}

public MyTeacherModel(Integer images, String text, String badges) {
    this.images = images;
    this.text = text;
    Badges = badges;
}

public Integer getImages() {
    return images;
}

public String getText() {
    return text;
}

public String getBadges() {
    return Badges;
}

Currently I am using the hardcoded text, how can i fetch the data from this JSON to the TextViews ?

Rohit
  • 27
  • 6

3 Answers3

2

Please try this code

StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {

        try {
            JSONObject jsonObject = new JSONObject(response);

            JSONObject jsonObjectBadges = jsonObject.getJSONObject("Badges");
            int Notice = jsonObjectBadges.getInt("Notice");
            int Event = jsonObjectBadges.getInt("Event");

            JSONObject jsonObjectResponse = jsonObject.getJSONObject("Response");
            int ResponseVal = jsonObjectResponse.getInt("ResponseVal");
            String Reason = jsonObjectResponse.getString("Reason");

            Log.e("URL_DATA : ", "Notice : " + Notice  + "\n" + 
            "Event : " + Event  + "\n" + 
            "ResponseVal : " + ResponseVal  + "\n" +
            "Reason : " + Reason  + "\n");

        } catch (JSONException e) {
            e.printStackTrace();
        }


    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

    }
});

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
0

You need to use java json object to serialize and deserialize json text to java models. For more better experience , use libraries like Gson , Moshi , Jackson and etc

try {
            JSONObject jsonObject = new JSONObject("Badges");
            Log.e("Response is : ", String.valueOf(jsonObject));
        } catch (JSONException e) {
            e.printStackTrace();
        }

Try this :

try {
            JSONObject jsonObject = new JSONObject(response);
            Log.e("Response is : ", String.valueOf(jsonObject));
        } catch (JSONException e) {
            e.printStackTrace();
        }

That response is your json string and valid to initialize.

Ali Khoshraftar
  • 521
  • 1
  • 4
  • 16
0

You need to create model classes based on you json response in following way:

If your JSON response:

{
    "Badges": {
         "Notice": 0,
         "Event": 0
     },
    "Response": {
         "ResponseVal": 1,
         "Reason": "Success! Record Found."
     }
}

Then your model classes should be (you need to create three model classes) :

class NetworkResponse {
   Badges Badges;  // Note: name of your variable should be same as the field received in 
                   // JSON response
   Response Response;
}

class Badges {
  int Notice;
  int Event;
}

class Response {
  int ResponseVal;
  String Reason;
}

Now inside your string request code made by volley , you need to parse the JSON response using GSON library in following way.

StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.e("URL : ", URL);

            try {
                Gson gson = new Gson();
                NetworkResponse responseObject = gson.fromJson(response,NetworkResponse.class)

            } catch (JSONException e) {
                e.printStackTrace();
            }


        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

Here from your responseObject you access your values and populate them in your text views in following way:

textView.setText(responseObject.Badges.Notice)
textView.setText(responseObject.Badges.Event)

textView.setText(responseObject.Response.ResponseVal)
textView.setText(responseObject.Response.Reason)
Prathmesh Swaroop
  • 611
  • 1
  • 5
  • 13