HttpURLConnection的GET请求POST请求小总结
来源:互联网 发布:机械绘图软件 编辑:程序博客网 时间:2024/05/20 18:49
先搭建一个简单的服务器,具体方法见我另外一篇文章测试HttpUrlConnection请求时如何搭建一个简单的服务器
GET请求代码如下:
//网络请求是一个耗时操作,要在子线程里面开启 new Thread(new Runnable() { @Override public void run() { try { //get请求的url URL url=new URL("http://192.168.2.135:8080/web/MyProject?username=zhangsan&password=123"); //url写法: 协议://IP地址:端口号/访问的文件 //协议一般是http,IP地址是要访问的IP地址,端口一般是8080,然后加上访问的是什么 //新建一个HttpURLConnection,从url开启一个连接 HttpURLConnection connection= (HttpURLConnection) url.openConnection(); //设置请求的模式 connection.setRequestMethod("GET"); //设置请求连接超时时间 connection.setConnectTimeout(5000); //设置访问时的超时时间 connection.setReadTimeout(5000); //开启连接 connection.connect(); InputStream inputStream=null; BufferedReader reader=null; //如果应答码为200的时候,表示成功的请求带了,这里的HttpURLConnection.HTTP_OK就是200 if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){ //获得连接的输入流 inputStream=connection.getInputStream(); //转换成一个加强型的buffered流 reader=new BufferedReader(new InputStreamReader(inputStream)); //把读到的内容赋值给result final String result=reader.readLine(); //子线程不能更新UI线程的内容,要更新需要开启一个Ui线程,这里Toast要在Ui线程 runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show(); } }); } //关闭流和连接 reader.close(); inputStream.close(); connection.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start();POST请求是看不到URL里面的头文件内容的,把头文件内容从连接的输出流中写入,这样相对于GET请求会比较安全些
//网络请求是一个耗时操作,要在子线程里面开启 new Thread(new Runnable() { @Override public void run() { try { //get请求的url URL url=new URL("http://192.168.2.135:8080/web/MyProject"); //url写法: 协议://IP地址:端口号/访问的文件 //协议一般是http,IP地址是要访问的IP地址,端口一般是8080,然后加上访问的是什么 //新建一个HttpURLConnection,从url开启一个连接 HttpURLConnection connection= (HttpURLConnection) url.openConnection(); //设置请求的模式 connection.setRequestMethod("POST"); //设置请求连接超时时间 connection.setConnectTimeout(5000); //设置访问时的超时时间 connection.setReadTimeout(5000); //设置连接允许写入 connection.setDoOutput(true); //设置头文件内容 String data="username=zhangsan&password=123"; //获得连接的输出流 OutputStream outputStream = connection.getOutputStream(); //把头文件内容写入流中 outputStream.write(data.getBytes()); //开启连接 connection.connect(); InputStream inputStream=null; BufferedReader reader=null; //如果应答码为200的时候,表示成功的请求带了,这里的HttpURLConnection.HTTP_OK就是200 if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){ //获得连接的输入流 inputStream=connection.getInputStream(); //转换成一个加强型的buffered流 reader=new BufferedReader(new InputStreamReader(inputStream)); //把读到的内容赋值给result final String result=reader.readLine(); //子线程不能更新UI线程的内容,要更新需要开启一个Ui线程,这里Toast要在Ui线程 runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show(); } }); } //关闭流和连接 reader.close(); inputStream.close(); connection.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start();简单的下载服务器图片的代码
new Thread(new Runnable() { @Override public void run() { try { //创建一个url,指定要下载的图片的url URL url=new URL("http://192.168.2.135:8080/web/aaa.bmp"); //从url打开连接,下载图片默认是GET请求 HttpURLConnection connection= (HttpURLConnection) url.openConnection(); //连接 connection.connect(); //如果应答码是200,代表连接上了 if(connection.getResponseCode()==200){ //从连接获得输入流 InputStream inputStream=connection.getInputStream(); //把输入流转成bitmap final Bitmap map= BitmapFactory.decodeStream(inputStream); runOnUiThread(new Runnable() { @Override public void run() { //把bitmap设置到控件上 imageView.setImageBitmap(map); } }); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start();简单的下载某个文件的代码
//设置一个progressdialog final ProgressDialog diglog=new ProgressDialog(this); //设置diglog的样式 diglog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //设置diglog显示出来 diglog.show(); new Thread(new Runnable() { @Override public void run() { try { //要下载文件的url URL url=new URL("http://192.168.2.135:8080/web/mymusic.mp3"); //从url打开连接 HttpURLConnection connection= (HttpURLConnection) url.openConnection(); //获得文件的大小 diglog.setMax(connection.getContentLength()); //建立连接 connection.connect(); //设置下载到的地方 final File file=new File(Environment.getExternalStorageDirectory(),"/mymusic.mp3"); if(connection.getResponseCode()==200){ //获得连接的输入流 InputStream inputStream=connection.getInputStream(); //新建一个输出流 FileOutputStream fos=new FileOutputStream(file); //设置每次读取的内容 byte[] bytes=new byte[1024]; //设置每次读取到的内容的长度 int len=-1; while ((len=inputStream.read(bytes))!=-1){ //把读到的内容写入到内存里 fos.write(bytes,0,len); //设置diglog每次的增长值 diglog.incrementProgressBy(len); } //文件下载完设置进度条隐藏 diglog.dismiss(); //关闭流和连接 fos.close(); inputStream.close(); } connection.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start();
最后要提下注意的几点
子线程是一个耗时操作,不能在子线程中更新UI
如果要在子线程中更新UI,可以开启一个UI线程,或者使用消息机制
还要在清单配置文件那边加上INTERNET权限
ProgressDiglog是个特殊的控件,可以在子线程中来dissmiss掉
下载文件的时候,注意下载了什么格式的文件,下载下来要存到SD卡里的文件就要是什么格式
如果要下载很多张图片,用Bitmap会有OutofMemory(内存溢出)风险,
这里可以使用第三方来解决,比如Blide,Imageloader
2 0
- HttpURLConnection的GET请求POST请求小总结
- HttpUrlConnection的GET请求和POST请求
- httpurlconnection的Get请求和Post请求
- HttpURLConnection的post请求总结
- HttpURLConnection的get请求总结
- HttpURLConnection 的get跟post请求
- HttpURLConnection的get和post请求
- HttpURLConnection的GET和POST请求
- HttpURLConnection的get和post请求实例
- 【Java】HttpURLConnection,HttpClient 的 Get,Post 请求
- HttpUrlConnection的get和post请求方法
- HttpURLConnection的GET和POST请求方式
- 可靠的HttpUrlConnection实现get/post请求
- Java HttpURLConnection的post和get请求
- HttpURLConnection的get和post请求实例
- HttpUrlConnection post和get请求
- HttpURLConnection,get,post,网络请求
- HttpURLconnection/get和post请求
- 算法 存一下
- 广义表的递归实现
- 基于模糊滤波的数字图像去噪
- 中国流行歌手普遍缺乏科学的高音。
- SpringMVC工作原理
- HttpURLConnection的GET请求POST请求小总结
- javaBean之useBean,setProperty,getProperty
- 关于链表的认识
- 乘法逆元(密码学)
- spring 事务使用的坑
- 01背包+完全背包问题
- 在Android模拟器上安装apk的时候出现 INSTALL_FAILED_NO_MATCHING_ABIS
- 第一章 Shiro简介——《跟我学Shiro》
- Multicast 组播