豆瓣api_v2版本开始返回的是json,然后android里面自带json解析的类库,所以感觉非常容易就解析了。

本实例采用了把搜索结果json解析到ListView中,图片异步下载,点击某个item跳转到详细信息的界面。

解析过程:

List<SingleEntity> result = new ArrayList<SingleEntity>();
		String ch = URLEncoder.encode(key, "utf-8");

		String uString = "http://api.douban.com/v2/movie/search?q=" + ch;

		URL url = new URL(uString);

		StringBuilder builder = new StringBuilder();
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(url.openStream()));
		for (String s = bufferedReader.readLine(); s != null; s = bufferedReader
				.readLine()) {
			builder.append(s);
		}

		JSONArray jsonArray = null;
		try {
			JSONObject jsonObject = new JSONObject(builder.toString());
			jsonArray = jsonObject.getJSONArray("subjects");
		} catch (JSONException e) {

			e.printStackTrace();
		}

		for (int i = 0; i < jsonArray.length(); i++) {
			JSONObject jsonObject = (JSONObject) jsonArray.opt(i);
			try {
				JSONObject imagesObj = jsonObject.getJSONObject("images");

				SingleEntity movieBriefPojo = new SingleEntity();
				movieBriefPojo.setMovieName(jsonObject.getString("title"));
				movieBriefPojo.setAuthorName(jsonObject.getString("year"));
				movieBriefPojo.setFirstUrl(jsonObject.getString("id"));
				movieBriefPojo.setImageUrl(imagesObj.getString("small"));
				result.add(movieBriefPojo);

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		return result;
	}

这是搜索音乐然后解析结果:

public List<SingleEntity> findMusicXml(String keyworld) throws IOException {

		List<SingleEntity> result = new ArrayList<SingleEntity>();
		String ch = URLEncoder.encode(keyworld, "utf-8");

		String uString = "http://api.douban.com/v2/music/search?q=" + ch;

		URL url = new URL(uString);

		StringBuilder builder = new StringBuilder();
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(url.openStream()));
		for (String s = bufferedReader.readLine(); s != null; s = bufferedReader
				.readLine()) {
			builder.append(s);
		}

		JSONArray jsonArray = null;
		try {
			JSONObject jsonObject = new JSONObject(builder.toString());
			jsonArray = jsonObject.getJSONArray("musics");
		} catch (JSONException e) {

			e.printStackTrace();
		}

		for (int i = 0; i < jsonArray.length(); i++) {
			JSONObject jsonObject = (JSONObject) jsonArray.opt(i);
			try {

				JSONObject attrs = jsonObject.getJSONObject("attrs");
				JSONArray singerArray = attrs.getJSONArray("singer");

				SingleEntity movieBriefPojo = new SingleEntity();
				movieBriefPojo.setMovieName(attrs.getJSONArray("title")
						.getString(0));
				movieBriefPojo.setAuthorName(singerArray.getString(0));

				movieBriefPojo.setFirstUrl(jsonObject.getString("id"));
				movieBriefPojo.setImageUrl(jsonObject.getString("image"));
				result.add(movieBriefPojo);

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		return result;
	}

这是搜索书籍然后解析结果

public List<SingleEntity> findBookXml(String keyworld) throws Exception {
		{
			List<SingleEntity> result = new ArrayList<SingleEntity>();
			String ch = URLEncoder.encode(keyworld, "utf-8");

			String uString = "http://api.douban.com/v2/book/search?q=" + ch;

			URL url = new URL(uString);

			StringBuilder builder = new StringBuilder();
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(url.openStream()));
			for (String s = bufferedReader.readLine(); s != null; s = bufferedReader
					.readLine()) {
				builder.append(s);
			}
			
			JSONObject jsonObject = new JSONObject(builder.toString());
			JSONArray books = jsonObject.getJSONArray("books");
			
			for (int i = 0; i < books.length(); i++) {
				JSONObject book = (JSONObject) books.opt(i);
				
				SingleEntity bookSingleEntity = new SingleEntity();
				bookSingleEntity.setMovieName(book.getString("title"));//书本名称
				bookSingleEntity.setFirstUrl(book.getString("id"));//书籍具体的地址
				bookSingleEntity.setImageUrl(book.getString("image"));//书籍图片
				bookSingleEntity.setAuthorName(book.getJSONArray("author").optString(0));//作者
				
				result.add(bookSingleEntity);
			}
			
			return result;
		}
	}

其从图片的异步下载用了某个博客中的方法,但是我不记得详细地址了请原谅,下面是运行图


下载地址:http://download.csdn.net/detail/qq634416025/5296520

posted on 2013-04-24 17:22  纯洁的坏蛋  阅读(561)  评论(0编辑  收藏  举报