koteswara D K
koteswara D K

Reputation: 628

using POJO Class with the GSON

I am creating a project with a Json sample array I have generated POJO class and I have a JSON which I need to parse. I have done all the setup but the app is not showing any error and it's not showing any data inside the app. here is my POJO Class generated.

 public class Android {

@SerializedName("ver")
@Expose
private String ver;
@SerializedName("name")
@Expose
private String name;
@SerializedName("api")
@Expose
private String api;

public String getVer() {
    return ver;
}

public void setVer(String ver) {
    this.ver = ver;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getApi() {
    return api;
}

public void setApi(String api) {
    this.api = api;
     }

     }

 public class    AndroidVersion {

@SerializedName("android")
@Expose
private List<Android> android = null;

public List<Android> getAndroid() {
    return android;
}

public void setAndroid(List<Android> android) {
    this.android = android;
}

   }

And my Json be like

{
  "android": [
    {
      "ver": "1.5",
      "name": "Cupcake",
      "api": "API level 3"
    },
    {
      "ver": "1.6",
      "name": "Donut",
      "api": "API level 4"
    },
    {
      "ver": "2.0 - 2.1",
      "name": "Eclair",
      "api": "API level 5 - 7"
    }
  ]
}

and i have parsed the data as shown below

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


            Gson gson=new GsonBuilder().create();
            try {
                JSONObject jsonObject=new JSONObject(response);
                JSONArray jsonArray=jsonObject.getJSONArray("android");
                Android averion= gson.fromJson(jsonObject.toString(),Android.class);

                List<Android> verionlist=new ArrayList<>();
                verionlist.add(averion);
                adapter=new ReccyclerAdapter(verionlist);
                recyclerView.setAdapter(adapter);

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


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

        }
    });
    MySingleton.getInstance(this).addToRequestQueue(stringRequest);
}

can any one tell me what is the problem with my code. y its not showing the data. if you know any blog or a site to learn the about how to use the POJO with all type of JSON in android

here is the adapter class.

    public class ReccyclerAdapter extends 
  RecyclerView.Adapter<ReccyclerAdapter.MYViewHolder> {
private String TAG=getClass().getSimpleName();
private List<Android> list=new ArrayList<>();
ReccyclerAdapter(List<Android> list){
    this.list=list;

}

@NonNull
@Override
public MYViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.data_row,viewGroup,false);
    return new MYViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MYViewHolder myViewHolder, int i) {

    myViewHolder.api_level.setText(list.get(i).getApi());
    myViewHolder.name.setText(list.get(i).getName());
    myViewHolder.version.setText(list.get(i).getVer());

}


@Override
public int getItemCount() {
    return list.size();
}
public static class MYViewHolder extends RecyclerView.ViewHolder {
    TextView version,name,api_level;
    public MYViewHolder(@NonNull View itemView) {
        super(itemView);
        name=(TextView)itemView.findViewById(R.id.name);
        version=(TextView)itemView.findViewById(R.id.vesrion);
        api_level=(TextView)itemView.findViewById(R.id.api_level);

    }
}
 }

Upvotes: 0

Views: 1398

Answers (4)

koteswara D K
koteswara D K

Reputation: 628

i found the answer as i wanted.here is the solution

   Gson gson = new Gson();

    AndroidVersion generalInfoObject = gson.fromJson(response, AndroidVersion.class);

    adapter=new ReccyclerAdapter(generalInfoObject.getAndroid());
    recyclerView.setAdapter(adapter);

and i changed the Android version class to

 public class  AndroidVersion {

 @SerializedName("android")
 List<Android> android;

 public List<Android> getAndroid() {
    return android;
 }

 public void setAndroid(List<Android> android) {
    this.android = android;
 }

 }

thanks for giving me solution hints with your solutions

Upvotes: 0

Dinesh Shekhawat
Dinesh Shekhawat

Reputation: 539

You don't need to go through the pain of individually adding each JSON object to the list. Instead a better way would be to directly convert the Response String using TypeToken. Gson library allows you to do this simply as follows:

Gson gson = new Gson();

Type type = new TypeToken<List<Android>>() {}.getType();

List<Android> versionList = gson.fromJson(response, type);

or if you want to do your task in just one line of code

List<Android> versionList = new Gson().fromJson(response, new TypeToken<List<Android>>() {}.getType());

Upvotes: 0

Tejas Pandya
Tejas Pandya

Reputation: 4087

Try this

 Gson gson = new Gson();
            try {
                JSONObject jsonObject=new JSONObject(response);
                JSONArray jsonArray=jsonObject.getJSONArray("android");



                Android averion= gson.fromJson(jsonObject.toString(),Android.class);
                 //here jsonObject.toString() is  your AndroidVersion not Android .

                List<Android> verionlist=new ArrayList<>();
                verionlist.add(averion);
                adapter=new ReccyclerAdapter(verionlist);
                recyclerView.setAdapter(adapter);

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

Upvotes: 0

Sandeep Parish
Sandeep Parish

Reputation: 2228

Put this Android averion= gson.fromJson(jsonObject.toString(),Android.class); code in a for loop like:

This JSONObject jsonObject=new JSONObject(response); has your response not obj at 0,1 and 2 positions obj.

 List<Android> verionlist=new ArrayList<>();

create new ArrayList in orcreate of activity

and than use for loop to add each object from android array in arraylist like:

for(int i=0;i<jsonArray.length(); i++){
  Android averion= gson.fromJson(jsonArray.get(i).toString(),Android.class);
  verionlist.add(averion);
}

 adapter=new ReccyclerAdapter(verionlist);
 recyclerView.setAdapter(adapter);

It will work for you.

Upvotes: 1

Related Questions