2016-12-13 1 views
0

JSON-Datei wie folgt aussehen:erhalten json Array mit retorift 2

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

dies meine Schnittstelle ist:

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(); 
} 

das ist mein JSONResponse:

public class JSONResponse { 

    private Deatails[] posts; 

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

und das ist mein deatails Klasse:

public class Deatails { 

    private String title; 


    public String getTitle() { 

     return title; 
    } 

} 

wie Sie Titel von Posts erhalten und gut funktionieren, aber nicht wissen, wie URL von Anhängen in Posts erhalten !?

+0

Keine Notwendigkeit, so viel Aufwand zu geben, verwenden Sie [jsonschema2pojo] (http://www.jsonschema2pojo.org/), um Modellklasse zu generieren, müssen Sie nur Antwort einfügen und Klasse wird generiert. –

+0

Ich denke, das könnte helfen http://stackoverflow.com/questions/31112124/getting-simple-json-object-response-using-retrofit-library – Raghavendra

+0

Ihr JSON ist nicht gültig. Bitte korrigieren Sie das zuerst. –

Antwort

0

Erstellen Sie eine Klasse

public class UrlAttachment{ 
    String url; 

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

    public String getUrl(){ 
     return url; 
    } 
} 

Dann in Ihre nächste Wohnung Klasse

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; 
} 
} 

Hinweis: Ihre Json sein sollte wie folgt

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

Wenn Sie wollen, dass Stimme meine Frage zu beantworten, antworten Sie bitte vollständig – erfan

+0

aktualisieren Sie einfach meine Antwort –

+0

ich nicht testen ur-Code aber scheint nicht wahr! Wie kann ich URLs von Anhängen erhalten? Ich habe sogar keine URL gesehen! – erfan

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; 
    } 
} 
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()); 
          } 
         }); 
+0

wenn Sie nicht wissen Antwort nicht Ihren Code hier einfügen – erfan

+0

Dies ist die richtige Lösung, die ich gerade entwickelt habe. – vasupujy