Android使用HttpUrlConnection请求服务器发送数据详解
来源:互联网 发布:今天原油api数据 编辑:程序博客网 时间:2024/06/05 04:37
版权声明:本文为博主原创文章,未经博主允许不得转载。转载注明出处:Sunzxyong
在Java中可以使用HttpURLConnection发起这两种请求,了解此类,对于了解soap,和编写servlet的自动测试代码都有很大的帮助。
一、那么我们先来看看用HttpUrlConnection怎样用Post发起请求传输json数据(这里以常用的json作为示例):
- public void httpUrlConnPost(String name,String password){
- HttpURLConnection urlConnection = null;
- URL url = null;
- try {
- url = new URL("http://192.168.1.105:8080/KYDServer/servlet/Login");
- urlConnection = (HttpURLConnection) url.openConnection();//打开http连接
- urlConnection.setConnectTimeout(3000);//连接的超时时间
- urlConnection.setUseCaches(false);//不使用缓存
- //urlConnection.setFollowRedirects(false);是static函数,作用于所有的URLConnection对象。
- urlConnection.setInstanceFollowRedirects(true);//是成员函数,仅作用于当前函数,设置这个连接是否可以被重定向
- urlConnection.setReadTimeout(3000);//响应的超时时间
- urlConnection.setDoInput(true);//设置这个连接是否可以写入数据
- urlConnection.setDoOutput(true);//设置这个连接是否可以输出数据
- urlConnection.setRequestMethod("POST");//设置请求的方式
- urlConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");//设置消息的类型
- urlConnection.connect();// 连接,从上述至此的配置必须要在connect之前完成,实际上它只是建立了一个与服务器的TCP连接
- JSONObject json = new JSONObject();//创建json对象
- json.put("name", URLEncoder.encode(name, "UTF-8"));//使用URLEncoder.encode对特殊和不可见字符进行编码
- json.put("password", URLEncoder.encode(password, "UTF-8"));//把数据put进json对象中
- String jsonstr = json.toString();//把JSON对象按JSON的编码格式转换为字符串
- //-------------使用字节流发送数据--------------
- //OutputStream out = urlConnection.getOutputStream();
- //BufferedOutputStream bos = new BufferedOutputStream(out);//缓冲字节流包装字节流
- //byte[] bytes = jsonstr.getBytes("UTF-8");//把字符串转化为字节数组
- //bos.write(bytes);//把这个字节数组的数据写入缓冲区中
- //bos.flush();//刷新缓冲区,发送数据
- //out.close();
- //bos.close();
- //------------字符流写入数据------------
- OutputStream out = urlConnection.getOutputStream();//输出流,用来发送请求,http请求实际上直到这个函数里面才正式发送出去
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));//创建字符流对象并用高效缓冲流包装它,便获得最高的效率,发送的是字符串推荐用字符流,其它数据就用字节流
- bw.write(jsonstr);//把json字符串写入缓冲区中
- bw.flush();//刷新缓冲区,把数据发送出去,这步很重要
- out.close();
- bw.close();//使用完关闭
- if(urlConnection.getResponseCode()==HttpURLConnection.HTTP_OK){//得到服务端的返回码是否连接成功
- //------------字节流读取服务端返回的数据------------
- //InputStream in = urlConnection.getInputStream();//用输入流接收服务端返回的回应数据
- //BufferedInputStream bis = new BufferedInputStream(in);//高效缓冲流包装它,这里用的是字节流来读取数据的,当然也可以用字符流
- //byte[] b = new byte[1024];
- //int len = -1;
- //StringBuffer buffer = new StringBuffer();//用来接收数据的StringBuffer对象
- //while((len=bis.read(b))!=-1){
- //buffer.append(new String(b, 0, len));//把读取到的字节数组转化为字符串
- //}
- //in.close();
- //bis.close();
- //Log.d("zxy", buffer.toString());//{"json":true}
- //JSONObject rjson = new JSONObject(buffer.toString());//把返回来的json编码格式的字符串数据转化成json对象
- //------------字符流读取服务端返回的数据------------
- InputStream in = urlConnection.getInputStream();
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- String str = null;
- StringBuffer buffer = new StringBuffer();
- while((str = br.readLine())!=null){//BufferedReader特有功能,一次读取一行数据
- buffer.append(str);
- }
- in.close();
- br.close();
- JSONObject rjson = new JSONObject(buffer.toString());
- Log.d("zxy", "rjson="+rjson);//rjson={"json":true}
- boolean result = rjson.getBoolean("json");//从rjson对象中得到key值为"json"的数据,这里服务端返回的是一个boolean类型的数据
- if(result){//判断结果是否正确
- mHandler.sendEmptyMessage(USERLOGIN_SUCCESS);
- }else{
- mHandler.sendEmptyMessage(USERLOGIN_FAILED);
- }
- }else{
- mHandler.sendEmptyMessage(USERLOGIN_FAILED);
- }
- } catch (Exception e) {
- mHandler.sendEmptyMessage(USERLOGIN_FAILED);
- }finally{
- urlConnection.disconnect();//使用完关闭TCP连接,释放资源
- }
- }
服务端的代码为:(使用HttpUrlConnection,服务端是用输入流来读取客户端发过来的数据。而使用HttpClient的话,则服务端用相应的key值来获取数据)
- public void doPost(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- request.setCharacterEncoding("UTF-8");
- System.out.println(request.getContentType());//得到客户端发送过来内容的类型,application/json;charset=UTF-8
- System.out.println(request.getRemoteAddr());//得到客户端的ip地址,192.168.1.101
- //------------使用字节流读取客户端发送过来的数据------------
- // BufferedInputStream bis = new BufferedInputStream(request.getInputStream());
- // byte[] b = new byte[1024];
- // int len=-1;
- // StringBuffer buffer = new StringBuffer();
- // while((len=bis.read(b))!=-1){
- // buffer.append(new String(b, 0, len));
- // }
- // bis.close();
- // System.out.println("buffer="+buffer);
- // JSONObject json = JSONObject.fromObject(buffer.toString());
- BufferedReader br = new BufferedReader(new InputStreamReader(//使用字符流读取客户端发过来的数据
- request.getInputStream()));
- String line = null;
- StringBuffer s = new StringBuffer();
- while ((line = br.readLine()) != null) {
- s.append(line);
- }
- br.close();
- System.out.println(s.toString());//{"password":"123456","name":"admin"}
- JSONObject json = JSONObject.fromObject(s.toString());//转化为jSONObject对象
- String name = json.getString("name");//从json对象中得到相应key的值
- String password = json.getString("password");
- response.setCharacterEncoding("UTF-8");
- response.setContentType("application/json;charset=UTF-8");
- if (name.equals("admin") && password.equals("123456")) {
- JSONObject rjson = new JSONObject();
- rjson.put("json", true);//返回boolean类型的值
- System.out.println("rjson=" + rjson);
- // response.getWriter().write(rjson.toString());//向客户端发送一个带有json对象内容的响应
- response.getOutputStream().write(rjson.toString().getBytes("UTF-8"));//向客户端发送一个带有json对象内容的响应
- } else {
- JSONObject rjson = new JSONObject();
- rjson.put("json", false);
- System.out.println("rjson=" + rjson);
- // response.getWriter().write(rjson.toString());//向客户端发送一个带有json对象内容的响应
- response.getOutputStream().write(rjson.toString().getBytes("UTF-8"));//向客户端发送一个带有json对象内容的响应
- }
- }
二、我们再来看看用Get请求:
- public void httpUrlConnGet(String name,String password){
- HttpURLConnection urlConnection = null;
- URL url = null;
- try {
- String urlStr = "http://192.168.1.105:8080/KYDServer/servlet/Login?name="+name+"&password="+password;
- url = new URL(urlStr);
- urlConnection = (HttpURLConnection) url.openConnection();
- urlConnection.connect();
- if(urlConnection.getResponseCode()==HttpURLConnection.HTTP_OK){
- InputStream in = urlConnection.getInputStream();
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- String line = null;
- StringBuffer buffer = new StringBuffer();
- while((line=br.readLine())!=null){
- buffer.append(line);
- }
- in.close();
- br.close();
- String result = buffer.toString();
- if(result.equals("ok")){
- mHandler.sendEmptyMessage(USERLOGIN_SUCCESS);
- }else{
- mHandler.sendEmptyMessage(USERLOGIN_FAILED);
- }
- }else{
- mHandler.sendEmptyMessage(CONN_FIALED);
- }
- } catch (Exception e) {
- mHandler.sendEmptyMessage(CONN_FIALED);
- }finally{
- urlConnection.disconnect();
- }
- }
所以总结一下:
- 使用HttpUrlConnection请求服务器的话:
用Get请求:客户端的数据是放在url中的,通过url来传给服务器。服务端接收客户端发来的数据是用对应的key值的,服务端返回数据给客户端是用response.getWriter().write()或response.getOutputStream().write()来返回的,客户端接收服务端返回来的数据是用urlConnection.getInputStream()输入流来读取的。
用Post请求:客户端的数据是放在正文中的,即是通过urlConnection.getOutputStream()输出流来发送给服务器的。服务端接收客户端发来的数据是用request.getInputStream()输入流来读取的,服务端返回数据给客户端是用response.getWriter().write()或response.getOutputStream().write()来返回的,客户端接收服务端返回来的数据是用urlConnection.getInputStream()输入流来读取的。【注】:使用HttpUrlConnection客户端的发送和接收数据都是用流操作的。
- 使用HttpClient请求服务器的话:
用Get请求:客户端的数据是放在url中的,通过url传给服务器。服务端接收客户端发来的数据是用对应的key值的,服务端返回数据给客户端是用response.getWriter().write()或response.getOutputStream().write()来返回的,客户端接收服务端返回来的数据是用
response.getEntity()来接收。
用Post请求:客户端的数据是放在List<NameValuePair>parameters集合中,通过post.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"))发送给服务器。服务端接收客户端发来的数据是用对应的key值的,服务端返回数据给客户端是用response.getWriter().write()或response.getOutputStream().write()来返回的,客户端接收服务端返回来的数据是用
response.getEntity()来接收。
HttpURLConnection.connect()函数,实际上只是建立了一个与服务器的tcp连接,并没有实际发送http请求。无论是post还是get,http请求实际上直到HttpURLConnection.getInputStream()这个函数里面才正式发送出去。
在HttpUrlConnection中,顺序是重中之重,对urlConnection对象的一切配置(那一堆set函数)都必须要在connect()函数执行之前完成。
http请求实际上由两部分组成,一个是http头,所有关于此次http请求的配置都在http头里面定义,另一个是正文content。在connect()函数里面,会根据HttpURLConnection对象的配置值生成http头,因此在调用connect函数之前,就必须把所有的配置准备好。
紧接着http头的是http请求的正文,正文的内容通过outputStream写入,实际上outputStream不是一个网络流,充其量是个字符串流,往里面写入的东西不会立即发送到网络,而是在流关闭后,根据输入的内容生成http正文。
转载请注明出处!!!
- Android使用HttpUrlConnection请求服务器发送数据详解
- Android使用HttpUrlConnection请求服务器发送数据详解
- Android使用HttpUrlConnection请求服务器发送数据详解
- Android使用HttpURLConnection向服务器发送post和get请求
- Android使用HttpURLConnection和HttpClient请求服务器数据
- Android使用HttpURLConnection进行POST请求,向服务器上传数据
- 使用HttpURLConnection发送XML请求数据
- Android 使用HttpURLConnection发送Post/Get请求
- Android使用HttpURLConnection发送Get请求
- Android上使用HttpURLConnection发送HTTP请求
- android使用HttpURLConnection发送Post/Get请求
- Android使用HttpURLConnection发送HTTP请求
- HttpURLConnection往服务器发送请求
- 使用HttpURLConnection向服务器发送post和get请求
- 使用HttpURLConnection向服务器发送post和get请求
- 使用HttpURLConnection向服务器发送post和get请求
- 使用HttpURLConnection向服务器发送post和get请求
- HttpUrlConnection 发送POST请求数据
- 链表操作
- 输入法dialog、window
- Android 单元测试01--JUnit测试
- 搜索收录推送
- HTTP协议详解(真的很经典)
- Android使用HttpUrlConnection请求服务器发送数据详解
- WPF的RadioButton若界面重载可能会导致RadioButton不可用
- PLSQL Developer怎么查看一个表的建表语句
- Day2 of 6
- windows系统安装ubuntu后,grub中没有windows启动项
- MYSQL简单操作(eclipse)
- EL表达式中的${}出现难以查到的JasperException问题
- Hibernate(JPA)注解大全
- JSON详解