1.返回一个参数时
private String getResultByIsbn(String isbn) throws ClientProtocolException,
IOException, IllegalStateException, XmlPullParserException {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url + isbn);
HttpResponse response = client.execute(get);
return getBookInfo(response.getEntity().getContent());
}
private String getBookInfo(InputStream inputStream)
throws XmlPullParserException, IOException {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
parser.setInput(inputStream, "UTF-8");
StringBuilder builder = new StringBuilder();
for (int i = parser.getEventType(); i != XmlPullParser.END_DOCUMENT; i = parser
.next()) {
if (i == XmlPullParser.START_TAG
&& parser.getName().equals("title")) {
builder.append("名称: ").append(parser.nextText());
break;
}
}
return builder.toString();
}
2.显示详细内容时
try {
bookInfo = getResultByIsbn(this.getIntent().getExtras().getString(
"ISBN"));
} catch (Exception e) {
throw new RuntimeException(e);
}
resultWeb = (WebView) this.findViewById(R.id.resultWeb);
this.resultWeb.getSettings().setSupportZoom(false);
this.resultWeb.getSettings().setJavaScriptCanOpenWindowsAutomatically(
true);
this.resultWeb.getSettings().setJavaScriptEnabled(true);
this.resultWeb.loadUrl("file:///android_asset/results.html");
this.resultWeb.addJavascriptInterface(new Object() {
public String getBookName() {
return bookInfo.getName();
}
public String getBookSummary() {
return bookInfo.getSummary();
}
public String getBookImageUrl() {
return bookInfo.getImageUrl();
}
public String getBookAuthor() {
return bookInfo.getAuthor();
}
}, "searchResult");
}
private BookInfo getResultByIsbn(String isbn)
throws ClientProtocolException, IOException, IllegalStateException,
XmlPullParserException {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url + isbn);
HttpResponse response = client.execute(get);
return getBookInfo(response.getEntity().getContent());
}
private BookInfo getBookInfo(InputStream inputStream)
throws XmlPullParserException, IOException {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
parser.setInput(inputStream, "UTF-8");
BookInfo bookInfo = new BookInfo();
for (int i = parser.getEventType(); i != XmlPullParser.END_DOCUMENT; i = parser
.next()) {
if (i == XmlPullParser.START_TAG
&& parser.getName().equals("attribute")
&& parser.getAttributeValue(0).equals("title")) {
bookInfo.setName(parser.nextText());
Log.v("soubook", "title>>" + bookInfo.getName());
continue;
}
if (i == XmlPullParser.START_TAG
&& parser.getName().equals("attribute")
&& parser.getAttributeValue(0).equals("author")) {
bookInfo.setAuthor(parser.nextText());
Log.v("soubook", "author>>" + bookInfo.getAuthor());
continue;
}
if (i == XmlPullParser.START_TAG
&& parser.getName().equals("summary")) {
bookInfo.setSummary(parser.nextText());
Log.v("soubook", "summary>>" + bookInfo.getSummary());
continue;
}
if (i == XmlPullParser.START_TAG && parser.getName().equals("link")) {
if (parser.getAttributeValue(1).equals("image")) {
bookInfo.setImageUrl(parser.getAttributeValue(0));
Log.v("soubook", "iamge>>" + bookInfo.getImageUrl());
}
continue;
}
}
Log.v("soubook", ">>>>> parse end.");
return bookInfo;
}
private String getResultByIsbn(String isbn) throws ClientProtocolException,
IOException, IllegalStateException, XmlPullParserException {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url + isbn);
HttpResponse response = client.execute(get);
return getBookInfo(response.getEntity().getContent());
}
private String getBookInfo(InputStream inputStream)
throws XmlPullParserException, IOException {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
parser.setInput(inputStream, "UTF-8");
StringBuilder builder = new StringBuilder();
for (int i = parser.getEventType(); i != XmlPullParser.END_DOCUMENT; i = parser
.next()) {
if (i == XmlPullParser.START_TAG
&& parser.getName().equals("title")) {
builder.append("名称: ").append(parser.nextText());
break;
}
}
return builder.toString();
}
2.显示详细内容时
try {
bookInfo = getResultByIsbn(this.getIntent().getExtras().getString(
"ISBN"));
} catch (Exception e) {
throw new RuntimeException(e);
}
resultWeb = (WebView) this.findViewById(R.id.resultWeb);
this.resultWeb.getSettings().setSupportZoom(false);
this.resultWeb.getSettings().setJavaScriptCanOpenWindowsAutomatically(
true);
this.resultWeb.getSettings().setJavaScriptEnabled(true);
this.resultWeb.loadUrl("file:///android_asset/results.html");
this.resultWeb.addJavascriptInterface(new Object() {
public String getBookName() {
return bookInfo.getName();
}
public String getBookSummary() {
return bookInfo.getSummary();
}
public String getBookImageUrl() {
return bookInfo.getImageUrl();
}
public String getBookAuthor() {
return bookInfo.getAuthor();
}
}, "searchResult");
}
private BookInfo getResultByIsbn(String isbn)
throws ClientProtocolException, IOException, IllegalStateException,
XmlPullParserException {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url + isbn);
HttpResponse response = client.execute(get);
return getBookInfo(response.getEntity().getContent());
}
private BookInfo getBookInfo(InputStream inputStream)
throws XmlPullParserException, IOException {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
parser.setInput(inputStream, "UTF-8");
BookInfo bookInfo = new BookInfo();
for (int i = parser.getEventType(); i != XmlPullParser.END_DOCUMENT; i = parser
.next()) {
if (i == XmlPullParser.START_TAG
&& parser.getName().equals("attribute")
&& parser.getAttributeValue(0).equals("title")) {
bookInfo.setName(parser.nextText());
Log.v("soubook", "title>>" + bookInfo.getName());
continue;
}
if (i == XmlPullParser.START_TAG
&& parser.getName().equals("attribute")
&& parser.getAttributeValue(0).equals("author")) {
bookInfo.setAuthor(parser.nextText());
Log.v("soubook", "author>>" + bookInfo.getAuthor());
continue;
}
if (i == XmlPullParser.START_TAG
&& parser.getName().equals("summary")) {
bookInfo.setSummary(parser.nextText());
Log.v("soubook", "summary>>" + bookInfo.getSummary());
continue;
}
if (i == XmlPullParser.START_TAG && parser.getName().equals("link")) {
if (parser.getAttributeValue(1).equals("image")) {
bookInfo.setImageUrl(parser.getAttributeValue(0));
Log.v("soubook", "iamge>>" + bookInfo.getImageUrl());
}
continue;
}
}
Log.v("soubook", ">>>>> parse end.");
return bookInfo;
}