[转载]Android开发之Http通信HttpURLConnection接口 – 一个Coding猪的故事 – 博客频道 – CSDN.NET.
Android开发之Http通信HttpURLConnection接口
/*
* Android开发之Http通信HttpConnection接口
* 北京Android俱乐部群:167839253
* Created on: 2012-5-9
* Author: blueeagle
* Email: liujiaxiang@gmail.com
*/
本文总结了一下《Android应用开发揭秘》里面关于Http通信部分HttpURLConnection接口的相关知识。
HttpURLConnection接口
Http通信协议中,使用的最多的就是Get和Post。Get请求可以获取静态页面,也可以把参数放在字串后面,传递给服务器。Post与Get不同的是Post的参数不是放在URL字串的里面,而是放在http请求数据中。
HttpURLConnection是JAVA的标准类,继承自URLConnection类;
HttpURLConnection和URLConnection类都是抽象类,无法直接实例化对象。
其对象主要是通过URL的openConnection方法获得。
实例定义代码:
//构造一个URL对象 url = new URL(httpUrl); //使用HttpURLConnection打开链接,urlConn就是实例对象 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
openConnection方法只是创建了一个HttpURLConnection或者URLConnection的实例,并不进行真正的链接操作。
每次openConnection的时候都将创建一个新的实例。
因此在连接之前可以对该对象的属性进行设置。
//设置输入(输出)流
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
//设置以POST方式
urlConn.setRequestMethod("POST");
//POST请求不能使用缓存
urlConn.setUseCaches(false);
//在连接完成之后可以关闭这个连接
urlConn.disconnect();
利用Get和Post方式来获取一个网页内容。
HttpURLConnection默认使用Get方式,如果要使用Post方式,则需要setRequestMethod设置。然后将我们要传递的参数内容通过weiteBytes方法写入数据流。
Get方式访问无参数的代码:
public class HttpURLConnectionActivity02 extends Activity {
/** Called when the activity is first created. */
private final String DEBUG_TAG = "HttpURLConnectionActivityActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView mTextView = (TextView)this.findViewById(R.id.myTextView);
//http地址
String httpUrl = "http://10.1.69.34/http1.jsp";
//获得的数据
String resultData = "";
URL url = null;
try
{
//构造一个URL对象
url = new URL(httpUrl);
}
catch (MalformedURLException e)
{
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";
}
if ( !resultData.equals("") )
{
mTextView.setText(resultData);
}
else
{
mTextView.setText("读取的内容为NULL");
}
//关闭InputStreamReader
in.close();
//关闭http连接
urlConn.disconnect();
//设置显示取得的内容
}
catch (IOException e)
{
Log.e(DEBUG_TAG, "IOException");
}
}
else
{
Log.e(DEBUG_TAG, "Url NULL");
}
//设置按键事件监听
Button button_Back = (Button) findViewById(R.id.Button_back);
/* 监听button的事件信息 */
button_Back.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
/* 新建一个Intent对象 */
Intent intent = new Intent();
/* 指定intent要启动的类 */
intent.setClass(HttpURLConnectionActivity02.this, HttpURLConnectionActivity.class);
/* 启动一个新的Activity */
startActivity(intent);
/* 关闭当前的Activity */
HttpURLConnectionActivity02.this.finish();
}
});
}
}
总结:
针对HTTP协议,简单来说:
GET方式是通过把参数键值对附加在url后面来传递的,是文本方式的。
在服务器端可以从’QUERY_STRING’这个变量中直接读取,效率较高,但缺乏安全性,也无法来处理复杂的数据,长度有限制。主要用于传递简单的参数。
POST方式:就传输方式讲参数会被打包在http报头中传输,可以是二进制的。
从CONTENT_LENGTH这个环境变量中读取,便于传送较大一些的数据,同时因为不暴露数据在浏览器的地址栏中,安全性相对较高,但
Mikel