工具:android studio
使用URLConnection 连接网络获取网页数据,把联网放在异步线程里执行 使用AsyncTask
extends AsyncTask<String, Integer, StringBuffer> 在 doInBackground 方法里执行联网的方法,
onPostExecute(StringBuffer result) 方法用于在执行完后台任务后更新UI,显示结果
连接网络有get 和 post 方法,
public void doGetURL(String url) throws Exception {
URL localURL = new URL(url);
URLConnection connection = localURL.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
if (httpURLConnection.getResponseCode() >= 300) {
throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
}
inputStream = httpURLConnection.getInputStream();
inputStreamReader = new InputStreamReader(inputStream);
reader = new BufferedReader(inputStreamReader);
while ((tempLine = reader.readLine()) != null) {
tempHTML.append(tempLine);
}
}
private void doPostURL(String url) throws Exception{
URL localURL = new URL(url);
URLConnection connection = localURL.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(url.length()));
outputStream = httpURLConnection.getOutputStream();
outputStreamWriter = new OutputStreamWriter(outputStream);
outputStreamWriter.write(url.toString());
outputStreamWriter.flush();
if (httpURLConnection.getResponseCode() >= 300) {
throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
}
inputStream = httpURLConnection.getInputStream();
inputStreamReader = new InputStreamReader(inputStream);
reader = new BufferedReader(inputStreamReader);
while ((tempLine = reader.readLine()) != null) {
tempHTML.append(tempLine);
}
}
很多时候我们看的URL 地址都是有重定向的,这个好解决,自己递归或者使用 URLConnection提供的方法,解决重定向问题,获取真实的地址
/**
* 直接将重定向交给HttpURLConnection完成,调用它的setInstanceFollowRedirects(true)方法,
* 这样一来重定向对于外部来说是透明的,我们完全感知不到重定向的发生,
* 但是我们没有办法截获重定向的过程,并且对于重定向次数有限制,如果超过4次的重定向,后续的重定向将会被忽略。
* */
public String redirectGetPath(final String str)
throws Malfo