0

This is my json object structure:

   object   {1}

      products  [3]

            0   {14}

            1   {14}

            2   {14}

and here is my code:

gitapi.java

public interface gitapi {
    @GET("/admin/API/{user}")
    public void getFeed(@Path("user") String user, Callback<gitmodel> response);    
}

MainActivity.java

Button click;
TextView tv;
EditText edit_user;
ProgressBar pbar;
String API = "http://xxyyzz.com";                         //BASE URL

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    click = (Button) findViewById(R.id.button);
    tv = (TextView) findViewById(R.id.tv);
    edit_user = (EditText) findViewById(R.id.edit);
    pbar = (ProgressBar) findViewById(R.id.pb);
    pbar.setVisibility(View.INVISIBLE);

    click.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String user = edit_user.getText().toString();
            pbar.setVisibility(View.VISIBLE);

            //Retrofit section start from here...
            RestAdapter restAdapter = new RestAdapter.Builder()
                    .setEndpoint(API).build();                                        //create an adapter for retrofit with base url

            gitapi git = restAdapter.create(gitapi.class);                            //creating a service for adapter with our GET class

            //Now ,we need to call for response
            //Retrofit using gson for JSON-POJO conversion

            git.getFeed(user, new Callback<gitmodel>() {
                @Override
                public void success(gitmodel gitmodel, Response response) {
                    //we get json object from github server to our POJO or model class

                    tv.setText("getTitle :" + gitmodel.getTitle());

                    pbar.setVisibility(View.INVISIBLE);                               //disable progressbar
                }

                @Override
                public void failure(RetrofitError error) {
                    tv.setText(error.getMessage());
                    pbar.setVisibility(View.INVISIBLE);                               //disable progressbar
                }
            });
        }
    });
}

The problem is I dont know how to access the inner json objects. Is there a way to do this.

from jsonschemaa2pojo.com i got the pojo file as 2 files one for gitmodel.java and other for product.java. My Question is how can i access the objects within the json object.

Ex: you can see the tv.setText("getTitle :" + gitmodel.getTitle()); in MainActivity.java which is what i want to achieve. i want to call the methods in order to access the inner object. Thank you...

4
  • gitmodel.getInnerObject() ... or gitmodel.getObjectList().get(x) ... there are bazillions topic about json, please don't be lazy and do some reasearch before asking Commented Jun 26, 2015 at 8:36
  • Thanks for the reply. I want to access each elements of the json object however nested and complex it is. In this case i want to access the elements of the inner object. Commented Jun 26, 2015 at 8:38
  • gitmodel.getInnerObject() should returns InnerModel(similar to gitmodel but should define inner complex property)... InnerModel should have innerproperty and setter/getter ... so now you can use gitmodel.getInnerObject().getInnerProperty() ... in other words: you have to define class for every complex type ... please use google to search an examples ... or at least read gson documentation Commented Jun 26, 2015 at 8:42
  • I tried that, but it didn't work. I might be doing something wrong. This is what i did: I created the gitmodel class which contains a getProducts() and setProducts() and a seperate product class with all setter/getter methods in it. Now i tried making a method in gitmodel which says getInnerObjectElements() in which i create a product object and call the product.getElement() to get the innerObject's Elements. I am a noob. Please help me out. thanks Commented Jun 26, 2015 at 8:54

1 Answer 1

4

You have to create POJO using this link : http://www.jsonschema2pojo.org/

In this link select Source type: JSON and Annotation style: GSON and after that click on preview it will create pojo as per your Json and you will easily parse json.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.