HttpUrlConnection
2015-12-13 21:09
633 查看
本文的笔记来自对慕课网,Android中的Http通信:http://www.imooc.com/video/7213。在此记录学习,做为自己今后翻录查阅使用。
案例:HttpUrlConnection 加载百度页面
主要布局文件 activity_http_network_main.xml:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HttpNewworkMainActivity" >
<WebView
android:id="@+id/
4000
webview1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
HttpUrlConnection.java
HttpNetworkMainActivity.java
权限
整个工程目录下载地址:
链接:http://pan.baidu.com/s/1o6ReYy2 密码:djhr
案例:HttpUrlConnection 加载百度页面
主要布局文件 activity_http_network_main.xml:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HttpNewworkMainActivity" >
<WebView
android:id="@+id/
4000
webview1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
HttpUrlConnection.java
package com.mzm.testhttpnetwork; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.os.Handler; import android.webkit.WebView; public class HttpThread extends Thread { private String url; private WebView webview; private Handler handler; public HttpThread(String url, WebView webview, Handler handler) { super(); this.url = url; this.webview = webview; this.handler = handler; } @Override public void run() { try { URL httpUrl = new URL(url); try { HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); conn.setReadTimeout(5000); conn.setRequestMethod("GET"); InputStreamReader stream =new InputStreamReader(conn.getInputStream()); BufferedReader reader = new BufferedReader(stream); final StringBuffer sb = new StringBuffer(); String str; while((str = reader.readLine())!= null){ sb.append(str); } handler.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub webview.loadData(sb.toString(), "text/html;charset=utf-8", null); } }); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } super.run(); } }
HttpNetworkMainActivity.java
package com.mzm.testhttpnetwork; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.Menu; import android.webkit.WebView; public class HttpNetworkMainActivity extends Activity { private WebView webview; private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_http_network_main); webview = (WebView) findViewById(R.id.webview1); new HttpThread("http://www.baidu.com", webview, 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.http_newwork_main, menu); return true; } }
权限
<uses-permission android:name="android.permission.INTERNET"/>
整个工程目录下载地址:
链接:http://pan.baidu.com/s/1o6ReYy2 密码:djhr
相关文章推荐
- 利用Socket建立网络连接的步骤
- NFS网络文件系统的应用
- 什么是TCP连接的三次握手
- HttpServletRequest和HttpServletResponse
- HttpServlet cannot be resolved to a type
- TCP/IP详解卷1 读书笔记:第二十七章 FTP协议
- TCP/IP网络协议
- Android--Apache HttpClient
- Android--HTTP协议
- Firefox 的页面缩放选项在这里 - Http Tool 插件查看自动格式化后的 json
- TCP及socket通信原理
- 参加南京未来网络SDN大会的整理
- nodejs 5.2.0文档自翻译——HTTP模块
- Apache 的 httpd.conf 详解
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许
- TCP/IP详解卷1 读书笔记:第二十六章 Telnet协议
- TCP连接的状态详解以及故障排查
- vsphere创建并管理 ESXI网络
- HTTP协议
- Redhat7 配置http