Android4.0.4编程日记(5)--URL网络链接获取数据
2013-01-22 14:31
609 查看
package com.example.test; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.StrictMode; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import com.example.pojo.Params; /** * * @author 王莹 URL HttpURLConnection */ public class MainActivity extends Activity { private ListView mylistview; private TextView textView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.client); try { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build()); getHttp(); } catch (Exception e) { e.printStackTrace(); } } private void getHttp() { try { URL url = new URL("http://www.google.cn/"); // URL url = new URL("http://192.168.0.133:8888/AndroidTest/login.jsp"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setConnectTimeout(10000); conn.setRequestMethod("GET"); conn.setRequestProperty("accept", "*/*"); String location = conn.getRequestProperty("location"); int resCode = conn.getResponseCode(); conn.connect(); InputStream stream = conn.getInputStream(); byte[] data = new byte[102400]; int length = stream.read(data); String str = new String(data, 0, length); conn.disconnect(); textView=(TextView) findViewById(R.id.httpnote); textView.setText(str); stream.close(); } catch (Exception ee) { System.out.print("ee:" + ee.getMessage()); } } /** * 异常解决android.os.NetworkOnMainThreadException * http://www.cnblogs.com/sjrhero/articles/2606833.html */ }
顺带一提,那个AndroidManifest.xml中别忘记
<uses-permission android:name="android.permission.INTERNET"/>
相关文章推荐
- Android4.0.4编程日记(4)--List单击Intent跳转并获取数据
- android 网络编程--URL获取数据/图片
- Android通过HttpURLConnection链接到网络,并获取网络数据
- android 网络编程--URL获取数据/图片
- Android网络编程-----从服务器端获取xml数据并解析
- 第十二篇:Android GET请求获取网络数据以及POST数据(使用HttpURLConnection)
- Android网络编程之获取网络上的Json数据实例
- Python 网络爬虫 009 (编程) 通过正则表达式来获取一个网页中的所有的URL链接,并下载这些URL链接的源代码
- Android通过URL获取网络数据
- Python 网络爬虫 009 (编程) 通过正则表达式来获取一个网页中的所有的URL链接,并下载这些URL链接的源代码
- Android数据解析之JSON,网络编程之URL,异步任务AsyncTask讲解
- Android4.0.4编程日记(3)--ListView单击获取事件
- android HttpURLConnection编程详解,获取网络图片,html代码查
- Android 网络编程之HttpURLConnection(conn.getResponseCode() != 200,从url读取写入数据)
- Android网络编程---从服务器端获取JSON数据
- Android通过URL获取网络数据
- android HttpURLConnection编程详解,获取网络图片,html代码查看
- (转)Android通过URL获取网络数据
- Android HttpURLConnection(Get,Post方式)进行网络通信 获取数据和网络图片
- android:通过url向网络端获取json格式数据并解析显示