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 Code2)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 Code3)上传图片操作,Tcp 数据包的一些知识,在这里有用到。