Android开发入门:简单的远程调试日志


在Android手机上调试程序时,由于没有数据线,就建立了一个无线局域网和本机服务器做离线调试。

需要打印日志的地方调用WL.log("key","value");即可

  1. public class WL { 
  2.     static String url = "http://10.12.11.54:8080/log/imageLog"
  3.  
  4.     public static void Log(String key ,String value){ 
  5.         HttpURLConnection con =null
  6.         try { 
  7.             URL uri = new URL(url+"?"+key+"="+value); 
  8.             con = (HttpURLConnection) uri.openConnection(); 
  9.             con.setRequestMethod("POST"); 
  10.             con.getResponseCode(); 
  11.         } catch (MalformedURLException e) { 
  12.             e.printStackTrace(); 
  13.         } catch (IOException e) { 
  14.             e.printStackTrace(); 
  15.         } 
  16.     } 

另服务端的几行代码:

 

  1. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  2.         try
  3.             Enumeration<String> enu = request.getParameterNames(); 
  4.             while(enu.hasMoreElements()){ 
  5.                 String logName = enu.nextElement().toString(); 
  6.                 System.out.println("Time:"new Date().toLocaleString()+"    "+logName+" :"+request.getParameter(logName)); 
  7.             } 
  8.         }catch (Exception e) { 
  9.             e.printStackTrace(); 
  10.         } 
  11.     } 

更多Android相关信息见Android 专题页面 http://www.bkjia.com/topicnews.aspx?tid=11

相关内容