如何解析JSON,POJO和Retrofit

时间:2015-09-24 13:28:44

标签: android json parsing retrofit pojo

您好我不知道该做什么我倾向于使用很多教程并且无法编写代码,这将起作用。

JSON:

asanas": [

{

    "id": 34,
    "name": "Easy Pranayama",
    "imgLarge": "/system/photos/asanas/34/large/34.jpg",
    "imgThumb": "/system/photos/asanas/34/thumb/34.jpg",
    "level_id": 1,
    "type_id": 9,
    "audio": "/system/audios/asanas/34/body/en/34.mp3",
    "title_audio": "/system/audios/asanas/34/title/en/34.mp3",
    "duration": 69,
    "fullDesc": "Lie down comfortably on your back so that your abdominal muscles are relaxed. Close your eyes.",
    "photo": "/system/photos/asanas/34/original/34.jpg",
    "sanskrit_name": "Sahaj Pranayama ",
    "sanskrit_audio_title": "/sanskrit_audio_titles/original/missing.png",
    "breathing_video": "/system/videos/asanas/34/breathing/34.mp4",
    "is_paid": false,
    "is_breathing": true,
    "old_identifier": "331",
    "old_pk": 772

},
{

    "id": 35,
    "name": "Easy Pranayama Advanced",
    "imgLarge": "/system/photos/asanas/35/large/35.jpg",
    "imgThumb": "/system/photos/asanas/35/thumb/35.jpg",
    "level_id": 2,
    "type_id": 9,
    "audio": "/system/audios/asanas/35/body/en/35.mp3",
    "title_audio": "/system/audios/asanas/35/title/en/35.mp3",
    "duration": 92,
    "fullDesc": "Lie down comfortably on your back so that your abdominal muscles are relaxed. Close your eyes. Bring your hands behind your head.",
    "photo": "/system/photos/asanas/35/original/35.jpg",
    "sanskrit_name": "Sahaj Pranayama Advanced",
    "sanskrit_audio_title": "/sanskrit_audio_titles/original/missing.png",
    "breathing_video": "/system/videos/asanas/35/breathing/35.mp4",
    "is_paid": true,
    "is_breathing": true,
    "old_identifier": "332",
    "old_pk": 773

},
{

    "id": 4,
    "name": "Ocean Sound",
    "imgLarge": "/system/photos/asanas/4/large/4.jpg",
    "imgThumb": "/system/photos/asanas/4/thumb/4.jpg",
    "level_id": 1,
    "type_id": 9,
    "audio": "/system/audios/asanas/4/body/en/4.mp3",
    "title_audio": "/system/audios/asanas/4/title/en/4.mp3",
    "duration": 221,
    "fullDesc": "Sit down comfortably with your back straight. Relax.",
    "photo": "/system/photos/asanas/4/original/4.jpg",
    "sanskrit_name": "Ujjayi Pranayama",
    "sanskrit_audio_title": "/sanskrit_audio_titles/original/missing.png",
    "breathing_video": "/system/videos/asanas/4/breathing/4.mp4",
    "is_paid": false,
    "is_breathing": true,
    "old_identifier": "301",
    "old_pk": 742

}
    ]

}

MainActivity:

public class MainActivity extends AppCompatActivity {


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


        MYApi myapi = (MYApi) new Builder().setLogLevel(LogLevel.FULL).setEndpoint(MYurl.BASE_URL).build().create(MYApi.class);
        tx = (TextView) findViewById(R.id.tx);

        myapi.getAsanas(new Callback<Asanas>() {
            public void success(Asanas asanas,Response response) {


                Log.d("L",String.valueOf(asanas.getAsanas().size()));
            }

            public void failure(RetrofitError error) {
            }
        });
    }

和POJO:

public class AsanaPOJO {

    private int id;
    private String name;
    private String imgLarge;
    private String imgThumb;
    private int level_id;
    private int type_id;
    private String audio;
    private String title_audio;
    private int duration;
    private String fullDesc;
    private String photo;
    private String sanskrit_name;
    private String sanskrit_audio_title;
    private String breathing_video;
    private boolean is_paid;
    private boolean is_breathing;
    private int old_identifier;
    private int old_pk;

    public AsanaPOJO() {
    }

    public String getSanskrit_audio_title ()
    {
        return sanskrit_audio_title;
    }

    public void setSanskrit_audio_title (String sanskrit_audio_title)
    {
        this.sanskrit_audio_title = sanskrit_audio_title;
    }

    public String getAudio ()
    {
        return audio;
    }

    public void setAudio (String audio)
    {
        this.audio = audio;
    }

    public String getImgThumb ()
    {
        return imgThumb;
    }

    public void setImgThumb (String imgThumb)
    {
        this.imgThumb = imgThumb;
    }

    public String getFullDesc ()
    {
        return fullDesc;
    }

    public void setFullDesc (String fullDesc)
    {
        this.fullDesc = fullDesc;
    }

    public String getImgLarge ()
    {
        return imgLarge;
    }

    public void setImgLarge (String imgLarge)
    {
        this.imgLarge = imgLarge;
    }

    public int getLevel_id ()
    {
        return level_id;
    }

    public void setLevel_id (int level_id)
    {
        this.level_id = level_id;
    }

    public String getPhoto ()
    {
        return photo;
    }

    public void setPhoto (String photo)
    {
        this.photo = photo;
    }

    public int getType_id ()
    {
        return type_id;
    }

    public void setType_id (int type_id)
    {
        this.type_id = type_id;
    }

    public int getOld_pk ()
    {
        return old_pk;
    }

    public void setOld_pk (int old_pk)
    {
        this.old_pk = old_pk;
    }

    public int getId ()
    {
        return id;
    }

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

    public int getDuration ()
    {
        return duration;
    }

    public void setDuration (int duration)
    {
        this.duration = duration;
    }

    public String getTitle_audio ()
    {
        return title_audio;
    }

    public void setTitle_audio (String title_audio)
    {
        this.title_audio = title_audio;
    }

    public boolean getIs_breathing ()
    {
        return is_breathing;
    }

    public void setIs_breathing (boolean is_breathing)
    {
        this.is_breathing = is_breathing;
    }

    public String getName ()
    {
        return name;
    }

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

    public int getOld_identifier ()
    {
        return old_identifier;
    }

    public void setOld_identifier (int old_identifier)
    {
        this.old_identifier = old_identifier;
    }

    public boolean getIs_paid ()
    {
        return is_paid;
    }

    public void setIs_paid (boolean is_paid)
    {
        this.is_paid = is_paid;
    }

    public String getSanskrit_name ()
    {
        return sanskrit_name;
    }

    public void setSanskrit_name (String sanskrit_name)
    {
        this.sanskrit_name = sanskrit_name;
    }

    public String getBreathing_video ()
    {
        return breathing_video;
    }

    public void setBreathing_video (String breathing_video)
    {
        this.breathing_video = breathing_video;
    }

    @Override
    public String toString(){
        return "Asana"+name+","+id;


    }

    public int checkBool(String s){
        if (s.equals("true")){
            return 1;
        }
        else return 0;
    }


}

public class Asanas {

    private List<AsanaPOJO> asanas;

    public Asanas() {
    }

    public List<AsanaPOJO> getAsanas ()
    {
        return this.asanas;
    }

    public void setAsanas (List<AsanaPOJO> asanas)
    {
        this.asanas = asanas;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [asanas = "+asanas+"]";
    }
}

怎么了?我做错了什么?错在哪里?方法成功后我没有任何对象,因为我没有任何东西。 Log.d( “L”,将String.valueOf(asanas.getAsanas()的大小()));此行不会在日志中显示任何内容。

09-24 17:09:33.692  30791-30808/com.example.user.asanasproject D/Retrofit﹕ ---> HTTP GET http://**.***.**.***:4444/asanas
09-24 17:09:33.712  30791-30808/com.example.user.asanasproject D/Retrofit﹕ ---> END HTTP (no body)
09-24 17:09:33.722  30791-30791/com.example.user.asanasproject I/Adreno200-EGLSUB﹕ <ConfigWindowMatch:2218>: Format RGBA_8888.
09-24 17:09:33.792  30791-30791/com.example.user.asanasproject D/memalloc﹕ /dev/pmem: Mapped buffer base:0x520f7000 size:9236480 offset:7700480 fd:58
09-24 17:09:33.812  30791-30791/com.example.user.asanasproject D/OpenGLRenderer﹕ Enabling debug mode 0
09-24 17:09:34.102  30791-30791/com.example.user.asanasproject D/memalloc﹕ /dev/pmem: Mapped buffer base:0x52ab3000 size:7700480 offset:6164480 fd:61
09-24 17:09:34.122  30791-30791/com.example.user.asanasproject D/CLIPBOARD﹕ Hide Clipboard dialog at Starting input: finished by someone else... !
09-24 17:09:36.812  30791-30808/com.example.user.asanasproject D/Retrofit﹕ <--- HTTP 200 http://**.***.**.***/asanas (2988ms)
09-24 17:09:36.872  30791-30808/com.example.user.asanasproject D/Retrofit﹕ : HTTP/1.1 200 OK
09-24 17:09:37.072  30791-30808/com.example.user.asanasproject D/Retrofit﹕ Cache-Control: max-age=0, private, must-revalidate
09-24 17:09:37.092  30791-30808/com.example.user.asanasproject D/Retrofit﹕ Connection: close
09-24 17:09:37.092  30791-30808/com.example.user.asanasproject D/Retrofit﹕ Content-Type: application/json; charset=utf-8
09-24 17:09:37.102  30791-30808/com.example.user.asanasproject D/Retrofit﹕ Date: Thu, 24 Sep 2015 14:09:33 GMT
09-24 17:09:37.102  30791-30808/com.example.user.asanasproject D/Retrofit﹕ ETag: W/"bf8e2b594c3f63f6ad9969ee543e6414"
09-24 17:09:37.112  30791-30808/com.example.user.asanasproject D/Retrofit﹕ Status: 200 OK
09-24 17:09:37.112  30791-30808/com.example.user.asanasproject D/Retrofit﹕ X-Android-Received-Millis: 1443103776613
09-24 17:09:37.112  30791-30808/com.example.user.asanasproject D/Retrofit﹕ X-Android-Sent-Millis: 1443103774432
09-24 17:09:37.112  30791-30808/com.example.user.asanasproject D/Retrofit﹕ X-Content-Type-Options: nosniff
09-24 17:09:37.122  30791-30808/com.example.user.asanasproject D/Retrofit﹕ X-Frame-Options: SAMEORIGIN
09-24 17:09:37.122  30791-30808/com.example.user.asanasproject D/Retrofit﹕ X-Request-Id: be43bf34-da6a-4872-9eb0-7cee5e9d40b5
09-24 17:09:37.122  30791-30808/com.example.user.asanasproject D/Retrofit﹕ X-Runtime: 1.945165
09-24 17:09:37.122  30791-30808/com.example.user.asanasproject D/Retrofit﹕ X-XSS-Protection: 1; mode=block
09-24 17:09:39.092  30791-30808/com.example.user.asanasproject D/Retrofit﹕ {"asanas":[{"id":34,"name":"Easy Pranayama","imgLarge":"/system/photos/asanas/34/large/34.jpg","imgThumb":"/system/photos/asanas/34/thumb/34.jpg","level_id":1,"type_id":9,"audio":"/system/audios/asanas/34/body/en/34.mp3","title_audio":"/system/audios/asanas/34/title/en/34.mp3","duration":69,"fullDesc":"Lie down comfortably on your back so that your abdominal muscles are relaxed. Close your eyes.","photo":"/system/photos/asanas/34/original/34.jpg","sanskrit_name":"Sahaj Pranayama ","sanskrit_audio_title":"/sanskrit_audio_titles/original/missing.png","breathing_video":"/system/videos/asanas/34/breathing/34.mp4","is_paid":false,"is_breathing":true,"old_identifier":"331","old_pk":772},{"id":35,"name":"Easy Pranayama Advanced","imgLarge":"/system/photos/asanas/35/large/35.jpg","imgThumb":"/system/photos/asanas/35/thumb/35.jpg","level_id":2,"type_id":9,"audio":"/system/audios/asanas/35/body/en/35.mp3","title_audio":"/system/audios/asanas/35/title/en/35.mp3","duration":92,"fullDesc":"Lie down comfortably on your back so that your abdominal muscles are relaxed. Close your eyes. Bring your hands behind your head.","photo":"/system/photos/asanas/35/original/35.jpg","sanskrit_name":"Sahaj Pranayama Advanced","sanskrit_audio_title":"/sanskrit_audio_titles/original/missing.png","breathing_video":"/system/videos/asanas/35/breathing/35.mp4","is_paid":true,"is_breathing":true,"old_identifier":"332","old_pk":773},{"id":4,"name":"Ocean Sound","imgLarge":"/system/photos/asanas/4/large/4.jpg","imgThumb":"/system/photos/asanas/4/thumb/4.jpg","level_id":1,"type_id":9,"audio":"/system/audios/asanas/4/body/en/4.mp3","title_audio":"/system/audios/asanas/4/title/en/4.mp3","duration":221,"fullDesc":"Sit down comfortably with your back straight. Relax.","photo":"/system/photos/asanas/4/original/4.jpg","sanskrit_name":"Ujjayi Pranayama","sanskrit_audio_title":"/sanskrit_audio_titles/original/missing.png","breathing_video":"/system/videos/asanas/4/breathing/4.mp4","is_paid":false,"is_breathing":true,"old_identifier":"301","old_pk":742},{"id":5,"name":"Purifying Breath 3x20","imgLarge":"/system/photos/asanas/5/large/5.jpg","imgThumb":"/system/photos/asanas/5/thumb/5.jpg","level_id":1,"type_id":9,"audio":"/system/audios/asanas/5/body/en/5.mp3","title_audio":"/system/audios/asanas/5/title/en/5.mp3","duration":25,"fullDesc":"Sit down comfortably so that your abdominal muscles are relaxed and your back is straight. Close your eyes.","photo":"/system/photos/asanas/5/original/5.jpg","sanskrit_name":"Kapalabhati Pranayama 3x20","sanskrit_audio_title":"/sanskrit_audio_titles/original/missing.png","breathing_video":"/system/videos/asanas/5/breathing/5.mp4","is_paid":true,"is_breathing":true,"old_identifier":"302","old_pk":743},{"id":6,"name":"Purifying Breath 3x30","imgLarge":"/system/photos/asanas/6/large/6.jpg","imgThumb":"/system/photos/asanas/6/thumb/6.jpg","level_id":2,"type_id":9,"audio":"/system/audios/asanas/6/body/en/6.mp3","title_audio":"/system/audios/asanas/6/title/en/6.mp3","duration":22,"fullDesc":"Sit down comfortably so that your abdominal muscles are relaxed and your back is straight. Close your eyes.","photo":"/system/photos/asanas/6/original/6.jpg","sanskrit_name":"Kapalabhati Pranayama 3x30","sanskrit_audio_title":"/sanskrit_audio_titles/original/missing.png","breathing_video":"/system/videos/asanas/6/breathing/6.mp4","is_paid":true,"is_breathing":true,"old_identifier":"303","old_pk":744},{"id":7,"name":"90 Purifying Breaths","imgLarge":"/system/photos/asanas/7/large/7.jpg","imgThumb":"/system/photos/asanas/7/thumb/7.jpg","level_id":3,"type_id":9,"audio":"/system/audios/asanas/7/body/en/7.mp3","title_audio":"/system/audios/asanas/7/title/en/7.mp3","duration":65,"fullDesc":"Sit down comfortably so that your abdominal muscles are relaxed and your back is straight. Close your eyes.","photo":"/system/photos/asanas/7/original/7.jpg","sanskrit_name":"Kapalabhati Pranayama 90","sanskrit_audio_title":"/sanskrit_audio_titles/original/missing.png","breathi
09-24 17:09:39.092  30791-30808/com.example.user.asanasproject D/Retrofit﹕ ng_video": //blablabla all JSON// 

09-24 17:09:39.162  30791-30808/com.example.user.asanasproject D/Retrofit﹕ <--- END HTTP (261559-byte body)
09-24 17:09:40.642  30791-30791/com.example.user.asanasproject D/FAILURE﹕ FAILURE

MYApi界面:

public interface MYApi {
    @GET("/asanas")
    void getAsanas(Callback<Asanas> callback);
}

0 个答案:

没有答案