【原文:http://blog.csdn.net/corder_raine/article/details/8317306】
版权为作者所有,如有转载请注明出处
目录
第十天.Android网络与通信... 100
10.1 Android网络通讯介绍... 100
10.1.1 网络通讯技术... 100
10.2 Java.net 101
10.2.2主Activity. 101
10.2.3 直接获取数据... 102
10.2.4 以Get方式上传参数... 103
10.2.5 以Post方式上传参数... 103
10.3 ApacheHttpClient 105
10.3.1 使用HttpClient:主Activity. 105
10.3.2 HttpClient:HttpGet 106
10.3.3 HttpClient:HttpPost 107
10.4 装载并显示Web网页... 108
10.4.1 用线程刷新网页显示... 108
10.4.2 装载网页并显示... 109
10.5 Socket编程复习... 110
第十天.Android网络与通信
10.1 Android网络通讯介绍
10.1.1 网络通讯技术
Ø Java.net
Ø Apache HttpClient
Ø Socket技术
Ø 装载网页
Ø WiFi技术
Ø Bluetooth蓝牙
10.2 Java.net
10.2.2主Activity
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
|
public class
publicvoid super .onCreate(savedInstanceState); setContentView(R.layout.main) Buttonbutton_http /*监听button的事件信息 button_http.setOnClickListener(newButton.OnClickListener() publicvoid /*新建一个Intent对象 Intentintent /*指定intent要启动的类 intent.setClass(Activity01.this,Activity02.class); /*启动一个新的Activity*/ startActivity(intent); /*关闭当前的Activity*/ Activity01. this .finish(); } }); |
** Activity02是直接获取数据的demo!
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
Button /*监听button的事件信息 button_Get.setOnClickListener(newButton.OnClickListener() publicvoid { /*新建一个Intent对象 Intentintent /*指定intent要启动的类 intent.setClass(Activity01.this,Activity03.class); /*启动一个新的Activity*/ startActivity(intent); /*关闭当前的Activity*/ Activity01. this .finish(); } }); |
** Activity03是以Get方式上传参数的demo!
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
Button /*监听button的事件信息 button_Post.setOnClickListener(newButton.OnClickListener() publicvoid { /*新建一个Intent对象 Intentintent /*指定intent要启动的类 intent.setClass(Activity01.this,Activity04.class); /*启动一个新的Activity*/ startActivity(intent); /*关闭当前的Activity*/ Activity01. this .finish(); } }); } } //结束Activity1类 |
** Activity04是以Post方式上传参数的demo!
10.2.3 直接获取数据
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
//http地址 //获得的数据 String "" ; null ; //构造一个URL对象 url new URL(httpUrl); //使用HttpURLConnection打开连接 HttpURLConnection //得到读取的内容(流) InputStreamReader // BufferedReader String null ; //使用循环来读取获得的数据 while (((inputLine null )){ resultData+= "" ; } |
** 研究HttpURLConnectionDemo工程(Activity02.java)
** serverip要换成真实IP,不能用localhost或127.0.0.1
10.2.4 以Get方式上传参数
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
URLurl new URL(httpUrl); //使用HttpURLConnection打开连接 HttpURLConnectionurlConn //得到读取的内容(流) InputStreamReaderin new InputStreamReader(urlConn.getInputStream()); //为输出创建BufferedReader BufferedReaderbuffer new BufferedReader(in); StringinputLine null ; //使用循环来读取获得的数据 while (((inputLine null )){ "" ; //关闭InputStreamReader in.close(); //关闭http连接 urlConn.disconnect(); |
** 研究HttpURLConnectionDemo工程(Activity03.java)
** serverip要换成真实IP,不能用localhost或127.0.0.1
10.2.5 以Post方式上传参数
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
|
URL new URL(httpUrl); HttpURLConnection //因为这个是post请求,设立需要设置为true urlConn.setDoOutput( true );urlConn.setDoInput( true ); urlConn.setRequestMethod( "POST" ); // urlConn.setUseCaches( false ); // urlConn.setInstanceFollowRedirects( true ); urlConn.setRequestProperty( "Content-Type" , "application/x-www-form-urlencoded" ); urlConn.connect(); DataOutputStream String "par=" +URLEncoder.encode( "ABCDEFG" , "gb2312" ); //要上传的参数 out.writeBytes(content); //将要上传的内容写入流中 out.flush(); //获取数据 BufferedReader new InputStreamReader(urlConn.getInputStream())); String null ; while (((inputLine null )){resultData "" ;} reader.close();urlConn.disconnect(); |
** 研究HttpURLConnectionDemo工程(Activity03.java)
** serverip要换成真实IP,不能用localhost或127.0.0.1
main.xml
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
< LinearLayout android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > < TextView android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "通过下面的按钮进行不同方式的连接" /> < Button android:id = "@+id/Button_HTTP" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "直接获取数据" /> < Button android:id = "@+id/Button_Get" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "以GET方式传递数据" /> < Button android:id = "@+id/Button_Post" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "以POST方式传递数据" /> </ LinearLayout > |
AndroidManifest.xml
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
|
package = "com.lxt008" android:versionCode = "1" android:versionName = "1.0" > < application android:icon = "@drawable/icon" android:label = "@string/app_name" > < activity android:name = ".Activity01" android:label = "@string/app_name" > < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> </ intent-filter > </ activity > < activity android:name = "Activity02" ></ activity > < activity android:name = "Activity03" ></ activity > < activity android:name = "Activity04" ></ activity > </ application > < uses-permission android:name = "android.permission.INTERNET" /> < uses-sdk android:minSdkVersion=“7" </ manifest > |
10.3 ApacheHttpClient
10.3.1 使用HttpClient:主Activity
1
2
3
4
5
6
7
8
|
Button button_Get.setOnClickListener(newButton.OnClickListener() publicvoid Intentintent new Intent(); /* /*指定intent要启动的类 intent.setClass(Activity01.this,Activity02.class); startActivity(intent);/* } }); |
1
2
3
4
5
6
7
8
9
|
Button button_Post.setOnClickListener(newButton.OnClickListener() publicvoid Intentintent new Intent(); /*指定intent要启动的类 intent.setClass(Activity01. this ,Activity03. class ); startActivity(intent); this .finish();} }); } |
** 研究ApacheHttpClientDemo工程(Activity01.java)
** serverip要换成真实IP,不能用localhost或127.0.0.1
10.3.2 HttpClient:HttpGet
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
|
//HttpGet连接对象 HttpGet //取得HttpClient对象 HttpClient //请求HttpClient,取得HttpResponse HttpResponse //请求成功 if (httpResponse.getStatusLine().getStatusCode()== //取得返回的字符串 StringstrResult mTextView.setText(strResult); } else { mTextView.setText( "请求错误!" ); [align=left]} |
** 研究ApacheHttpClientDemo工程(Activity02.java)
** serverip要换成真实IP,不能用localhost或127.0.0.1
10.3.3 HttpClient:HttpPost
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
//HttpPost连接对象 HttpPost //使用NameValuePair来保存要传递的Post参数 List //添加要传递的参数 params.add(newBasicNameValuePair( "par" , "HttpClient_android_Post" )); //设置字符集 HttpEntity "gb2312" ); //请求httpRequest httpRequest.setEntity(httpentity); //取得默认的HttpClient HttpClient //取得HttpResponse HttpResponse if (httpResponse.getStatusLine().getStatusCode() StringstrResult |
** 研究ApacheHttpClientDemo工程(Activity03.java)
** serverip要换成真实IP,不能用localhost或127.0.0.1
10.4 装载并显示Web网页
10.4.1 用线程刷新网页显示
01
02
03
04
05
06
07
08
09
10
|
public void
new Thread(mRunnable).start(); //开启线程 //刷新网页显示 privatevoid HttpURLConnectionurlConn InputStreamReaderin new InputStreamReader(urlConn.getInputStream()); BufferedReaderbuffer new BufferedReader(in); StringinputLine null ; //使用循环来读取获得的数据 } |
01
02
03
04
05
06
07
08
09
10
|
private Runnable publicvoid while ( true ) 5 * 1000 ); //发送消息 mHandler.sendMessage(mHandler.obtainMessage()); Handler new Handler() public void
super .handleMessage(msg); refresh(); } |
** 研究HttpURLConnectionRefresh工程
10.4.2 装载网页并显示
1
2
3
4
5
6
7
8
|
WebView String "Hello ; //装载页面,你可以换成URL地址。 webView.loadDataWithBaseURL( "Demo" ,html, "text/html" , "utf-8" , null ); //设置支持JS webView.getSettings().setJavaScriptEnabled( true ); //设置浏览器客户端 webView.setWebChromeClient(newWebChromeClient()); |
10.5 Socket编程复习
Ø 以前课程中学过Socket编程。
Ø 研究SocketDemo以复习巩固Socket在Android中的使用。