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 Code2)数据部分