您的位置:首页 > 理论基础 > 计算机网络

Android中的Http通信(二)之根据Url读取网络数据

2015-11-22 15:19 483 查看
废话不多说,直接开始吧。如果你看到了这一篇文章,但是你对Http协议的基本知识还不够了解,那你就去看上一篇文章:Android中的Http通信(一)之Http协议基本知识。本篇文章主要介绍的是,根据url读取网页html,并且显示到webview上面。文章很简单,内容也很简单,那我为什么还要写呢,目的就是就是为了了解和熟悉URL对象和HttpURLConnection对象的使用。

废话不多说,直接看布局吧:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>
很简单,不做任何介绍。

看看Activity中的代码:

package com.example.http;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;

/**
* 根据一个URL,通过Http协议,读取其中的HTML代码,并显示到webView上
*
* @author xiaoyf
*
*/
public class MainActivity extends Activity {
private WebView mWebView;
private Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webView);
String url = "/article/9276825.html";
new HttpThread(url, mWebView, handler).start();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}


也是很easy的,看不懂的话,你还是学学基础吧


下面是URL和HttpURLConnection应用的核心代码,也是本篇的核心。

package com.example.http;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.os.Handler;
import android.webkit.WebView;

/**
* 为读取网络数据所开辟的子线程
*
* @author xiaoyf
*
*/
public class HttpThread extends Thread {
private final static int CONNECT_OUT_TIME = 5000;
private String url;
private WebView webView;
private Handler handler;

public HttpThread() {
super();
}

public HttpThread(String url, WebView webView, Handler handler) {
super();
this.url = url;
this.webView = webView;
this.handler = handler;
}

@Override
public void run() {
// TODO Auto-generated method stub
try {
// 第一步:创建必要的URL对象
URL httpUrl = new URL(url);
// 第二步:根据URL对象,获取HttpURLConnection对象
HttpURLConnection connection = (HttpURLConnection) httpUrl
.openConnection();
// 第三步:为HttpURLConnection对象设置必要的参数(是否允许输入数据、连接超时时间、请求方式)
connection.setDoInput(true);
connection.setConnectTimeout(CONNECT_OUT_TIME);
connection.setRequestMethod("GET");
// 第四步:开始读取数据
final StringBuffer buffer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String temp = null;
while ((temp = reader.readLine()) != null) {
buffer.append(temp);
}
reader.close();

handler.post(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
webView.loadData(buffer.toString(),
"text/html;charset=utf-8", "utf-8");
}
});

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}


流程就是:创建URL对象----->根据URL对象获取HttpURLConnection对象----->为HttpURLConnection对象设置必要的参数----->开始读取数据----->通过handler对象,将html代码显示到webview上

项目下载:http://download.csdn.net/detail/u014544193/9337639
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: