Android中供给的HttpURLConnection和HttpClient接口能够用来开发HTTP程序。以下是自己在学习中的总结与概括。
1. HttpURLConnection接口
首要需求清晰的是,Http通信中的POST和GET恳求办法的不同。GET能够获得静态页面,也能够把参数放在URL字符串后边,传递给服务器。而POST办法的参数是放在Http恳求中。因而,在编程之前,应当首要清晰运用的恳求办法,然后再依据所运用的办法挑选相应的编程办法。
HttpURLConnection是承继于URLConnection类,二者都是抽象类。其目标首要经过URL的openConnection办法获得。创立办法如下代码所示:
URL url = new URL(http://www.51cto.com/index.jsp?par=123456);
HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();
过以下办法能够对恳求的特点进行一些设置,如下所示:
//设置输入和输出流
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
//设置恳求办法为POST
urlConn.setRequestMethod(POST);
//POST恳求不能运用缓存
urlConn.setUseCaches(false);
//封闭衔接
urlConn.disConnection();
HttpURLConnection默许运用GET办法,例如下面代码所示:
//运用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();
//封闭http衔接
urlConn.disconnect();
假如需求运用POST办法,则需求setRequestMethod设置。代码如下:
String httpUrl = http://192.168.1.110:8080/httpget.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();
//由于这个是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流
DataOutputStream out = new DataOutputStream(urlConn.getOutputStream());
//要上传的参数
String content = par= + URLEncoder.encode(ABCDEFG, gb2312);
//即将上传的内容写入流中
out.writeBytes(content);
//改写、封闭
out.flush();
out.close();
2. HttpClient接口
运用Apache供给的HttpClient接口相同能够进行HTTP操作。
关于GET和POST恳求办法的操作有所不同。GET办法的操作代码示例如下:
// http地址
String httpUrl = http://192.168.1.110:8080/httpget.jsp?par=HttpClient_android_Get;
//HttpGet衔接目标
HttpGet httpRequest = new HttpGet(httpUrl);
//获得HttpClient目标
HttpClient httpclient = new DefaultHttpClient();
//恳求HttpClient,获得HttpResponse
HttpResponse httpResponse = httpclient.execute(httpRequest);
//恳求成功
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
//获得回来的字符串
String strResult = EntityUtils.toString(httpResponse.getEntity());
mTextView.setText(strResult);
}
else
{
mTextView.setText(恳求过错!);
}
}
运用POST办法进行参数传递时,需求运用NameValuePair来保存要传递的参数。,别的,还需求设置所运用的字符集。代码如下所示:
// http地址
String httpUrl = http://192.168.1.110:8080/httpget.jsp;
//HttpPost衔接目标
HttpPost httpRequest = new HttpPost(httpUrl);
//运用NameValuePair来保存要传递的Post参数
List
//增加要传递的参数
params.add(new BasicNameValuePair(par, HttpClient_android_Post));
//设置字符集
HttpEntity httpentity = new UrlEncodedFormEntity(params, gb2312);