百度大家都会用,但是相信大家用百度的时候在输入框中随便输入什么内容,就会自动出现相关内容。有些人称之为自动补全,这个demo叫做在线查询比较贴近。
解析百度在线查询方法:
private List<String> onlineSearch(String content) throws IOException {
if (content == null)
content = "java";
String name = "a";
// String path = "http://nssug.baidu.com/su?wd="
// + URLEncoder.encode(singer, "UTF-8")
// + "&prod=mp3&oe=utf-8&callback=undefined";
String path="http://suggestion.baidu.com/su?wd="+URLEncoder.encode(content, "UTF-8")+"&p=3&cb=window.bdsug.sug";
System.out.println("path = " + path);
double fileLength = 0.0;
File parent = new File("D:\\path");
if (!parent.exists()) {
parent.mkdirs();
}
File mp3File = new File(parent, name);
System.out.println(mp3File);
OutputStream os = null;
InputStream is = null;
URL url = new URL(path);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// 此处必须伪造referer,否则会自动返回首页.分析后,与cookie无关
con.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon;)");
con.setRequestProperty("Accept-Encoding", "deflate");
con.setRequestProperty("referer", "http://nssug.baidu.com");
con.setDoInput(true);
con.connect();
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
is = con.getInputStream();
byte[] b = new byte[1024 * 5];
int length = -1;
os = new FileOutputStream(mp3File);
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
os.flush();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(mp3File), "gbk"));
String tmp = reader.readLine();
reader.close();
Pattern p = Pattern.compile("\\(.*?\\)");
Matcher m = p.matcher(tmp);
String result="";
while (m.find()) {
result+=m.group();
}
result = result.replaceAll("\\(", "");
result = result.replaceAll("\\)", "");
Gson gson = new Gson();
HashMap object = (HashMap) gson.fromJson(result, Object.class);
ArrayList list = (ArrayList) object.get("s");
return list;
}
测试类:
public static void main(String[] args) throws IOException {
OnlineTest test = new OnlineTest();
List<String> list = new ArrayList<String>();
list= test.onlineSearch(null);
for(int i=0;i<list.size();i++) {
System.out.println(list.get(i));
}
}
分享到:
相关推荐
仿百度输入框仿百度输入框仿百度输入框仿百度输入框仿百度输入框仿百度输入框仿百度输入框
可能根据输入模糊关联数据库,实现类似百度输入框
asp实现百度输入框效果 需要修改数据库 修改完后就可以使用
http://www.cnblogs.com/sufei/archive/2011/12/19/2293289.html详细介绍
仿百度输入框显示搜索内容
仿百度输入框智能提示,输入关键字后自动查找出相似的内容
通过输入框实时查询列表值,在电脑上好用,但是在微信客户端没有效果的解决方案
万能的ASP能否做出百度输入框关键字联想的效果? 希望大家共同研究 源代码是从网友wdyllff修改来的,我添加了数据库,修改了 部分代码,现已能输入关键字正常搜索,但有乱码BUG 希望ASP爱好者共同完善。 测试关键字...
输入页面和数据页面是两个页面,当用户输入时,会发送请求到数据页面,数据页面以json的形式返回数据
JS仿百度输入框 JS仿百度输入框 JS仿百度输入框 JS仿百度输入框
;height:20px;font-size:14pt;" placeholder="请输入a或b模拟效果" id="o" onkeyup="autoComplete.start(event)"> <!--自动完成 DIV--> </div>
jquery可输入选择框(类百度输入框效果)
类似于百度输入框历史记录的UI效果
输入一个或几个首汉字,联想查询like信息.
NULL 博文链接:https://hebenben.iteye.com/blog/665753
一个简单的HTML+PHP+MySQL的交互页面(输入框内容查询,HTML页面显示) HTML输入框输入内容,点击按键,把数据发送给PHP,PHP接收数据,然后访问MySQL数据库,数据库获取的结果,再通过HTML打印出来表格显示。
仿百度输入框自动匹配功能的JS代码
实现输入内容提示的功能(仿google_百度输入框提示) 具体可参见博文: http://blog.csdn.net/longteng1116/article/details/8816872#reply