HTML5技术

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

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

dealWithException( 3 Exception e, 4 LghHttpSingleListener lghHttpListeners) 5 { 6 HttpDataBean bean = new HttpDataBean(); 7 bean.setListeners(lghHttpListeners); 8 if (e instanceof MalformedURLExcepti

dealWithException( 3 Exception e, 4 LghHttpSingleListener lghHttpListeners) 5 { 6 HttpDataBean bean = new HttpDataBean(); 7 bean.setListeners(lghHttpListeners); 8 if(e instanceof MalformedURLException){ 9 Log.d(TAG, "链接格式有问题 "+e.toString()); 10 sendMessage(UrlFailed,bean); 11 }else if(e instanceof SocketTimeoutException){ 12 Log.d(TAG, "连接超时 "+e.toString()); 13 sendMessage(TimeOut,bean); 14 }else if(e instanceof ProtocolException){ 15 Log.d(TAG, "协议异常,注意不要多次连接 " + e.toString()); 16 sendMessage(ProtocolFailed, bean); 17 }else if(e instanceof UnsupportedEncodingException){ 18 Log.d(TAG, "编码类型异常 " + e.toString()); 19 sendMessage(EncodingFailed, bean); 20 }else if(e instanceof IOException){ 21 Log.d(TAG, "io 异常 " + e.toString()); 22 sendMessage(IOFailed,bean); 23 } 24 } HttpURLConnection getHttpUrlConnection 28 (String url,String requestWay) throws IOException { 29 Log.d(TAG,"url is "+url); 30 URL mRrl = new URL(url); 31 HttpURLConnection httpURLConnection = (HttpURLConnection) mRrl.openConnection(); 32 httpURLConnection.setRequestMethod(requestWay); 33 httpURLConnection.setRequestProperty("Charset", "UTF-8"); 34 httpURLConnection.setConnectTimeout(5 * 1000); 35 return httpURLConnection; 36 } HttpDataBean commonGetResult( 40 HttpURLConnection httpURLConnection, 41 LghHttpSingleListener listener 42 ) throws IOException { 43 if(httpURLConnection==null){ ; 45 } 46 BufferedReader br = new BufferedReader 47 ( 48 new InputStreamReader(httpURLConnection.getInputStream(),"UTF-8"), 49 8*1024 50 ); 51 StringBuffer resultBuffer = new StringBuffer(""); 52 String line; 53 while ((line = br.readLine())!=null){ 54 resultBuffer.append(line); 55 } 56 HttpDataBean bean = new HttpDataBean(); 57 bean.setResponse(resultBuffer.toString()); 58 bean.setListeners(listener); 59 br.close(); 60 return bean; 61 } OutputStream commonCombinePostText( 65 String[] keys, 66 String[] values, 67 OutputStream outputStream) throws IOException 68 { 69 StringBuffer requestStr = new StringBuffer(); 70 int keysLength = keys.length; 71 for(int i=0;i<keysLength;i++){ 72 requestStr.append(keys[i]+"="+values[i]+"&"); 73 } 74 outputStream.write(requestStr.toString().getBytes()); 75 return outputStream; 76 }

View Code

    2)数据部分

 

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

网友点评
i