【Android】使用http的Post和Get方式获取网络数据
来源:互联网 发布:ubuntu分区方案 编辑:程序博客网 时间:2024/06/11 19:37
PS:在manifest里面添加INTERNET权限
下面简单的说一下Get和Post方式之间的区别
1. 从文字意义上说:Get是从服务器上面获取数据,而Post是从服务器上传递数据
2. http如果没有在setRequestMethod(“POST”)的话,默认是Get方式的,而setRequestMethod()里面的参数只要有以下几种
/**
* The subset of HTTP methods that the user may select via {@link
* #setRequestMethod(String)}.
*/
private static final String[] PERMITTED_USER_METHODS = {
“OPTIONS”,
“GET”,
“HEAD”,
“POST”,
“PUT”,
“DELETE”,
“TRACE”
// Note: we don’t allow users to specify “CONNECT”
};
3. Get方式获取数据是将参数拼接到Url地址上面,而Post方式则是写进Connection里面,相对Get方式比较安全
4. Get方式传递数据量有限,最多2Kb,而Post理论上无限制,其实也就100K左右
5. 相对执行效率,Get方式较快
总结:如果是做数据查询,用Get方式,如果是机密方式或者数据增删改就用Post
以下是两个使用http的Post和Get方式获取网络数据的Demo
1. 使用httpGet读取网络数据
由于android4.0之后,网络耗时操作需要在后台操作!所以需要用到AsyncTask异步操作!
String url =”http://www.baidu.com”;
new AsyncTask<String, Void, Void>() {
@Override
protected Void doInBackground(String… params) {
try {
//通过URl获取到访问地址
URL url = new URL(params[0]);
//获取到链接的Connection资源
URLConnection connection = url.openConnection();
//通过connection获取数据流,并将获得的字节流数据转成字符流
InputStream ins = connection.getInputStream();
InputStreamReader insReader = new InputStreamReader(ins);
//将获得的字符流数据转化成更容易处理的字符输入流
BufferedReader br = new BufferedReader(insReader);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
//最后关闭流处理
br.close();
insReader.close();
ins.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}.execute(url);
2. 使用httpPost读取网络数据
//HttpPost方式
public void onBtnPost(View view) {
new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String… params) {
StringBuffer sb = new StringBuffer();
try {
//通过URl获取到访问地址
URL url = new URL(params[0]);
//获取到链接的Connection资源
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod(“POST”);
OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream(), “utf-8”);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(“keyfrom=testMarco&key=1197299496&type=data&doctype=json&version=1.1&q=good”);
bw.flush();
//通过connection获取数据流,并将获得的字节流数据转成字符流
InputStream ins = connection.getInputStream();
InputStreamReader insReader = new InputStreamReader(ins);
//将获得的字符流数据转化成更容易处理的字符输入流
BufferedReader br = new BufferedReader(insReader);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
sb.append(line);
}
//最后关闭流处理
br.close();
insReader.close();
ins.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (TextUtils.isEmpty(sb.toString().trim())) {
return “result null”;
} else {
return sb.toString();
}
}
@Override
protected void onPostExecute(String s) {
tvContent.setText(“post” + s);
}
}.execute(“http://fanyi.youdao.com/openapi.do”);
}
0 0
- 【Android】使用http的Post和Get方式获取网络数据
- Android使用HTTP GET、HTTP POST获取网络数据
- android 使用Http的POST方式读取网络数据
- java使用http get以及post方式读取网络数据
- android使用Http的Get方式读取网络数据
- Android程序:使用Http的Get方式读取网络数据
- android 使用Http的Get方式读取网络数据
- Android HttpURLConnection(Get,Post方式)进行网络通信 获取数据和网络图片
- Android——使用Http的get方式获取数据
- android中HTTP协议访问网络及其方式及其http的GET和POST的优缺点
- 在线程中使用HttpClicent获取网络数据(get和post两种方式)
- http-使用get和post方式提交数据
- http使用post和get方式提交数据
- android网络编程http的get,post方式
- WebView的使用(http协议访问网络Get和Post请求方式)
- Android之使用HTTP协议的Get/Post方式向服务器提交数据
- Android之使用HTTP协议的Get/Post方式向服务器提交数据
- Android之使用HTTP的get,post,HttpClient三种方式向服务器端提交文本数据
- Svn与Git的区别总结
- hihocoder 1121
- Android多线程之HandlerThread
- MAC生成器
- js-图像img预加载
- 【Android】使用http的Post和Get方式获取网络数据
- VMware Ubuntu 下与Win7共享文件夹
- 释迦牟尼:最经典和灵性的4句话
- ActivityManifest里面windowSoftInputMode属性简介
- 【杭电2004】成绩转换
- 【杭电 5363】Key Set
- 简单实用,Gson解析
- 锤子剪子布游戏
- SAP的模块化服务应用市场Hybris,前景如何?