有时候如果不是想要用webview来看整个网页的内容,而是只想用它或别的类来看网页上一部分文字或图片,可是怎么从网页上分析出某一部分的url是什么?我看了一个范例是这样:
private static final String queryString="http://dict.cn/mini.php?q=";
public void searchResult() {
String query = queryString + URLEncoder.encode(wordText.getText().toString().trim()) ;
try {
mWebView.loadUrl(query);
Log.e(TAG, "searchResult:"+query);
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "searchResult error");
}
}
这个是引用http://dict.cn/网页的词典查询的内容,在"http://dict.cn/mini.php?q=的q=后面加上要查询的词就会出来解释,可是coder是怎么知道mini.php?q=这一部分的呢?
private static final String queryString="http://dict.cn/mini.php?q=";
public void searchResult() {
String query = queryString + URLEncoder.encode(wordText.getText().toString().trim()) ;
try {
mWebView.loadUrl(query);
Log.e(TAG, "searchResult:"+query);
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "searchResult error");
}
}
这个是引用http://dict.cn/网页的词典查询的内容,在"http://dict.cn/mini.php?q=的q=后面加上要查询的词就会出来解释,可是coder是怎么知道mini.php?q=这一部分的呢?
在说 你访问一下网站 抓个包什么的 就能知道了
都是http请求
在这种情况下你要得到"二逼青年"这个内容的话,那么你点击右键,查看源代码,就会看到里面包含了这个词语,这时你可以在android程序中抓取网页源代码,然后解析它,得到你想要得到的内容。
HttpGet httpGet=new HttpGet();
String result=httpGet.httpGet("二逼青年", "gb2312");
//获取数据的类
public class HttpGet {
//str为要翻译的词语,encoding为编码方式
public String httpGet(String str, String encoding) throws Exception {
String result = "";
String strUrl = "http://dict.cn/mini.php?q=" + str;
URL url = new URL(strUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(1000 * 6);
if (httpURLConnection.getResponseCode() == 200) {
InputStream inputStream = httpURLConnection.getInputStream();
byte[] data = getByte(inputStream);
result = new String(data,encoding);
}
return result;
} private byte[] getByte(InputStream inputStream) throws Exception {
byte[] temp = new byte[1024];
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int len = -1;
while ((len = inputStream.read(temp)) != -1) {
byteArrayOutputStream.write(temp, 0, len);
}
inputStream.close();
byteArrayOutputStream.close();
return byteArrayOutputStream.toByteArray();
}
}
你能把你想做的功能描述一下吗?
代码在上面已经给你写出来了
我跟你说下做法吧:
1.在android界面上用一个文本框接收用户输入
2.将用户输入的字符串调用下面的方法HttpGet httpGet=new HttpGet();
String result=httpGet.httpGet(userInput, "gb2312");//这里userInput代表用户输入的要翻译的词语3.这样得到result就是翻译了userInput后的源代码,然后你解析result字符串,将你想要的东西解析出来,这样应该说得很清楚了吧?
我如果换一个网站叫stormier.dict.cn,我那里的链接就不是/mini.php?q=了啊!!!!
没准是/jdakldja.php?a= 没准是/dshhsdsaa.php?b=你怎么知道是什么!!!!!