Android开发之基础-------------Get请求和Post请求

向服务器提交请求时,有两种提交方式,下面对这两种提交方式进行简单的整理。

一、get方式 和 post方式 请求服务器的区别:

1. 请求的url地址不同
get:http://kr41m1i4fwt359b:8080/itheima75/servlet/LoginServlet?username=root&pwd=123
post:http://kr41m1i4fwt359b:8080/itheima75/servlet/LoginServlet
2. 请求头不同,post请求多了4个请求头和一个请求内容
Content-Length: 21
Cache-Control: max-age=0
Origin: http://kr41m1i4fwt359b:8080
Content-Type: application/x-www-form-urlencoded

username=root&pwd=123
3. 请求时携带数据大小不同
get:1k
post:理论上无限制

1.get请求实例代码:

String url_str = "http://192.168.20.82:8080/itheima75/servlet/LoginServlet?username="+username+"&pwd="+password;
				try{
					//1.创建url
					URL url = new URL(url_str);
					//2.获取一个UrlConnection
					HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
					//3.设置参数
					openConnection.setRequestMethod("GET");
					openConnection.setConnectTimeout(10*1000);
					//4.获取状态码,判断
					if(openConnection .getResponseCode() == 200){
						//5.获取流信息,转换字符串
						InputStream inputStream = openConnection.getInputStream();
						String result = StreamUtils.streamToString(inputStream);
						Message msg = Message.obtain();
						msg.obj = result;
						handler.sendMessage(msg);
						//6.关流,关连接
						inputStream.close();
						openConnection.disconnect();
					}

				}catch (Exception e) {
					e.printStackTrace();
				}
2.post请求实例代码:

String url_str = "http://192.168.20.82:8080/itheima75/servlet/LoginServlet";
				try{
					//1.创建url
					URL url = new URL(url_str);
					//2.获取一个UrlConnection
					HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
					//3.设置参数 ,设置一些请求头
					openConnection.setRequestMethod("POST");
					openConnection.setConnectTimeout(10*1000);
					
					String content = "username="+username+"&pwd="+password;//拼装请求时携带的数据
					openConnection.setRequestProperty("Content-Length", content.length()+"");//设置请求头 field:请求头的key,newValue :请求头的值
					openConnection.setRequestProperty("Cache-Control", "max-age=0");
					openConnection.setRequestProperty("Origin", "http://192.168.20.82:8080");
					openConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
					
					//设置当前的UrlConnection可以上传主题内容。
					openConnection.setDoOutput(true);
					//获取UrlConnection的一个写入流将主题内容写入,上传
					openConnection.getOutputStream().write(content.getBytes());
					//4.获取状态码,判断
					if(openConnection .getResponseCode() == 200){
						//5.获取流信息,转换字符串
						InputStream inputStream = openConnection.getInputStream();
						String result = StreamUtils.streamToString(inputStream);
						Message msg = Message.obtain();
						msg.obj = result;
						handler.sendMessage(msg);
						//6.关流,关连接
						inputStream.close();
						openConnection.disconnect();
					}
				}catch (Exception e) {
					e.printStackTrace();
				}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uni-app是一个跨平台的开发框架,可以同时开发iOS、Android和Web应用。在uni-app中,可以使用uni.request方法进行网络请求。 uni.request方法是封装了原生的XMLHttpRequest和fetch方法,可以发送HTTP请求并获取响应数据。它支持GET、POST等常见的请求方式,并且可以设置请求头、请求参数等。 使用uni.request方法发送网络请求的基本步骤如下: 1. 引入uni.request方法:在需要发送网络请求的页面或组件中,使用import语句引入uni.request方法。 2. 调用uni.request方法:使用uni.request方法发送网络请求,传入请求的URL、请求参数、请求头等信息。 3. 处理响应数据:在uni.request方法的回调函数中,可以获取到服务器返回的响应数据,并进行相应的处理。 以下是一个示例代码,演示了如何使用uni.request方法发送GET请求: ``` import uniRequest from '@/common/request.js'; uniRequest({ url: 'https://api.example.com/users', method: 'GET', success: (res) => { console.log(res.data); }, fail: (err) => { console.log(err); } }); ``` 在上述代码中,我们首先引入了一个名为uniRequest的封装方法,该方法封装了uni.request方法。然后,我们调用uniRequest方法发送了一个GET请求,并在成功回调函数中打印了服务器返回的数据。 需要注意的是,uni.request方法是异步的,所以我们需要通过回调函数来处理响应数据。在回调函数中,可以根据服务器返回的状态码、响应数据等进行相应的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值