HTML5技术

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

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

doGet( final String url){ 3 doGet(-1, url, null ); 4 } doGet( final int requestCode, final String url){ 7 doGet(requestCode, url, null ); 8 } doGet( requestCode, 12 final String url, 13 final LghHttp

doGet(final String url){ 3 doGet(-1, url, null); 4 } doGet(final int requestCode,final String url){ 7 doGet(requestCode, url, null); 8 } doGet( requestCode, 12 final String url, 13 final LghHttpSingleListener lghHttpListeners) 14 { 15 Runnable runnable = new Runnable() { 16 @Override run() { 18 get(requestCode, url, lghHttpListeners); 19 } 20 }; 21 if(threadPool != null){ 22 threadPool.execute(runnable); 23 }else{ 24 Log.d(TAG,"do get threadPool is null"); 25 } 26 } get(int requestCode,String url,LghHttpSingleListener lghHttpListener){ 29 try { 30 HttpURLConnection httpURLConnection = getHttpUrlConnection(url,"GET"); 31 httpURLConnection.setUseCaches(false); 32 sendMessage(Success,requestCode, commonGetResult(httpURLConnection,lghHttpListener)); 33 } catch (MalformedURLException e) { 34 dealWithException(e,lghHttpListener); 35 } catch (IOException e) { 36 dealWithException(e,lghHttpListener); 37 } 38 }

View Code

    2)Post 请求操作

doPost(String url){ 3 doPost(-1, url); 4 } doPost(int requestCode,String url){ 7 doPost(requestCode, url, null, null); 8 } doPost(int requestCode,String url,LghHttpSingleListener listener){ 11 doPost(requestCode, url, null, null,listener); 12 } doPost(int requestCode,String url,String[] keys,String[] values){ 15 doPost(requestCode, url, keys, values, null); 16 } doPost( requestCode, 20 final String url, 21 final String[] keys, 22 final String[] values, 23 final LghHttpSingleListener listener 24 ){ 25 Runnable runnable = new Runnable() { 26 @Override run() { 28 post(requestCode, url,keys,values, listener); 29 } 30 }; 31 if(threadPool != null){ 32 threadPool.execute(runnable); 33 }else{ 34 Log.d(TAG,"do post threadPool is null"); 35 } 36 } post( 40 int requestCode, 41 String url, 42 String[] keys, 43 String[] values, 44 LghHttpSingleListener listener 45 ){ 46 if(url==null){ 47 return; 48 } 49 try{ 50 HttpURLConnection httpURLConnection = getHttpUrlConnection(url,"POST"); httpURLConnection.setUseCaches(false); (keys!=null && values!=null){ 55 OutputStream outputStream = httpURLConnection.getOutputStream(); 56 commonCombinePostText(keys,values,outputStream); 57 outputStream.flush(); 58 outputStream.close(); 59 } 60 sendMessage(Success,requestCode, commonGetResult(httpURLConnection,listener)); 61 }catch (MalformedURLException e){ 62 dealWithException(e,listener); 63 } catch (SocketTimeoutException e){ 64 dealWithException(e,listener); 65 } catch (ProtocolException e) { 66 dealWithException(e,listener); 67 } catch (UnsupportedEncodingException e) { 68 dealWithException(e,listener); 69 } catch (IOException e) { 70 dealWithException(e,listener); 71 } 72 }

View Code

    3)上传图片操作,Tcp 数据包的一些知识,在这里有用到。

 

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

网友点评
%