No1:
例如: http://www.*****.com/china/index.htm
1)http:// 代表超文本传送协议, 通知 *****.com 服务器显示 web 页, 通常不用输入
2)www 代表一个 web(万维网)服务器
3)*****.com / 这是装有网页的服务器的域名, 或站点服务器的名称
4)china / 为该服务器上的子目录, 就好像文件夹
5)index.htmindex.htm 是文件夹中的一个 html 文件(网页)
No2:
HTTP 协议 (超文本传送协议) 是用于从 WWW 服务器传输超文本到本地浏览器的传送协议
No3:
android 系统提供了以下 3 种通信接口
1)标准 Java 接口: java.net
2)Apache 接口: org.apache.http
3)android 网络接口: android.net.http
在 android 系统中包括 apache httpclient 库, 此库为执行 android 中的网络操作之首选方法
此外, android 还允许通过标准的 java 联网 api(java.net 包)来访问网络即使 java.net 包, 也是在内部使用该 apache 库
No4:
android 系统中, 存在以下与网络连接相关的包:
1)java.net
提供联网相关的类, 包括流和数据报套接字互联网协议以及通用的 HTTP 处理此为多用途的联网资源经验丰富的 java 开发人员可立即使用此惯用的包来创建应用程序
2)java.io
尽管未明确联网, 但其仍然非常重要此包中的各种类通过其他 java 包中提供的套接字和链接来使用它们也可用来与本地文件进行交互(与网络进行交互时经常发生)
3)java.nio
包含表示具体数据类型的缓冲的各种类便于基于 java 语言的两个端点之间的网络通信
4)org.apache.*
表示可为进行 HTTP 通信提供精细控制和功能的各种包可以将 apache 识别为普通的开源 web 服务器
5)android.net
包括核心 java.net.* 类之外的各种附加的网络接入套接字此包包括 URL 类, 其通常在传统联网之外的 android 应用程序开发中使用
6)android.net.http
包含可操作 SSL 证书的各种类
7)android.net.wifi
包含可管理 android 平台中 wi-fi(802.11 无线以太网)所有方面的各种类并非所有的设备均配备有 wi-fi 能力, 尤其随着 android 在对制造商 (如诺基亚和 LG) 手机的翻盖手机研发方面取得了进展
8)android.telephony.gsm
包含管理和发送短信 (文本) 消息所要求的各种类随着时间的推移, 可能将引入一种附加的包, 以提供有关 GSM 网络 (如 CDMA 或类似 android.telephony.cdma) 的类似功能
No5:
Apache 联网流程:
在 android 系统中, 可以采用 HttpPost 和 HttpGet 来封装 Post 请求和 HttpGet 来封装 Post 请求和 Get 请求, 再使用 HttpClient 的 execute 方法发送 Post 或者 Get 请求并返回服务器的响应数据
1)设置连接和读取超时时间, 并新建 HttpClient 对象
- // 设置连接超时时间和数据读取超时时间
- HttpParams httpParams = new BasicHttpParams();
- HttpConnectionParams.setConnectionTimeout(httpParams, KeySource.CONNECTION_TIMEOUT_INT);
- HttpConnectionParams.setSoTimeout(httpParams, KeySource.SO_TIMEOUT_INT);
- // 新建 HttpClient 对象
- HttpClient httpClient = new DefaultHttpClient(httpParams);
2)实现 Get 请求
- // 获取请求
- HttpGet get = new HttpGet(url);
- //set Http head parameters
- //Map<String,String> headers
- if (headers != null) {
- Set < String > setHead = headers.keySet();
- Iterator < String > iteratorHead = setHead.iterator();
- while (iteratorHead.hasNext()) {
- String headerName = iteratorHead.next();
- String headValue = (String) headers.get(headerName);
- MyLog.d(headerName, headValue);
- get.setHeader(headerName, headValue);
- }
- }
- //connect
- //need try catch
- response = httpClient.execute(get);
3)实现 Post 发送请求处理
- HttpPost post = new HttpPost(KeySource.HOST_URL_STR);
- //set HTTP head parameters
- Map < String,
- String > headers = heads;
- Set < String > setHead = headers.keySet();
- Iterator < String > iteratorHead = setHead.iterator();
- while (iteratorHead.hasNext()) {
- String headName = iteratorHead.next();
- String headValue = (String) headers.get(headName);
- post.setHeader(headName, headValue);
- }
- /**
- * 通常的 HTTP 实体需要在执行上下文的时候动态生成的
- * HttpClient 的提供使用 EntityTemplate 实体类和 ContentProducer 接口支持动态实体
- * 内容制作是通过写需求的内容到一个输出流, 每次请求的时候都会产生
- * 因此, 通过 EntityTemplate 创建实体通常是独立的, 重复性好
- */
- ContentProducer cp = new ContentProducer() {
- public void writeTo(OutputStream outstream) throws IOException {
- Writer writer = new OutputStreamWriter(outstream, "UTF-8");
- writer.write(requestBody);
- writer.flush();
- writer.close();
- };
- HttpEntity entity = new EntityTemplate(cp);
- post.setEntity(entity);
- }
- //connect,need try catch
- response = httpClient.execute(post);
4)通过 Response 响应请求
- if (response.getStatusLine().getStatusCode() == 200) {
- /**
- * 因为直接调用 toString()可能会导致某些中文字符出现乱码的情况, 所以此处使用 toByteArray.
- * 如果需要转成 String 对象, 可以先调用 EntityUtils.toByteArray()方法将消息实体转成 byte 数组, 再由 new String(byte[] bArray)转换成字符串
- */
- byte[] bResultXml = EntityUtils.toByteArray(response.getEntity());
- if (bResultXml != null) {
- String strXml = new String(bResultXml, "utf-8");
- }
- }
无论多么复杂的项目, 都需要遵循上面的流程
No6:
Apache 的核心功能是 HttpClient,android 的 SDK 提供了 apache 的 httpClient 来方便我们使用各种 HTTP 服务可以把 HttpClient 想象成一个浏览器, 通过它的 API 我们可以很方便的发出 Get 请求和 Post 请求
- try {
- // 创建一个默认的 HttpClient
- HttpClient httpClient = new DefaultHttpClient();
- // 创建一个 Get 请求
- HttpGet request = new HttpGet("www.google.com");
- // 发送 Get 请求, 并将响应内容转换成字符串
- String response = httpClient.execute(request, new BasicResponseHandler());
- Log.v("response text", response);
- } catch(ClientProtocolException e) {
- e.printStackTrace();
- } catch(IOException e) {
- e.printStackTrace();
- }
- No7:
多线程 HttpClient
- public class CustomerHttpClient {
- private static final String CHARSET = HTTP.UTF_8;
- private static HttpClient customerHttpClient;
- private CustomerHttpClient() {}
- public static synchronized HttpClient getHttpClient() {
- if (null == customerHttpClient) {
- HttpParams params = new BasicHttpParams();
- // 设置一些基本参数
- HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
- HttpProtocolParams.setContentCharset(params, CHARSET);
- HttpprotocolParams.setUseExpectContinue(params, true);
- HttpProtocolParams.setUserAgent(params, "Mozilla/5.0(Linux;U;Android 2.2.1;en-us;Nexus One Build.FRG83)" + "AppleWebKit/553.1(KHTML,like Cecko) Version/4.0 Mobile Safari/533.1");
- // 超时设置
- /* 从连接池中取连接的超时时间 */
- ConManagerParams.setTimeout(params, 1000);
- /* 连接超时 */
- HttpConnectionParams.setConnectionTimeout(params, 2000);
- /* 请求超时 */
- HttpConnectionParams.setSoTimeout(params, 4000);
- // 设置 HttpClient 支持 HTTP 和 HTTPS 两种模式
- SchemeRegistery schReg = new SchemeRegistry();
- schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
- schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
- // 使用线程安全的链接管理来创建 HttpClient
- ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
- customerHttpClient = new DefaultHttpClient(conMgr, params);
- }
- return customerHttpClient;
- }
- }
- No8:
把 HttpClient 封装在 Application
- public class MyApplication extends Application{
- private HttpClient httpClient;
- @Override
- public void onCreate(){
- super.onCreate();
- httpClient = this.createHttpClient();
- }
- @Override
- public void onLowMemory(){
- super.onLowMemory();
- this.shutdownHttpClient();
- }
- @Override
- public void onTerminate(){
- super.onTerminate();
- this.shutdownHttpClient();
- }
- // 创建 HttpClient 实例
- private HttpClient createHttpClient(){
- HttpPrarms params = new BasicHttpParams();
- HttpProtocolParams.setVersion(params,HttpVersion.HTTP_1_1);
- HttpProtocolParams.setContentCharset(params,HTTP.DEFAULT_CONTENT_CHARSET);
- HttpProtocolParams.setUseExceptionContinue(params,true);
- SchemeRegistry schReg = new SchemeRegistry();
- schReg.register(new Scheme("http",PlainSocketFactory.getSocketFactory(),80));
- schReg.register(new Scheme("https",SSLSocketFactory.getSocketFactory,443));
- ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params,schReg);
- return new DefaultHttpClient(connMgr,params);
- }
- // 关闭连接管理器并释放资源
- private void shutdownHttpClient(){
- if(httpClient!=null && httpClient.getConnectionManager()!=null){
- httpClient.getConnectionManager.shutdown();
- }
- }
- // 对外提供 HttpClient 实例
- public HttpClient getHttpClient(){
- return httpClient;
- }
- }
- No9:
IP 地址是给每个连接在 Internet 上的主机分配的一个 32bit 地址 java.net 中处理 IP 地址的类是 InetAddress
- String GetHostAddress(String strHostName) {
- InetAddress address = null;
- try {
- address = InetAddress.getByName(strHostName);
- } catch(UnknownHostException e) {
- System.out.println(e.getMessage());
- }
- return InetAddress.getHostAddress();
- }
- void GetAllIp(String strHostName) {
- InetAddress[] add = null;
- try {
- add = InetAddress.getAllByName(strHostName);
- for (int i = 0; i < addr.lenth; i++) {
- System.out.println(addr[i]);
- }
- } catch(UnknownHostException) {
- System.out.println(e.getMessage());
- }
- }
网络异常是比较常见的现象, 所以必须要捕获
No10:
URL 类
- Void EasyURL(String strURL) {
- URL url = new URL(strURL);
- try {
- InputStream html = url.openStream();
- int c;
- do {
- c = html.read();
- cf(c != -1) System.out.println((char) c);
- } while ( c != - 1 )
- } catch(IOException e) {
- System.out.println(e.getMessage());
- }
- }
- No11:
套接字 Socket:
基本思想: 客户端建立一个到服务器的链接, 一旦链接建立了, 客户端就可以往套接字里写入数据, 并向服务器发送数据; 反过来, 服务器读取客户端写入套接字里的数据
- void WebPing(String strUrl) {
- try {
- InetAddress addr;
- Socket sock = new Socket(strUrl, 80);
- Addr = sock.getInetAddress();
- System.out.println("Connected to" + addr);
- Sock.close();
- } catch(IOException e) {
- System.out.println(e.getMessage());
- }
- }
- No12: URLConnection:
使用场景例如: HTTP 数据头的传递
- void SendRequest(String strURL) {
- URL url = URL(strURL);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setDoInput(true);
- conn.setDoOutput(true);
- conn.setRequestProperty("Content-type", "application/xxx");
- conn.coonnect();
- System.out.println(Conn.getResponseMessage());
- InputMessage is = Conn.getInputStream();
- int c;
- do {
- c = is.read();
- if (c != -1) System.out.println((char) c);
- } while ( c != - 1 )
- }
- No13:
android 网络接口 android.net.http 来处理 HTTP 请求, android.net.http 是 android.net 中的一个包, 在里面主要包含处理 SSL 证书的类
在 android.net.http 中存在如下 4 个类:
- )AndroidHttpClient
- )SslCertificate
- )SslCertificate.DName
- )SslError
AndroidHttpClient 就是用来处理 HTTP 请求的
android.net.* 实际上是通过对 Apache 的 HttpClient 的封装来实现的一个 HTTP 编程接口, 同时还提供了 HTTP 请求队列管理, 以及 HTTP 连接池管理, 以提高并发请求情况下 (如转载网页时) 的处理效率, 除此之外还有网络状态监听等接口
来源: http://www.bubuko.com/infodetail-2489524.html