代码改变世界

android中的网络通信

2011-03-06 23:20  雪夜&流星  阅读(771)  评论(0编辑  收藏  举报

    1、HttpURLConnection

    

package com.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

publicclass Activity01 extends Activity {
privatefinal String DEBUG_TAG ="Activity02";
private TextView mTextView;
private Button mButton;
private Button mButton1;
/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView
= (TextView) findViewById(R.id.Textview01);
mButton
= (Button) findViewById(R.id.Button01);
mButton1
=(Button) findViewById(R.id.Button02);
mButton.setOnClickListener(
new OnClickListener() {

@Override
publicvoid onClick(View v) {
// TODO Auto-generated method stub
refresh();
}
});
mButton1.setOnClickListener(
new OnClickListener() {

@Override
publicvoid onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(Activity01.
this, Activity02.class);
startActivity(intent);
Activity01.
this.finish();
}
});
// 开启线程
new Thread(mRunnable).start();
}

privatevoid refresh() {
// TODO Auto-generated method stub
String httpURL ="http://wap.baidu.com/";
String resultData
="";
URL url
=null;
try {
url
=new URL(httpURL);
}
catch (MalformedURLException e) {
// TODO Auto-generated catch block
Log.e(DEBUG_TAG, "MalformedURLException");
}
if (url !=null) {
try {
// 使用HttpURLConnection打开连接
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();
// 得到读取的数据流
InputStreamReader in =new InputStreamReader(urlConn
.getInputStream());
// 为输出创建BufferedReader
BufferedReader buffer =new BufferedReader(in);
String inputLine
=null;
// 使用循环来读取获得的数据
while ((inputLine = buffer.readLine()) !=null) {
// 我们在每一行后面加一个换行符"\n"
resultData += inputLine +"\n";
}
// 关闭InputStreamReader
in.close();
// 关闭连接
urlConn.disconnect();
// 设置显示取得的内容
if (resultData !=null) {
mTextView.setText(resultData);
}
else {
mTextView.setText(
"读取的内容为Null");
}

}
catch (IOException e) {
// TODO Auto-generated catch block
Log.e(DEBUG_TAG, "IOException");
}
}
else {
Log.e(DEBUG_TAG,
"url Null");
}
}

private Runnable mRunnable =new Runnable() {

@Override
publicvoid run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(
5000);
// 发送消息
mHandler.sendMessage(mHandler.obtainMessage());
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
Log.e(DEBUG_TAG, e.toString());
}
}
}
};

Handler mHandler
=new Handler() {

@Override
publicvoid handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
// 刷新
refresh();
}

};
}

2、java.net.URL

package com.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

publicclass Activity02 extends Activity{
privatefinal String DEBUG_TAG ="Activity02";
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.http);
TextView mTextView
=(TextView) findViewById(R.id.TextView_HTTP);
//http地址
String httpURL ="http://www.huohu123.com/?src=ffbookmark";
//获得的数据
String resultData ="";
URL url
=null;
try {
url
=new URL(httpURL);
}
catch (MalformedURLException e) {
// TODO Auto-generated catch block
Log.e(DEBUG_TAG, "MalformedURLException");
}
if (url !=null) {
try {
// 使用HttpURLConnection打开连接
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();
// 得到读取的数据流
InputStreamReader in =new InputStreamReader(urlConn
.getInputStream());
// 为输出创建BufferedReader
BufferedReader buffer =new BufferedReader(in);
String inputLine
=null;
// 使用循环来读取获得的数据
while ((inputLine = buffer.readLine()) !=null) {
// 我们在每一行后面加一个换行符"\n"
resultData += inputLine +"\n";
}
// 关闭InputStreamReader
in.close();
// 关闭连接
urlConn.disconnect();
// 设置显示取得的内容
if (resultData !=null) {
mTextView.setText(resultData);
}
else {
mTextView.setText(
"读取的内容为Null");
}

}
catch (IOException e) {
// TODO Auto-generated catch block
Log.e(DEBUG_TAG, "IOException");
}
}
else {
Log.e(DEBUG_TAG,
"url Null");
}
Button mButton
=(Button) findViewById(R.id.Button_Back);
mButton.setOnClickListener(
new OnClickListener() {

@Override
publicvoid onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(Activity02.
this, Activity01.class);
startActivity(intent);
Activity02.
this.finish();
}
});
}

}

 

3、以Get方式

package com.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

publicclass Activity03 extends Activity {
privatefinal String DEBUG_TAG ="Activity03";
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.http);
TextView mTextView
=(TextView) findViewById(R.id.TextView_HTTP);
//http地址
String httpURL ="http://localhost:8080/bbb/list.jsp";
//获得的数据
String resultData ="";
URL url
=null;
try {
url
=new URL(httpURL);
}
catch (MalformedURLException e) {
// TODO Auto-generated catch block
Log.e(DEBUG_TAG, "MalformedURLException");
}
if (url !=null) {
try {
// 使用HttpURLConnection打开连接
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();
// 得到读取的数据流
InputStreamReader in =new InputStreamReader(urlConn
.getInputStream());
// 为输出创建BufferedReader
BufferedReader buffer =new BufferedReader(in);
String inputLine
=null;
// 使用循环来读取获得的数据
while ((inputLine = buffer.readLine()) !=null) {
// 我们在每一行后面加一个换行符"\n"
resultData += inputLine +"\n";
}
// 关闭InputStreamReader
in.close();
// 关闭连接
urlConn.disconnect();
// 设置显示取得的内容
if (resultData !=null) {
mTextView.setText(resultData);
}
else {
mTextView.setText(
"读取的内容为Null");
}

}
catch (IOException e) {
// TODO Auto-generated catch block
Log.e(DEBUG_TAG, "IOException");
}
}
else {
Log.e(DEBUG_TAG,
"url Null");
}
Button mButton
=(Button) findViewById(R.id.Button_Back);
mButton.setOnClickListener(
new OnClickListener() {

@Override
publicvoid onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(Activity03.
this, Activity01.class);
startActivity(intent);
Activity03.
this.finish();
}
});
}
}

节选自:eoe特刊!