当前位置: 首页 > news >正文

做网站 套模板 后端工作室官网模板

做网站 套模板 后端,工作室官网模板,网页源代码查找指定文字,flash 网站源码安卓原生App开发时常用的http开发工具 系统内置http请求工具为 HttpURLConnectionhttpClient 是 apache 的开源工具okHttp 使用更简单#xff0c;语法相对HttpURLConnection也简洁了许多#xff0c;需要在graddle添加依赖。 本文主要讲解如何使用HttpURConnection向服务器发…安卓原生App开发时常用的http开发工具 系统内置http请求工具为 HttpURLConnectionhttpClient 是 apache 的开源工具okHttp 使用更简单语法相对HttpURLConnection也简洁了许多需要在graddle添加依赖。 本文主要讲解如何使用HttpURConnection向服务器发送Reqest, 保存Response内容如何上传文件等内容。 1. 使用 HttpURLConnection 发送http GET请求 step-1: 创建1个URL 对象 URL url new URL(http://www.yahoo.com);step-2: 创建1个 HttpURLConnection对象并绑定URL对象 object, HttpURLConnection httpConn (HttpURLConnection)url.openConnection();step-3: 设置请求方法以及其它必要选项 httpConn.setConnectTimeout(10000); httpConn.setRequestMethod(GET);设置了请求方法后就开始发送 step-4: 读取 Response 在httpConn对象上打开1个input stream, 用BufferReader的方式从httpConnect中读字节 InputStream inputStream httpConn.getInputStream(); InputStreamReader inputStreamReader new InputStreamReader(inputStream); BufferedReader bufferedReader new BufferedReader(inputStreamReader); //保存response内容 StringBuffer readTextBuf new StringBuffer(); String line bufferedReader.readLine(); while(line ! null) {readTextBuf.append(line);// Continue to read text line.line bufferedReader.readLine(); }step-5: 读完response以后此连接就不需要了别忘了关闭连接 httpConn.disconnect();2. 发送 POST请求步骤 与GET请求步骤不同在于POST 需要在消息体内携带请求参数因此要在httpConn连接上打开ouput stream才能发出 将第1节的step-3 改为如下 httpConn.setRequestMethod(GET); httpConn.setRequestProperty(“Key”,”Value”); httpConn.setDoOutput(true);Output the stream to the server OutputStream outputPost new BufferedOutputStream(httpConn.getOutputStream()); writeStream(outputPost); outputPost.flush(); outputPost.close();至此POST消息发送完成。 读取Response响应体的方法与上节相同。 下面是1个完整的发送POST 示例 (1)准备1个HashMap(类似于python字典保存请求参数 HashMapString, String params; //此处添加 params.put(name, Jack); params.put(company, Baidu);(2)将HashMap转为stringBuild类型 StringBuilder sbParams new StringBuilder();int i 0;for (String key : params.keySet()) {try {if (i ! 0){sbParams.append();}sbParams.append(key).append().append(URLEncoder.encode(params.get(key), UTF-8));} catch (UnsupportedEncodingException e) {e.printStackTrace();}i;}(3) 创建 HttpURLConnection 对象打开连接发送POST请求参数 try{String url http://www.example.com/test.php;URL urlObj new URL(url);HttpURLConnection conn (HttpURLConnection) urlObj.openConnection();conn.setDoOutput(true);conn.setRequestMethod(POST);conn.setRequestProperty(Accept-Charset, UTF-8);conn.setReadTimeout(10000);conn.setConnectTimeout(15000);conn.connect();String paramsString sbParams.toString(); //sbParams是前面准备好的DataOutputStream wr new DataOutputStream(conn.getOutputStream());wr.writeBytes(paramsString);wr.flush();wr.close(); } catch (IOException e) {e.printStackTrace(); }(4)接收response, 保存body 内容 try {InputStream in new BufferedInputStream(conn.getInputStream());BufferedReader reader new BufferedReader(new InputStreamReader(in));StringBuilder result new StringBuilder();String line;while ((line reader.readLine()) ! null) {result.append(line);}Log.d(test, result from server: result.toString());} catch (IOException e) {e.printStackTrace(); } finally {if (conn ! null) {conn.disconnect();} } 3. 发送Json POST request 与普通的POST请求不同JSON POST请求参数为json格式, 因此需要修改请求头部参数Content-Type与Accept参数 修改step-3步骤: httpConn.setRequestMethod(POST);设置Content-Type头部参数为“application/json” httpConn.setRequestProperty(Content-Type, application/json);设置“Accept”头部参数为“application/json” httpConn.setRequestProperty(Accept, application/json);设置连接准备发送数据 httpConn.setDoOutput(true);创建1个json body String jsonInputString {name: Upendra, job: Programmer};将json字符串写入output stream try(OutputStream os httpConn.getOutputStream()) {byte[] input jsonInputString.getBytes(utf-8);os.write(input, 0, input.length); }4. 用POST请求上传文件 上传文件时POST请求头部参数Content-Type使用 multipart/form-data, 实现也比较容易 URL url new URL(postTarget); HttpURLConnection connection (HttpURLConnection) url.openConnection();String auth Bearer oauthToken; connection.setRequestProperty(Authorization, basicAuth);String boundary UUID.randomUUID().toString(); connection.setRequestMethod(POST); connection.setDoOutput(true); connection.setRequestProperty(Content-Type, multipart/form-data;boundary boundary);DataOutputStream request new DataOutputStream(uc.getOutputStream());request.writeBytes(-- boundary \r\n); request.writeBytes(Content-Disposition: form-data; name\description\\r\n\r\n); request.writeBytes(fileDescription \r\n);request.writeBytes(-- boundary \r\n); request.writeBytes(Content-Disposition: form-data; name\file\; filename\ file.fileName \\r\n\r\n); request.write(FileUtils.readFileToByteArray(file)); request.writeBytes(\r\n);request.writeBytes(-- boundary --\r\n); request.flush(); int respCode connection.getResponseCode();switch(respCode) {case 200://all went ok - read response...break;case 301:case 302:case 307://handle redirect - for example, re-post to the new location...break;...default://do something sensible } 总结 使用 HttpURLConnection 发送请求流程还是比较清晰GET, POST,主要的区别在Step-3 步骤发送 JSON类型参数上传 file 还需要修改头部参数。 在Android App开发时可以在Activity用按钮事件来触发请求也可以使用service 在后台发送请求。 本文就不列出那些示例代码了。
http://www.w-s-a.com/news/657047/

相关文章:

  • 建设网站公司哪里好相关的热搜问题解决方案做网站要看什么书
  • 网站建设重要性黄岐建网站
  • 做网站电销《电子商务网站建设》精品课
  • 地方商城网站海外网站推广方法
  • 乐山 网站建设安阳给商家做网站推广
  • 网站空间一般多大邢台网站建设有哪些
  • h5网站开发工具有哪些wordpress清空post表
  • 公司开网站干嘛怎么制作一个免费的网站模板
  • 群晖wordpress搭建网站网站建设及管理
  • 中山企业网站建设公司抖音代运营合作模式
  • 南通营销网站开发做网站页面多少钱
  • 桂林生活网官方网站云主机和云电脑的区别
  • 内部网络网站怎么做vue做单页面网站
  • 如何建立网站教程wordpress粘帖图片
  • 广东网站备案要多久网站开发 pdf 文字版
  • 学校网站方案帮别人做钓鱼网站吗
  • 如何加强网站建设和信息宣传wordpress 搜索提示
  • 灰色网站怎么做php yaf 网站开发框架
  • 浙江建设网站首页提供做网站公司有哪些
  • 建公司网站报价公司seo是什么级别
  • 可信赖的武进网站建设中山网站建设方案
  • 网站设计方面有什么公司运动鞋网站建设目的
  • 学校门户网站流程建设方案找人做网站 多少钱
  • 网站域名更换相应内容网站策划 要求
  • 百盛联合建设集团网站开发网站的步骤
  • php做网站评价网络公司经营范围可以加技
  • 网站积分的作用保定专业网站建设
  • 莆田做网站公司电话如何提升网站访问速度
  • 网站开发流程步骤 口袋网页访问wordpress
  • 湘潭做网站的公司自助建站教程