首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android 应用Java自带的HttpURLConnection 连接网络 读取返回数据

2012-07-02 
android 使用Java自带的HttpURLConnection连接网络 读取返回数据??? ? @Override? ? protected void onCre

android 使用Java自带的HttpURLConnection 连接网络 读取返回数据

?

?

? ? @Override

? ? protected void onCreate(Bundle savedInstanceState) {

? ? ? ? // TODO Auto-generated method stub

? ? ? ? super.onCreate(savedInstanceState);

? ? ? ? setContentView(R.layout.main);

? ? ? ? TextView textView = (TextView) findViewById(R.id.textView1);

? ? ? ? String httpUri = "http://www.baidu.com";

? ? ? ? String result = "";

? ? ? ? URL url = null;

? ? ? ? try {

? ? ? ? ? ? url = new URL(httpUri);

? ? ? ? } catch (MalformedURLException e) {

? ? ? ? ? ? // TODO Auto-generated catch block

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? if (url != null) {

? ? ? ? ? ? try {

? ? ? ? ? ? ? ? // 使用HttpURLConnection打开连接

? ? ? ? ? ? ? ? HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();

? ? ? ? ? ? ? ? urlConn.setConnectTimeout(10000);// 10s内连不上就断开

? ? ? ? ? ? ? ? // 因为这个是post请求,设立需要设置为true

? ? ? ? ? ? ? ? // urlConn.setDoOutput(true);

? ? ? ? ? ? ? ? // urlConn.setDoInput(true);

? ? ? ? ? ? ? ? // 设置以POST方式

? ? ? ? ? ? ? ? // urlConn.setRequestMethod("POST");

? ? ? ? ? ? ? ? // Post 请求不能使用缓存

? ? ? ? ? ? ? ? // urlConn.setUseCaches(false);

? ? ? ? ? ? ? ? // urlConn.setInstanceFollowRedirects(true);

? ? ? ? ? ? ? ? // 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的

? ? ? ? ? ? ? ? // urlConn.setRequestProperty("Content-Type",

? ? ? ? ? ? ? ? // "application/x-www-form-urlencoded");

? ? ? ? ? ? ? ? // 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,

? ? ? ? ? ? ? ? // 要注意的是connection.getOutputStream会隐含的进行connect。

? ? ? ? ? ? ? ? urlConn.connect();

? ? ? ? ? ? ? ? // DataOutputStream流

? ? ? ? ? ? ? ? OutputStream outputStream = urlConn.getOutputStream();// 向服务器写入

? ? ? ? ? ? ? ? DataOutputStream out = new DataOutputStream(outputStream);

// DataOutputStream?is?objectOutputStream的子类,也可以用objectOutputStream类

? ? ? ? ? ? ? ? // 要上传的参数

? ? ? ? ? ? ? ? // String content = "name=gyf";

? ? ? ? ? ? ? ? // 将要上传的内容写入流中

? ? ? ? ? ? ? ? out.writeBytes(new String("name=gyf"));// 要是用objectOutputStream就是out.writeObject(content);//写入服务器的参数,但是放到内存中了

? ? ? ? ? ? ? ? // content = "&wife=" + URLEncoder.encode("lyx", "gb2312");

? ? ? ? ? ? ? ? // out.writeBytes(content);

? ? ? ? ? ? ? ? // 刷新、关闭

? ? ? ? ? ? ? ? out.flush();// 真正的写过去了

? ? ? ? ? ? ? ? out.close();

? ? ? ? ? ? ? ? // 获服务器取数据

? ? ? ? ? ? ? ? InputStream inputStream = urlConn.getInputStream();

? ? ? ? ? ? ? ? InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

? ? ? ? ? ? ? ? BufferedReader reader = new BufferedReader(inputStreamReader);// 读字符串用的。

? ? ? ? ? ? ? ? String inputLine = null;

? ? ? ? ? ? ? ? // 使用循环来读取获得的数据,把数据都村到result中了

? ? ? ? ? ? ? ? while (((inputLine = reader.readLine()) != null)) {

? ? ? ? ? ? ? ? ? ? // 我们在每一行后面加上一个"\n"来换行

? ? ? ? ? ? ? ? ? ? result += inputLine + "\n";

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? reader.close();// 关闭输入流

? ? ? ? ? ? ? ? // 关闭http连接

? ? ? ? ? ? ? ? urlConn.disconnect();

? ? ? ? ? ? ? ? // 设置显示取得的内容

? ? ? ? ? ? ? ? if (result != null) {

? ? ? ? ? ? ? ? ? ? textView.setText(result);

? ? ? ? ? ? ? ? }

?

? ? ? ? ? ? ? ? else {

? ? ? ? ? ? ? ? ? ? textView.setText("读取的内容为NULL");

? ? ? ? ? ? ? ? }

? ? ? ? ? ? } catch (Exception e) {

? ? ? ? ? ? ? ? // TODO: handle exception

? ? ? ? ? ? }

? ? ? ? }

?

? ? }

热点排行