android 网络请求get,post实现
2017-07-09 21:33
453 查看
HttpURLConnection
//
Get方式请求
public
static void requestByGet()throws Exception {
String path ="https://reg.163.com/logins.jsp?id=helloworld&pwd=android";
// 新建一个URL对象
URL url =
new URL(path);
// 打开一个HttpURLConnection连接
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
// 设置连接超时时间
urlConn.setConnectTimeout(5 * 1000);
// 开始连接
urlConn.connect();
// 判断请求是否成功
if (urlConn.getResponseCode() ==HTTP_200) {
// 获取返回的数据
byte[]data = readStream(urlConn.getInputStream());
Log.i(TAG_GET,"Get方式请求成功,返回数据如下:");
Log.i(TAG_GET,new String(data,"UTF-8"));
} else {
Log.i(TAG_GET,"Get方式请求失败");
}
// 关闭连接
urlConn.disconnect();
}
// Post方式请求
public
static void requestByPost()throws Throwable {
String path ="https://reg.163.com/logins.jsp";
// 请求的参数转换为byte数组
String params =
"id=" + URLEncoder.encode("helloworld",
"UTF-8")
+
"&pwd=" + URLEncoder.encode("android",
"UTF-8");
byte[]postData =
params.getBytes();
// 新建一个URL对象
URL url =
new URL(path);
// 打开一个HttpURLConnection连接
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
// 设置连接超时时间
urlConn.setConnectTimeout(5 * 1000);
// Post请求必须设置允许输出
urlConn.setDoOutput(true);
// Post请求不能使用缓存
urlConn.setUseCaches(false);
// 设置为Post请求
urlConn.setRequestMethod("POST");
urlConn.setInstanceFollowRedirects(true);
// 配置请求Content-Type(注意:这个设置有时返回的是html格式,而不是json格式,遇到则不用设置)
urlConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencode");
// 开始连接
urlConn.connect();
// 发送请求参数
DataOutputStream dos =new DataOutputStream(urlConn.getOutputStream());
dos.write(postData);
dos.flush();
dos.close();
// 判断请求是否成功
if (urlConn.getResponseCode() ==HTTP_200) {
// 获取返回的数据
byte[]data = readStream(urlConn.getInputStream());
Log.i(TAG_POST,"Post请求方式成功,返回数据如下:");
Log.i(TAG_POST,new String(data,"UTF-8"));
} else {
Log.i(TAG_POST,"Post方式请求失败");
}
}
org.apache.http包中的HttpGet和HttpPost类
// HttpGet方式请求
public
static void requestByHttpGet()throws Exception {
String path ="https://reg.163.com/logins.jsp?id=helloworld&pwd=android";
// 新建HttpGet对象
HttpGet httpGet =new HttpGet(path);
// 获取HttpClient对象
HttpClient httpClient =new DefaultHttpClient();
// 获取HttpResponse实例
HttpResponse httpResp =httpClient.execute(httpGet);
// 判断是够请求成功
if (httpResp.getStatusLine().getStatusCode() ==HTTP_200) {
// 获取返回的数据
String result = EntityUtils.toString(httpResp.getEntity(),"UTF-8");
Log.i(TAG_HTTPGET,"HttpGet方式请求成功,返回数据如下:");
Log.i(TAG_HTTPGET,result);
} else {
Log.i(TAG_HTTPGET,"HttpGet方式请求失败");
}
}
// HttpPost方式请求
public
static void requestByHttpPost()throws Exception {
String path ="https://reg.163.com/logins.jsp";
// 新建HttpPost对象
HttpPost httpPost =new HttpPost(path);
// Post参数
List<NameValuePair> params =new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id","helloworld"));
params.add(new BasicNameValuePair("pwd","android"));
// 设置字符集
HttpEntity entity =new UrlEncodedFormEntity(params, HTTP.UTF_8);
// 设置参数实体
httpPost.setEntity(entity);
// 获取HttpClient对象
HttpClient httpClient =new DefaultHttpClient();
// 获取HttpResponse实例
HttpResponse httpResp =httpClient.execute(httpPost);
// 判断是够请求成功
if (httpResp.getStatusLine().getStatusCode() ==HTTP_200) {
// 获取返回的数据
String result = EntityUtils.toString(httpResp.getEntity(),"UTF-8");
Log.i(TAG_HTTPGET,"HttpPost方式请求成功,返回数据如下:");
Log.i(TAG_HTTPGET,result);
} else {
Log.i(TAG_HTTPGET,"HttpPost方式请求失败");
}
}
// 获取连接返回的数据
private
static byte[] readStream(InputStreaminputStream)
throws Exception {
byte[]buffer =
new byte[1024];
intlen = -1;
ByteArrayOutputStream baos =new ByteArrayOutputStream();
while ((len =inputStream.read(buffer)) != -1) {
baos.write(buffer, 0,len);
}
byte[]data =
baos.toByteArray();
inputStream.close();
baos.close();
returndata;
}
//
Get方式请求
public
static void requestByGet()throws Exception {
String path ="https://reg.163.com/logins.jsp?id=helloworld&pwd=android";
// 新建一个URL对象
URL url =
new URL(path);
// 打开一个HttpURLConnection连接
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
// 设置连接超时时间
urlConn.setConnectTimeout(5 * 1000);
// 开始连接
urlConn.connect();
// 判断请求是否成功
if (urlConn.getResponseCode() ==HTTP_200) {
// 获取返回的数据
byte[]data = readStream(urlConn.getInputStream());
Log.i(TAG_GET,"Get方式请求成功,返回数据如下:");
Log.i(TAG_GET,new String(data,"UTF-8"));
} else {
Log.i(TAG_GET,"Get方式请求失败");
}
// 关闭连接
urlConn.disconnect();
}
// Post方式请求
public
static void requestByPost()throws Throwable {
String path ="https://reg.163.com/logins.jsp";
// 请求的参数转换为byte数组
String params =
"id=" + URLEncoder.encode("helloworld",
"UTF-8")
+
"&pwd=" + URLEncoder.encode("android",
"UTF-8");
byte[]postData =
params.getBytes();
// 新建一个URL对象
URL url =
new URL(path);
// 打开一个HttpURLConnection连接
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
// 设置连接超时时间
urlConn.setConnectTimeout(5 * 1000);
// Post请求必须设置允许输出
urlConn.setDoOutput(true);
// Post请求不能使用缓存
urlConn.setUseCaches(false);
// 设置为Post请求
urlConn.setRequestMethod("POST");
urlConn.setInstanceFollowRedirects(true);
// 配置请求Content-Type(注意:这个设置有时返回的是html格式,而不是json格式,遇到则不用设置)
urlConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencode");
// 开始连接
urlConn.connect();
// 发送请求参数
DataOutputStream dos =new DataOutputStream(urlConn.getOutputStream());
dos.write(postData);
dos.flush();
dos.close();
// 判断请求是否成功
if (urlConn.getResponseCode() ==HTTP_200) {
// 获取返回的数据
byte[]data = readStream(urlConn.getInputStream());
Log.i(TAG_POST,"Post请求方式成功,返回数据如下:");
Log.i(TAG_POST,new String(data,"UTF-8"));
} else {
Log.i(TAG_POST,"Post方式请求失败");
}
}
org.apache.http包中的HttpGet和HttpPost类
// HttpGet方式请求
public
static void requestByHttpGet()throws Exception {
String path ="https://reg.163.com/logins.jsp?id=helloworld&pwd=android";
// 新建HttpGet对象
HttpGet httpGet =new HttpGet(path);
// 获取HttpClient对象
HttpClient httpClient =new DefaultHttpClient();
// 获取HttpResponse实例
HttpResponse httpResp =httpClient.execute(httpGet);
// 判断是够请求成功
if (httpResp.getStatusLine().getStatusCode() ==HTTP_200) {
// 获取返回的数据
String result = EntityUtils.toString(httpResp.getEntity(),"UTF-8");
Log.i(TAG_HTTPGET,"HttpGet方式请求成功,返回数据如下:");
Log.i(TAG_HTTPGET,result);
} else {
Log.i(TAG_HTTPGET,"HttpGet方式请求失败");
}
}
// HttpPost方式请求
public
static void requestByHttpPost()throws Exception {
String path ="https://reg.163.com/logins.jsp";
// 新建HttpPost对象
HttpPost httpPost =new HttpPost(path);
// Post参数
List<NameValuePair> params =new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id","helloworld"));
params.add(new BasicNameValuePair("pwd","android"));
// 设置字符集
HttpEntity entity =new UrlEncodedFormEntity(params, HTTP.UTF_8);
// 设置参数实体
httpPost.setEntity(entity);
// 获取HttpClient对象
HttpClient httpClient =new DefaultHttpClient();
// 获取HttpResponse实例
HttpResponse httpResp =httpClient.execute(httpPost);
// 判断是够请求成功
if (httpResp.getStatusLine().getStatusCode() ==HTTP_200) {
// 获取返回的数据
String result = EntityUtils.toString(httpResp.getEntity(),"UTF-8");
Log.i(TAG_HTTPGET,"HttpPost方式请求成功,返回数据如下:");
Log.i(TAG_HTTPGET,result);
} else {
Log.i(TAG_HTTPGET,"HttpPost方式请求失败");
}
}
// 获取连接返回的数据
private
static byte[] readStream(InputStreaminputStream)
throws Exception {
byte[]buffer =
new byte[1024];
intlen = -1;
ByteArrayOutputStream baos =new ByteArrayOutputStream();
while ((len =inputStream.read(buffer)) != -1) {
baos.write(buffer, 0,len);
}
byte[]data =
baos.toByteArray();
inputStream.close();
baos.close();
returndata;
}
相关文章推荐
- Android联网app必用Get请求和Post请求的区别,网络编程代码实现
- Android中使用HttpURLConnection和HttpClient实现GET和POST请求访问网络
- 通过AsyncTask实现网络请求的方式,get和post
- android 之http网络请求之基本的GET,POST请求一
- Android中的http带参数网络请求,GET和POST
- Android实现Get-Post登陆请求
- Android网络框架-Volley(四) 使用get和post方法发送json请求
- Android 实现HttpClient发起GET或者POST请求
- Android开发网络请求之GET和POST
- Android Volley网络请求框架 实现post方法并带Map参数上传
- android(21)(使用HttpURLConnection,HttpClient和AsyncHttpClient分别实现post和get请求数据)
- Android核心基础-7.Android 网络通信-5.发送GET、POST请求
- Android HTTP实例 使用GET方法和POST方法发送请求(通过Apache接口实现)
- Android网络请求库okHttp进行post、get请求
- 通过AsyncTask实现网络请求的方式,get和post
- 12_Android中HttpClient的应用,doGet,doPost,doHttpClientGet,doHttpClient请求,另外借助第三方框架实现网络连接的应用,
- Android——HttpClient 实现POST和Get请求,Connection链接
- Android用Apache HttpClient 实现POST和Get请求
- Android网络编程专栏--get和post请求,上传下载文件,提交Cookie
- Android-封装post和get 网络请求