Parsing XML with XmlPull_(MyMoviesWithHttpClient)

explicitly move forward through an XML document bit by bit, and skip any entity you encounter in which you’re not interested. That’s what a pull parser does.

You’re looking for a lightweight way to parse XML documents without having to keep them in memory at all times. You specifically want a parser that fetches the next token (such as an element or text) from the document explicitly rather than via callbacks (pull).

The benefit of this model is that it’s easy to skip forward until a certain element is reached, read its value, and then exit early if that’s all you need.


XmlPullParser and its related classes are—unlike SAX or DOM—not part of the standard JDK 5. They’re bundled with Android as a third-party library in the org.xmlpull.v1 package.

//This task resolves a movie’s IMDb ID (passed as a String) to a Movie object
public class GetMovieRatingTask extends AsyncTask<String,Void,Movie> {
	   private static final String API_KEY = "624645327f33f7866355b7b728f9cd98";
	   private static final String API_ENDPOINT = "http://api.themoviedb.org/2.1";
	   private static final int PARSER_KIND_SAX = 0;
	   private static final int PARSER_KIND_XMLPULL = 1;
	   private static final int PARSER_KIND_JSON = 2;
	   private int parserKind = PARSER_KIND_SAX;
	   private Activity activity;
	   public GetMovieRatingTask(Activity activity){
		   this.activity=activity;
	   }
	   
	@Override
	protected Movie doInBackground(String... params) {
		// TODO Auto-generated method stub
		try{
			//input:IMDb ID;output:movie object
			String imdbId=params[0];
			HttpClient httpClient=MyMovies.getHttpClient();
			String format=parserKind==PARSER_KIND_JSON?"json":"Xml";
			//movie address
			//language (/en) and the response format (/xml).
			//API key, which identifies our application on the web service
			//API key is shared among all users of the application
			String path= "/Movie.imdbLookup/en/" + format + "/" + API_KEY + "/"+ imdbId;
			HttpGet request=new HttpGet(API_ENDPOINT+path);
			//send service request
			HttpResponse response=httpClient.execute(request);
			InputStream data=response.getEntity().getContent();
			//parse response
			switch(parserKind){
			case PARSER_KIND_SAX:
				 return SAXMovieParser.parseMovie(data);
			case PARSER_KIND_XMLPULL:
				 return XmlPullMovieParser.parseMovie(data);
			case PARSER_KIND_JSON:
				 return JsonMovieParser.parseMovie(data);
			default:
				throw new RuntimeException("unsupported parser");
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	//If parsing succeeded, we read the relevant fields from the Movie object 
	//and show them in a pop-up dialog
	protected void onPostExecute(Movie movie){
	if(movie==null){
		Toast.makeText(activity, "Error", Toast.LENGTH_SHORT).show();
		return;
	}
	Dialog dialog=new Dialog(activity);
	dialog.setContentView(R.layout.movie_dialog);
	dialog.setTitle("IMDb rating for \"" + movie.getTitle() + "\"");
	TextView rating=(TextView)dialog.findViewById(R.id.movie_dialog_rating);
	rating.setText(movie.getRating());
	dialog.show();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值