【问题标题】:android JSON parsing How to make two http Connection one after the otherandroid JSON解析如何一个接一个地建立两个http连接
【发布时间】:2013-06-12 15:30:36
【问题描述】:

我正在使用烂番茄 api,出于某种原因,如果我想获得有关电影导演或工作室等电影的更多信息,我需要另一个 http 页面。这是我获取电影信息的网址。从这个 JSON 我得到了 id(“self”:“http://api.rottentomatoes.com/api/public/v1.0/movies/771205893.json”)。 我想使用那个 id - 我想与那个特定的 url 建立另一个 http 连接,并获得关于每部电影的更多信息。 这是电影信息 JSON:

{

"total": 591,

"movies": [{

"title": "Jack and Jill",

"year": 2011,

"runtime": "",

"release_dates": {"theater": "2011-11-11"},

"ratings": {

  "critics_score": -1,

  "audience_score": 90

},

"synopsis": "",

"posters": {

},

"abridged_cast": [

  {

    "name": "Al Pacino",

    "characters": []

  },

  {

    "name": "Adam Sandler",

    "characters": []

  },

  {

    "name": "Katie Holmes",

    "characters": []

  }

],

"links": {

  "self": "http://api.rottentomatoes.com/api/public/v1.0/movies/771205893.json",

}

}],

这是我的代码:

        if (response != null) {
            try {
                // convert the String response to a JSON object,
                // because JSON is the response format Rotten Tomatoes uses
                JSONObject jsonResponse = new JSONObject(response);

                // fetch the array of movies in the response
                JSONArray movies = jsonResponse.getJSONArray("movies");

                // add each movie's title to an array
                movieTitles = new String[movies.length()];
                for (int i = 0; i < movies.length(); i++) {
                    JSONObject movie = movies.getJSONObject(i);
                    movieTitles[i] = movie.getString("title");
                }

                try {
                    movieId = new String[movies.length()];
                    for (int i = 0; i < movies.length(); i++) {
                        JSONObject movie = movies.getJSONObject(i);
                        movieId[i] = movie.getString("id");

                    }

获得movieId后,我只想获得用户按下的电影ID 所以我用:

   public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long id) {

            Intent i = new Intent(getApplicationContext(),
                    AfterClickingMovieFromInternet.class);

            i.putExtra("movieName", movieTitles[position]);
            getMovieInfo( movieId[position]);

在 getMovieInfo(movieId[position]) 中,我试图与 api 建立第二个连接,但由于某种原因它不会执行..... 如果有人有任何想法,我很高兴知道!

【问题讨论】:

  • 您是否尝试过 Toast 或记录 e.toString();?请记住,您无法在 doInBackground 中敬酒,您将在 onPostExcute 中进行
  • 我尝试将给定的字符串发送给另一个活动,但发送的字符串为空

标签: java android json parsing url


【解决方案1】:

2 个连续连接有什么问题? 你检查图书馆了吗?

我会推荐这个 - https://github.com/kodart/Httpzoid 它在内部使用 GSON 并提供与对象一起使用的 API。所有 JSON 详细信息均已隐藏。

Http http = HttpFactory.create(context);
http.get("http://example.com/users")
    .handler(new ResponseHandler<User[]>() {
        @Override
        public void success(User[] users, HttpResponse response) {
        }
    }).execute();

【讨论】:

    猜你喜欢
    • 2018-01-31
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    相关资源
    最近更新 更多