您的位置:首页 > 理论基础 > 计算机网络

Parsing XML with XmlPull_(MyMoviesWithHttpClient)

2013-01-31 13:56 295 查看
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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: