0

have json file like this :

 "posts": [
    {
      "title": "hello"
    },
 "attachments": [
      "url":"some url"
  ]
]

this is my interface:

public interface RequestInterface {

    @GET("http://memaraneha.ir/category/%d9%85%d9%82%d8%a7%d9%84%d8%a7%d8%aa/?json=1")
    Call<JSONResponse> getJSON();
}

this is my JSONResponse:

public class JSONResponse {

    private Deatails[] posts;

    public Deatails[] getPosts() {
        return posts;
    }
}

and this is my deatails class :

public class Deatails {

    private String title;


    public String getTitle() {

        return title;
    }

}

as you see get title from posts and work fine but dont know how get url from attachments inside posts!?

Erfan
  • 3,059
  • 3
  • 22
  • 49
  • No need to give so much efforts, use [jsonschema2pojo](http://www.jsonschema2pojo.org/) to generate model class, you just need to paste response and class will be generated. – Ravi Dec 13 '16 at 12:58
  • I think this might help http://stackoverflow.com/questions/31112124/getting-simple-json-object-response-using-retrofit-library – Raghavendra Dec 13 '16 at 12:58
  • Your json is not valid one. Please correct that first. – Sahil Munjal Dec 13 '16 at 13:18

3 Answers3

0

Create a class

public class UrlAttachment{
   String url;

   public void setUrl(String url){
         this.url = url;
   }

   public String getUrl(){
        return url;
   }
}

Then in your Deatails class

public class Deatails {

private String title;
private List<UrlAttachments> attachments;

public String getTitle() {

    return title;
}

public List<UrlAttachment> getAttachment(){
    return attachments;
}

public void setAttachment(List<UrlAttachment> list){
    this.attachments = list;
}
}

Note: Your Json Should be like this

"posts": [
  {
    "title": "hello"
    "attachments": [ 
        {
           "url":"some url"
        }
    ]
  },
]
Tabish Hussain
  • 852
  • 5
  • 13
0
public class JSONResponse {

    private Deatails[] posts;
    private Deatails2[] attachments;

    public Deatails[] getPosts() {
        return posts;
    }

    public Deatails2[] getAttachments() {
        return attachments;
    }
}


public class Deatails2 {
     private String url;

     public String getUrl() {
        return url;
    }
}
faranjit
  • 1,567
  • 1
  • 15
  • 22
0
//First of you add dependency

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.google.code.gson:gson:2.4'
    compile 'com.squareup.okhttp:okhttp:2.5.0'
    // compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
    compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
}

//then create interface

    public interface RequestInterface {
       @GET("json=1")
            Call<JSONResponse> getJSON();  
    } 


        // In Activity

        public static String BASE="http://memaraneha.ir/category/%d9%85%d9%82%d8%a7%d9%84%d8%a7%d8%aa/?" 

              Retrofit retrofit = new Retrofit.Builder()
                                .baseUrl(API_URL_BASE)
                                .addConverterFactory(GsonConverterFactory.create())
                                .build();

                        RequestInterface service = retrofit.create(RequestInterface .class);

                        // Asynchronous Call in Retrofit 2.0-beta2
                        Call<GetRoleData> call = service.getJSON();
                        call.enqueue(new Callback<GetRoleData>() {
                            @Override
                            public void onResponse(Response<GetRoleData> response, Retrofit retrofit) {
                                ArrayList<GetRoleData.Roles> arrayList = response.body().getUserRoles();
                                if (arrayList != null) {
                                    Log.i(LOG_TAG, arrayList.get(0).getName());
                                }
                            }

                            @Override
                            public void onFailure(Throwable t) {
                                Log.e(LOG_TAG, t.toString());
                            }
                        });
vasupujy
  • 442
  • 2
  • 11