当前位置:文档之家› 新版智能终端软件开发

新版智能终端软件开发

协 同
建立HttpClient,选择post或get请求服务器而获得网页 数据。



HttpClient client = new DefaultHttpClient();

HttpGet get = new HttpGet(url);

HttpResponse response = client.execute(get);
2010.06.
浙江大学计算机学院
4


大 学
从Internet获取数据


件 协 同
利用HttpURLConnection对象,我们可以从网络中获取网 页数据。




室 URL url = new URL("");
HttpURLConnection conn = (HttpURLConnection)
HttpEntity entity = response.getEntity();
//尝试读取entity的长度,返回-1表示长度未知
long length = entity.getContentLength();
InputStream is = entity.getContent();
/fatlab
String result = readData(is, "GBK");
conn.disconnect();
System.out.println(result);
/fatlab
2010.06.
浙江大学计算机学院
5


大 学
从Internet获取数据
2010.06.
浙江大学计算机学院
2


大 学
网络访问


件 协
使用网络,应该有相应使用允许。

文件AndroidManifest添加:




室 <uses-permission android:name="android.permission.INTERNET">
</uses-permission>
7



学 广播接收者BroadcastReceiver


件 协 同
第一步:继承BroadcastReceiver,并重写onReceive() 方法。
设 计
第二步:订阅感兴趣的广播Intent:

方法一:使用代码
验 室
方法二:在AndroidManifest.xml文件中的<application>节点 里进行订阅。


件 协 同
• 利用HttpURLConnection对象,我们可以从网络中获取文 件数据。




室 URL url = new
URL("/20100125/Img269812337.jpg");
HttpURLConnection conn = (HttpURLConnection)
/fatlab
2010.06.
浙江大学计算机学院
8
url.openConnection();
conn.setConnectTimeout(6* 1000);
if (conn.getResponseCode() != 200) throw new
RuntimeException("请求url失败");
InputStream is = conn.getInputStream();








同 设
智能终端软件开发




浙江大学计算机学院/软件学院
2010.06.
/fatlab
2010.06.
浙江大学计算机学院
1








同 设
6、网络连接




智能终端开发
/fatlab
readAsFile(is, "ImgAndroid.jpg");
/fatlab
2010.06.
浙江大学计算机学院
6


大 学
广播sendBroadcast()


件 协
Intent之间传递数据
同 广播Intent通过调用发送信息
设 计
Context.sendBroadcast()

Context.sendOrderedBroadcast()
验 室
Context.sendStickyBroadcast()
通常一个广播Intent可以被订阅了此Intent的多个广播接
收者所接收。
/fatlab
2010.06.
浙江大学计算机学院
url.openConnection(); conn.setConnectTimeout(6* 1000);//设置连接超时
if (conn.getResponseCode() != 200) throw new RuntimeException("请求url失败"); InputStream is = conn.getInputStream();//得到网络返回的输入流
<uses-permission
android:name="android.permission.CHANGE_NETWORK_STATE">
</uses-permission>
/fatlab
2010.06.
浙江大学计算机学院
3


大 学
以Get请求从Internet获取数据
相关主题