如何从Android中的POJO课程中获取项目?

时间:2016-10-09 19:25:08

标签: java android json pojo

我正在尝试从我的Android应用程序中的API获取特定的项目。 以下是api的JSON响应:

{

    "response": {
        "items": [
            {
                "episode_id": 9599548,
                "type": "RECORDED",
                "title": "Adabule muferad 100916",
                "duration": 3165940,
                "explicit": false,
                "show_id": 1392538,
                "author_id": 7725967,
                "site_url": "https://www.spreaker.com/episode/9599548",
                "image_url": "https://d1bm3dmew779uf.cloudfront.net/large/f390b915e356de35055d971be5110dcb.jpg",
                "image_original_url": "https://d3wo5wojvuv7l.cloudfront.net/images.spreaker.com/original/f390b915e356de35055d971be5110dcb.jpg",
                "published_at": "2016-10-09 11:01:48",
                "download_enabled": true,
                "waveform_url": "https://d3770qakewhkht.cloudfront.net/episode_9599548.gz.json?v=qB6pQ6"
            }
        ],
        "next_url": "https://api.spreaker.com/v2/users/7725967/episodes?filter=listenable&last_id=9599548&limit=1"
    }

}

我有三个Java类Items,Response和RadioProgramInfo。 以下是他们的代码:

Items.java

public class Items
{
    public String duration;
    public String title;
    public String download_enabled;
    public String image_original_url;
    public String image_url;
    public String explicit;
    public String episode_id;
    public String author_id;
    public String show_id;
    public String type;
    public String waveform_url;
    public String published_at;
    public String site_url;
    public String getDuration ()
    {
        return duration;
    }

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

    public String getTitle ()
    {
        return title;
    }

    public void setTitle (String title)
    {
        this.title = title;
    }

    public String getDownload_enabled ()
    {
        return download_enabled;
    }

    public void setDownload_enabled (String download_enabled)
    {
        this.download_enabled = download_enabled;
    }

    public String getImage_original_url ()
    {
        return image_original_url;
    }

    public void setImage_original_url (String image_original_url)
    {
        this.image_original_url = image_original_url;
    }

    public String getImage_url ()
    {
        return image_url;
    }

    public void setImage_url (String image_url)
    {
        this.image_url = image_url;
    }

    public String getExplicit ()
    {
        return explicit;
    }

    public void setExplicit (String explicit)
    {
        this.explicit = explicit;
    }

    public String getEpisode_id ()
    {
        return episode_id;
    }

    public void setEpisode_id (String episode_id)
    {
        this.episode_id = episode_id;
    }

    public String getAuthor_id ()
    {
        return author_id;
    }

    public void setAuthor_id (String author_id)
    {
        this.author_id = author_id;
    }

    public String getShow_id ()
    {
        return show_id;
    }

    public void setShow_id (String show_id)
    {
        this.show_id = show_id;
    }

    public String getType ()
    {
        return type;
    }

    public void setType (String type)
    {
        this.type = type;
    }

    public String getWaveform_url ()
    {
        return waveform_url;
    }

    public void setWaveform_url (String waveform_url)
    {
        this.waveform_url = waveform_url;
    }

    public String getPublished_at ()
    {
        return published_at;
    }

    public void setPublished_at (String published_at)
    {
        this.published_at = published_at;
    }

    public String getSite_url ()
    {
        return site_url;
    }

    public void setSite_url (String site_url)
    {
        this.site_url = site_url;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [duration = "+duration+", title = "+title+", download_enabled = "+download_enabled+", image_original_url = "+image_original_url+", image_url = "+image_url+", explicit = "+explicit+", episode_id = "+episode_id+", author_id = "+author_id+", show_id = "+show_id+", type = "+type+", waveform_url = "+waveform_url+", published_at = "+published_at+", site_url = "+site_url+"]";
    }
}

Response.java

public class Response
{
    private Items[] items;

    private String next_url;

    public Items[] getItems ()
    {
        return items;
    }

    public void setItems (Items[] items)
    {
        this.items = items;
    }

    public String getNext_url ()
    {
        return next_url;
    }

    public void setNext_url (String next_url)
    {
        this.next_url = next_url;
    }

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

RadioProgramInfo.java

public class RadioProgramInfo
{
    private Response response;

    public Response getResponse ()
    {
        return response;
    }

    public void setResponse (Response response)
    {
        this.response = response;
    }

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

我正在尝试访问名为" site_url"的特定项目。它位于Items.java

我的主类中尝试访问site_url的代码是:

Items url2 = new Items();
    String streamURL = String.valueOf(url2)+"/shoutcast?force_http=true";

//    new HttpRequestTask().execute();
//    return true;

    String url = "http://api.spreaker.com/listen/episode/9451446/shoutcast?force_http=true";
    //String url2 = Items.class.getName(site_url);

    public MainActivity() {

    }


    //System.out.println(streamURL);


    public class HttpRequestTask extends AsyncTask<Void, Void, Items> {

        protected Items doInBackground(Void... params) {
            try {
                final String url = String.valueOf(streamURL);
                RestTemplate restTemplate = new RestTemplate();
                //restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
                Items streamlink = restTemplate.getForObject(url, Items.class);
                return streamlink;
            }

            catch (Exception e) {
                Log.e("MainActivity", e.getMessage(), e);
            }
            return url2;
        }
    }

当我运行我的程序(媒体播放器应用程序)时: 它告诉我url的值为null(我在程序执行时调试模式)。

如何正确访问我之后的JSON响应中的项目? 我真的被困在这个。

--- ----- UPDATE 以下是控制台的响应:

10-09 15:25:39.522 2586-2647/software.blackstone.com.salafimasjidradioseries E/MainActivity: 'messageConverters' must not be empty
                                                                                             java.lang.IllegalArgumentException: 'messageConverters' must not be empty
                                                                                                 at org.springframework.util.Assert.notEmpty(Assert.java:269)
                                                                                                 at org.springframework.web.client.HttpMessageConverterExtractor.<init>(HttpMessageConverterExtractor.java:53)
                                                                                                 at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:235)
                                                                                                 at software.blackstone.com.salafimasjidradioseries.MainActivity$HttpRequestTask.doInBackground(MainActivity.java:46)
                                                                                                 at software.blackstone.com.salafimasjidradioseries.MainActivity$HttpRequestTask.doInBackground(MainActivity.java:39)
                                                                                                 at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                                                                 at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                                 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                                                                 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                                                 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                                                 at java.lang.Thread.run(Thread.java:818)

....完整的MainActivity Code是这样的:

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import org.springframework.web.client.RestTemplate;

import java.io.IOException;
public class MainActivity extends AppCompatActivity {

    static MediaPlayer mPlayer;
    ImageButton buttonPlay;
    ImageButton buttonStop;

    Items url2 = new Items();
    String streamURL = String.valueOf(url2)+"/shoutcast?force_http=true";

//    new HttpRequestTask().execute();
//    return true;

    String url = "http://api.spreaker.com/listen/episode/9451446/shoutcast?force_http=true";
    //String url2 = Items.class.getName(site_url);

    public MainActivity() {

    }


    //System.out.println(streamURL);


    public class HttpRequestTask extends AsyncTask<Void, Void, Items> {

        protected Items doInBackground(Void... params) {
            try {
                final String url = String.valueOf(streamURL);
                RestTemplate restTemplate = new RestTemplate();
                //restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
                Items streamlink = restTemplate.getForObject(url, Items.class);
                return streamlink;
            }

            catch (Exception e) {
                Log.e("MainActivity", e.getMessage(), e);
            }

            return url2;
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        new HttpRequestTask().execute();
    }

2 个答案:

答案 0 :(得分:1)

我承认没有任何使用Spring库进行移动开发的经验。但是,您可以使用其他几种流行的库来完成您的目标。

下面是我使用你的DTO课程掀起的一个例子。我通过我的应用resources :articles 添加了这些依赖项:

build.gradle
  • OkHttp是一个用于创建+执行HTTP请求的库
  • Gson是一个用于(联合国)封送数据的库,如json

代码应该是不言自明的。我刚刚插入这些位来进行HTTP GET并将数据编组到AsyncTask中的DTO中。

compile 'com.squareup.okhttp:okhttp:2.7.5'
compile 'com.google.code.gson:gson:2.4'
希望有所帮助!

答案 1 :(得分:0)

由于您不确定要使用什么并且目前正在使用Spring,我建议您查看Retrofit,它正是您正在寻找的内容。它有很好的文档和很多你正在尝试做的例子。这里有一个非常好的介绍:square.github.io/retrofit