概述
Android提供了两个令人兴奋的控件:AutoCompleteTextView和MultiAutoCompleteTextView,想了解它们的区别可以参考这里。但是在网上看了很多教程,其填充的内容都是有限的,基本都是写死在程序里。当我们在Google搜索框输入一些文字时,Google会提示相关的热门搜索词条,把这些词条填充到AutoCompleteTextView中将会是一件很棒的事情。这里就分享下如何从Google 的Suggest API获取实时Suggest,当然了也可以在自己的服务器上提供相应的API来实现特定的需求。
1、Google Suggest API 介绍:
URL:http://google.com/complete/search?output=toolbar&q=关键字&hl=国家
( 其中的hl参数: 中国:zh-CN
日本:ja
不带该参数的话,默认是英语语系)
返回的XML格式很简单,比较容易解析:(下面为该xml的解析工具类)
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.util.Log;
public class GoogleSuggestionAPI {
public static List<String> getSuggestion(String text)
throws ClientProtocolException, IOException, Exception {
List<String> result = new ArrayList<String>();
//
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
HttpClient httpget = new DefaultHttpClient();
HttpGet request = new HttpGet(
"http://google.com/complete/search?output=toolbar&q="
+ URLEncoder.encode(text).replace("%EF%BB%BF", "")
+ "&hl=ja");
request.setHeader("charset", "utf-8");
ResponseHandler<String> rh = new BasicResponseHandler();
String response = httpget.execute(request, rh);
InputStream is = new ByteArrayInputStream(
response.getBytes("UTF-8"));
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom;
dom = builder.parse(is);
org.w3c.dom.Element root = dom.getDocumentElement();
// Log.e("XML", root.toString());
NodeList items = root.getElementsByTagName("suggestion");
for (int i = 0; i < items.getLength(); i++) {
Element element = (Element) items.item(i);
result.add(element.getAttribute("data").toString());
// Log.e("encode", element.getAttribute("data").toString());
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
@SuppressWarnings("unused")
private static String convertStreamToString(InputStream is) {
Charset ch = Charset.forName("UTF8");
Log.i("ccc", "CHARSET=" + ch.displayName());
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, ch));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String originalStr = new String(sb.toString());
return sb.toString();
}
public static Document parse(String xml) {
Document doc = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
// encode the xml to UTF -8
ByteArrayInputStream encXML = new ByteArrayInputStream(
xml.getBytes("UTF8"));
doc = builder.parse(encXML);
// Log.e("XML parsing OK");
return doc;
} catch (Exception e) {
// log.error("Parser Error:" + e.getMessage());
// throw new ParsingFailedException(
// "Failed to parse XML : Document not well formed", e);
}
return doc;
}
}
2、 实现的思路
AutoCompleteTextView具体的用法就不讲了,具体的思路是为AutoCompleteTextView注册TextChangedListener,当Text改变时,启动一个AsyncTask调用上面的工具类,
解析完XML后,更新AutoCompleteTextView的adapter,因为具体的代码混入了很多自己项目的东西,不便于阅读,有需要的可以留言。
3、Yahoo提供的类似API
http://search.yahooapis.com/WebSearchService/V1/relatedSuggestion?appid=程序ID&query=关键字
(程序ID需要到这里申请)
最后
以上就是甜蜜毛衣为你收集整理的Android:采用Google Suggest API 实现 AutoCompleteTextView的填充的全部内容,希望文章能够帮你解决Android:采用Google Suggest API 实现 AutoCompleteTextView的填充所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复