安卓开发-Get和Post两种请求三种方式(模板)
来源:互联网 发布:房子平面设计图软件 编辑:程序博客网 时间:2024/06/03 18:06
《方式一,简单网络操作》
1.get请求:
//1.地址栏上的地址URL url = new URL("带参数url"); //2.似回车,打开链接HttpURLConnection conn = (HttpURLConnection)url.openConnection();//3.得告诉服务器是什么请求方式(GET)conn.setRequestMethod("GET");//4.判断响应码if(conn.getResponseCode() == 200){//5.获取输入流 InputStream is = conn.getInputStream();}
2.post请求:
//1.地址栏上的地址URL url = new URL("不带参数地址"); //2.似回车,打开链接HttpURLConnection conn = (HttpURLConnection)url.openConnection();//3.1.得告诉服务器是什么请求方式(POST)//3.2.设置请求头参数: //3.2.1. 告诉服务器是form表单提交数据的 //3.2.2. 告诉服务器数据的长度conn.setRequestMethod("POST");conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");conn.setRequestProperty("Content-Length", urlParams.length() + "");//4.将参数输出到服务器上,在安卓中HttpURLCnnnection的输出流(字节流)开关默认是关闭的,的手动设置输出流开关。conn.setDoOutput(true);conn.getOutputStream().write(urlParams.getBytes());//5.判断响应码if(conn.getResponseCode() == 200){ //6.获取输入流 InputStream is = conn.getInputStream(); }
3.get请求和post请求的区别:
1.告诉服务区是什么请求方式不同。
2.get请求只需要在url中添加参数的名称和值即可,post请求需要:
2.(1)告诉服务器是什么提交方式(表单)
2.(2)参数的长度
2.(3)参数以流的方式从客户端发送到服务器端【HttpURLConnection的输出流默认是关闭的,得手动打开】。
《方式二,Httpclient方式》
1.HttpGet请求:
//1.创建一个客户端,相当于浏览器HttpClient client = new DefaultHttpClient();//2.声明一个HpptGet请求HttpGet get = new HttpGet("带参数地址");//3.执行请求后得到响应对象HttpResponse response = client.execute(post);//4.通过响应对象得到响应行从而得到相应码int statusCode = response.getStatusLine().getStatusCode();if(statusCode == 200){ //5.从HttpEntity中得到相应过来的输入流(不管是请求的数据还是响应过来的数据都封装到HttpEntity中) HttpEntity entitys = response.getEntity(); InputStream is = entitys.getContent();}
2.HttpPost请求:
//1.创建一个客户端,相当于浏览器HttpClient client = new DefaultHttpClient();//2.声明一个HpptPost请求HttpPost post = new HttpPost("不带参数地址");//3.添加post的参数(不管是请求的数据还是响应过来的数据都封装到HttpEntity中)List<BasicNameValuePair> parameters = new ArrayList<>();parameters.add(new BasicNameValuePair("username", username));parameters.add(new BasicNameValuePair("pwd", pwd));HttpEntity entity = new UrlEncodedFormEntity(parameters);post.setEntity(entity);//4.执行请求后得到响应对象HttpResponse response = client.execute(post);//5.通过响应对象得到响应行从而得到相应码int statusCode = response.getStatusLine().getStatusCode();if(statusCode == 200){ //6.从HttpEntity中得到相应过来的输入流(不管是请求的数据还是响应过来的数据都封装到HttpEntity中) HttpEntity entitys = response.getEntity(); InputStream is = entitys.getContent();}
《方式三,AsyncHttpClient方式,需引入第三方文件,如图》
1.get请求:
//1.声明一个AsyncHttpClient客户端AsyncHttpClient client = new AsyncHttpClient();//2.在客户端中完成参数的设置RequestParams params = new RequestParams();params.put("username", username);params.put("pwd", pwd);client.get("带参数地址", new TextHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, String responseString) { Toast.makeText(MainActivity.this, responseString, Toast.LENGTH_SHORT).show(); } @Override public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { // TODO Auto-generated method stub }});
2.post请求(表单图片上传半实例):
//1.声明一个AsyncHttpClient客户端AsyncHttpClient client = new AsyncHttpClient();//2.在客户端中完成参数的设置String url = "不带参数地址";RequestParams params = new RequestParams();params.put("fName", "myPic.png");File file = new File(getFilesDir(), "a.jpg");params.put("file", file);client.post(url , params, new TextHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, String responseString) { Toast.makeText(MainActivity.this, responseString, Toast.LENGTH_SHORT).show(); } @Override public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { }});
阅读全文
0 0
- 安卓开发-Get和Post两种请求三种方式(模板)
- 安卓开发请求服务器的两种方式GET与POST(另附验证码的倒计时)
- android中post和get两种方式发送请求
- GET和POST两种请求方式的区别
- Android中post和get两种方式发送请求
- request请求获取参数(post和get两种方式)
- OkHttp使用Get和Post两种请求方式
- request请求获取参数(post和get两种方式)
- GET和POST两种请求方式的区别
- 学习笔记之安卓GET与POST网络请求的三种方式
- C# wnform 请求http ( get , post 两种方式 )
- C# winform 请求http ( get , post 两种方式 )
- C# wnform 请求http ( get , post 两种方式 )
- 前端发送get和post请求的三种方式
- httpURLConnection-网络请求的两种方式-get请求和post请求
- httpURLConnection-网络请求的两种方式-get请求和post请求
- iOS 网络请求数据两种请求方式GET,POST
- GET 和 POST两种调用方式
- 机器学习算法中几种常用距离计算的归纳
- Makefile示例
- Spring中@Autowired注解、@Resource注解的区别
- Android OpenGLES2.0(一)——了解OpenGLES2.0
- Opegl随笔
- 安卓开发-Get和Post两种请求三种方式(模板)
- 在SuperMap iClient 9D for MapboxGL中使用ECharts和MapV
- Windows8.1&&10 安装 Tensorflow 遇到的问题
- eclipse搭建安卓环境
- 新浪前端笔试题
- 【软考之旅】第十二章 面向对象
- 【ES6】Iterator和Generator
- WPF编译时出现【程序“\MVC_DHCC.exe”不包含适合于入口点的静态“Main”方法】的错误
- ctags,cscope,nerdtree,tmux 笔记