android中的网络通信

时间:2022-04-23 00:14:14

    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;

public class Activity01 extends Activity {
private final String DEBUG_TAG = " Activity02 " ;
private TextView mTextView;
private Button mButton;
private Button mButton1;
/** Called when the activity is first created. */
@Override
public void 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
public void onClick(View v) {
// TODO Auto-generated method stub
refresh();
}
});
mButton1.setOnClickListener(
new OnClickListener() {

@Override
public void 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();
}

private void 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
public void 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
public void 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;

public class Activity02 extends Activity{
private final String DEBUG_TAG = " Activity02 " ;
@Override
protected void 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
public void 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;

public class Activity03 extends Activity {
private final String DEBUG_TAG = " Activity03 " ;
@Override
protected void 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
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Activity03.
this , Activity01. class );
startActivity(intent);
Activity03.
this .finish();
}
});
}
}

节选自:eoe特刊!