当前位置: 代码迷 >> Android >> 请问一个有关加载网页内容的有关问题
  详细解决方案

请问一个有关加载网页内容的有关问题

热度:61   发布时间:2016-05-01 21:18:45.0
请教一个有关加载网页内容的问题。
有时候如果不是想要用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=这一部分的呢?

------解决方案--------------------
查看网页html源代码就知道了
在说 你访问一下网站 抓个包什么的 就能知道了
都是http请求
------解决方案--------------------
我用你的网址查了一下,q=二逼青年,然后回车,网页会自动将我要翻译的"二逼青年"进行一次编码,变成"%B6%FE%B1%C6%C7%E0%C4%EA"。
在这种情况下你要得到"二逼青年"这个内容的话,那么你点击右键,查看源代码,就会看到里面包含了这个词语,这时你可以在android程序中抓取网页源代码,然后解析它,得到你想要得到的内容。

------解决方案--------------------
我还是把代码给你贴出来吧
Java code
//调用的地方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();    }}
------解决方案--------------------
我使用HTTPANALYZER实现了
  相关解决方案