HTML5技术

如何独立开发一个网络请求框架 - 指尖下的幽灵(3)

字号+ 作者:H5之家 来源:博客园 2016-08-15 11:00 我要评论( )

doUpLoadPic( 3 String url, 4 String picName, 5 String streamName, 6 Bitmap bit 7 ){ 8 doUpLoadPic(-1, url, null , null , picName, streamName, bit, null ); 9 } doUpLoadPic( 12 int requestCode, 13 Stri

doUpLoadPic( 3 String url, 4 String picName, 5 String streamName, 6 Bitmap bit 7 ){ 8 doUpLoadPic(-1, url, null, null, picName, streamName, bit, null); 9 } doUpLoadPic( 12 int requestCode, 13 String url, 14 String picName, 15 String streamName, 16 Bitmap bit 17 ){ 18 doUpLoadPic(requestCode, url, null, null, picName, streamName, bit, null); 19 } doUpLoadPic( 22 int requestCode, 23 String url, 24 String picName, 25 String streamName, 26 Bitmap bit, 27 LghHttpSingleListener listener 28 ){ 29 doUpLoadPic(requestCode, url, null, null, picName, streamName, bit, listener); 30 } doUpLoadPic( 33 int requestCode, 34 String url, 35 String[] keys, 36 String[] values, 37 String picName, 38 String streamName, 39 Bitmap bit 40 ){ 41 doUpLoadPic(requestCode, url, keys, values, picName, streamName, bit, null); 42 } doUpLoadPic( requestCode, 46 final String url, 47 final String[] keys, 48 final String[] values, 49 final String picName, 50 final String streamName, 51 final Bitmap bit, 52 final LghHttpSingleListener listener 53 ){ 54 Runnable runnable = new Runnable() { 55 @Override run() { 57 UpLoadPic(requestCode, url, keys, values, picName, streamName, bit, listener); 58 } 59 }; 60 if(threadPool != null){ 61 threadPool.execute(runnable); 62 }else{ 63 Log.d(TAG,"do post threadPool is null"); 64 } 65 } * 此函数用来上传图片 69 * post 的 两种数据包格式: 70 * 1,application/x-www-form-urlencoded;用来上传文字 71 * 2,multipart/form-data; 二进制传输,除了文字之外,还可以用来传输 文件,例如图片! 72 * 3,multipart/form-data; 必须要带有分隔符 boundary 73 * 4,在http post请求的结尾,需要有一个分界线,但是是前后都有--的:--分隔符-- 74 * 参数: 75 * url 76 * picName 图片的名称 77 * streamName 流体值的名称 78 * 例如采用 php 接收,那么在服务器获取图片名称的写法是:$_FILES['streamName']['picName'] UpLoadPic( 81 int requestCode, 82 String url, 83 String[] keys, 84 String[] values, 85 String picName, 86 String streamName, 87 Bitmap bit, 88 LghHttpSingleListener listener 89 ){ String boundary = "******"; { 93 HttpURLConnection httpURLConnection = getHttpUrlConnection(url,"POST"); 94 httpURLConnection.setUseCaches(false); 95 httpURLConnection.setDoOutput(true); httpURLConnection.setRequestProperty("Connection","Keep-Alive"); 99 httpURLConnection.setRequestProperty("Content-Type","multipart/form-data;boundary="+boundary); DataOutputStream body = new DataOutputStream(httpURLConnection.getOutputStream()); body.writeBytes(twoHyphens+boundary+"\r\n"); body.writeBytes( 105 "Content-Disposition:form-data;" + 106 "name=\"" + streamName + "\";" + 107 "filename=\"" + picName + "\"" + "\r\n" 108 ); body.writeBytes("\r\n"); 111 if(keys!=null && values!=null){ 112 body.writeBytes(twoHyphens+boundary+"\r\n"); 113 body.writeBytes("Content-Disposition:form-data;"); 114 commonCombinePostText(keys,values,body); 115 body.writeBytes("\r\n"); 116 } compress = 100; 120 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 121 bit.compress(Bitmap.CompressFormat.JPEG, compress, baos); 122 if(IsOpenCompress){ 123 while (baos.toByteArray().length / 1024 > CompressLimit) { 124 baos.reset(); 125 compress -= 10; 126 if(compress==0){ 127 bit.compress(Bitmap.CompressFormat.JPEG, compress, baos); 128 break; 129 } 130 bit.compress(Bitmap.CompressFormat.JPEG, compress, baos); 131 } 132 } InputStream picStream = new ByteArrayInputStream(baos.toByteArray()); [10*1024]; 136 int count; 137 while((count = picStream.read(buffer))!=-1){ 138 body.write(buffer,0,count); 139 } 140 picStream.close(); 141 body.writeBytes("\r\n"); 142 body.writeBytes(twoHyphens + boundary + twoHyphens +"\r\n"); 143 body.flush(); sendMessage(Success,requestCode,commonGetResult(httpURLConnection,listener)); 146 body.close(); 147 }catch (MalformedURLException e){ 148 dealWithException(e,listener); 149 } catch (SocketTimeoutException e){ 150 dealWithException(e,listener); 151 } catch (ProtocolException e) { 152 dealWithException(e,listener); 153 } catch (UnsupportedEncodingException e) { 154 dealWithException(e,listener); 155 } catch (IOException e) { 156 dealWithException(e,listener); 157 } 158 }

View Code

 

  5,优点:

    1)绝对的轻量级,可以提升APK 的体积优化,没依赖其他第三方库

    2)内存管理方面可以放心

    3)请求速度方面是纯系统的 HttpUrlConnection,没有过多的代码片段

  6,可以进一步解耦拆分类,为了方便我自己使用所以我写在了一个里面,可以分为:

    1)公共部分

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 用TypeScript开发Vue——如何通过vue实例化对象访问实际ViewModel对象 - 小小沧海

    用TypeScript开发Vue——如何通过vue实例化对象访问实际ViewModel对

    2016-08-12 10:00

  • 拥抱.NET Core,如何开发一个跨平台类库 (1) - KAnts

    拥抱.NET Core,如何开发一个跨平台类库 (1) - KAnts

    2016-08-08 14:00

  • webkit webApp 开发技术要点总结 - -小克

    webkit webApp 开发技术要点总结 - -小克

    2016-07-29 17:00

  • ckplayer 如何在PC上完美支持 m3u8播放 - 冒雨ing

    ckplayer 如何在PC上完美支持 m3u8播放 - 冒雨ing

    2016-07-27 11:00

网友点评