2017-08-12 122 views
-5

我有一些json響應。 幫我分析在retrofit2 { 「狀態」 這樣的響應: 「成功」, 「代碼」: 「200」, 「消息」:{ 「老歌」: { 「ID」:「 8" , 「名稱」: 「TTTT」, 「album_image」: 「管理/上傳/ default_album_300_g4.png」 },{ 「ID」: 「9」, 「名稱」: 「GGGGG」, 「album_image」:「admin/upload/Jellyfish.jpg」 } ], 「Latest Songs」:[ { 「id:」9「, 「名」: 「GGGGG」, 「album_image」: 「管理/上傳/ Jellyfish.jpg」 } ] }} 所有 POJO類的json的是.....如何使用retrofit解析Json

public class Example { 

@SerializedName("status") 
@Expose 
private String status; 
@SerializedName("code") 
@Expose 
private String code; 
@SerializedName("messages") 
@Expose 
private Messages messages; 

public String getStatus() { 
    return status; 
} 

public void setStatus(String status) { 
    this.status = status; 
} 

public String getCode() { 
    return code; 
} 

public void setCode(String code) { 
    this.code = code; 
} 

public Messages getMessages() { 
    return messages; 
} 

public void setMessages(Messages messages) { 
    this.messages = messages; 
} 

}

public class Messages extends ArrayList<Messages> { 

@SerializedName("Old Songs") 
@Expose 
private List<OldSong> oldSongs = null; 
@SerializedName("Latest Songs") 
@Expose 
private List<LatestSong> latestSongs = null; 

public List<OldSong> getOldSongs() { 
    return oldSongs; 
} 

public void setOldSongs(List<OldSong> oldSongs) { 
    this.oldSongs = oldSongs; 
} 

public List<LatestSong> getLatestSongs() { 
    return latestSongs; 
} 

public void setLatestSongs(List<LatestSong> latestSongs) { 
    this.latestSongs = latestSongs; 
} 

}

public class OldSong { 

@SerializedName("id") 
@Expose 
private String id; 
@SerializedName("name") 
@Expose 
private String name; 
@SerializedName("album_image") 
@Expose 
private String albumImage; 

public String getId() { 
    return id; 
} 

public void setId(String id) { 
    this.id = id; 
} 

public String getName() { 
    return name; 
} 

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

public String getAlbumImage() { 
    return albumImage; 
} 

public void setAlbumImage(String albumImage) { 
    this.albumImage = albumImage; 
} 

}

public class LatestSong { 

@SerializedName("id") 
@Expose 
private String id; 
@SerializedName("name") 
@Expose 
private String name; 
@SerializedName("album_image") 
@Expose 
private String albumImage; 

public String getId() { 
    return id; 
} 

public void setId(String id) { 
    this.id = id; 
} 

public String getName() { 
    return name; 
} 

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

public String getAlbumImage() { 
    return albumImage; 
} 

public void setAlbumImage(String albumImage) { 
    this.albumImage = albumImage; 
} 

}

我的MainActivity類別是...

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener { 
Button send; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    send=(Button)findViewById(R.id.send); 
    send.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    /** 
    * Checking Internet Connection 
    */ 
    if (InternetConnection.checkConnection(getApplicationContext())) { 
     final ProgressDialog dialog; 
     /** 
     * Progress Dialog for User Interaction 
     */ 
     dialog = new ProgressDialog(MainActivity.this); 
     dialog.setTitle("json load"); 
     dialog.setMessage("getting json"); 
     dialog.show(); 
     //Creating an object of our api interface 
     ApiService api = RetroClient.getApiService(); 

     /** 
     * Calling JSON 
     */ 
     Call<Example> call = api.getMyJSON(); 
     /** 
     * Enqueue Callback will be call when get response... 
     */ 
     call.enqueue(new Callback<Example>() { 
      @Override 
      public void onResponse(Call<Example> call, Response<Example> response) { 
       dialog.dismiss(); 
       if(response.isSuccessful()) { 
        //want here to parse response 

       } else { 
        Snackbar.make(parentView,"something wrong" , Snackbar.LENGTH_LONG).show(); 
       } 
      } 

      @Override 
      public void onFailure(Call<Example> call, Throwable t) { 
       dialog.dismiss(); 
      } 
     }); 

    } else { 
     Snackbar.make(parentView, "internet_connection_not_available", Snackbar.LENGTH_LONG).show(); 
    } 
} 

}

+0

挖進谷歌你會發現很多答案。如果你遇到任何錯誤,然後張貼它 –

回答

1

你必須創造像下面的類。

public class LatestSong { 

@SerializedName("id") 
@Expose 
private String id; 
@SerializedName("name") 
@Expose 
private String name; 
@SerializedName("album_image") 
@Expose 
private String albumImage; 

public String getId() { 
return id; 
} 

public void setId(String id) { 
this.id = id; 
} 

public String getName() { 
return name; 
} 

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

public String getAlbumImage() { 
return albumImage; 
} 

public void setAlbumImage(String albumImage) { 
this.albumImage = albumImage; 
} 

} 

2類:

public class Messages { 

@SerializedName("Old Songs") 
@Expose 
private List<OldSong> oldSongs = null; 
@SerializedName("Latest Songs") 
@Expose 
private List<LatestSong> latestSongs = null; 

public List<OldSong> getOldSongs() { 
return oldSongs; 
} 

public void setOldSongs(List<OldSong> oldSongs) { 
this.oldSongs = oldSongs; 
} 

public List<LatestSong> getLatestSongs() { 
return latestSongs; 
} 

public void setLatestSongs(List<LatestSong> latestSongs) { 
this.latestSongs = latestSongs; 
} 

} 

3類:4

public class OldSong { 

@SerializedName("id") 
@Expose 
private String id; 
@SerializedName("name") 
@Expose 
private String name; 
@SerializedName("album_image") 
@Expose 
private String albumImage; 

public String getId() { 
return id; 
} 

public void setId(String id) { 
this.id = id; 
} 

public String getName() { 
return name; 
} 

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

public String getAlbumImage() { 
return albumImage; 
} 

public void setAlbumImage(String albumImage) { 
this.albumImage = albumImage; 
} 

} 

類:

public class Response { 

@SerializedName("status") 
@Expose 
private String status; 
@SerializedName("code") 
@Expose 
private String code; 
@SerializedName("messages") 
@Expose 
private Messages messages; 

public String getStatus() { 
return status; 
} 

public void setStatus(String status) { 
this.status = status; 
} 

public String getCode() { 
return code; 
} 

public void setCode(String code) { 
this.code = code; 
} 

public Messages getMessages() { 
return messages; 
} 

public void setMessages(Messages messages) { 
this.messages = messages; 
} 

} 

現在你必須設置Response.class你的改造。

+0

我有所有的pojo類,但我想解析這個Json –