android studio 3.1 tips

  • 测试程序都放在 samsung 的 SD CARD —- >  AndroidTestProgram 目录
  • 当实现一个派生类时, 鼠标放在派生类名字上 , 按 CTRL+I ,  可以选择要 implement 的 method
  • 如果有某个类没有import , 比如  Intent , 鼠标放在名字上,  按 Alt + Enter 可以 import
  • How to debug android program  ?

java 如何生成 json

来源网页 :  https://www.cnblogs.com/garfieldcgf/p/5967590.html

 

全文:

1.json简单介绍

1.1 json是最流行和广泛通用的数据传输格式,简称JavaScript Object Notation,最早在JavaScript中使用.

1.2 举个例子,下面是一个json对象,名字叫王尼玛,他有两个粉丝组成数组,一个叫小王,一个叫小尼玛:

{

“name”:”王尼玛”,

“fans”:[{

“name”:”小王”,

“age”:”7″

},{

“name”:”小尼玛”,

“age”:”10″

}]

}

2.json数据生成和解析

2.1 关于json处理的包有好几类,开源jackson,谷歌Gson,阿里巴巴的Fastjson.Gson功能强大,但是Fastjson性能更快.如何选择见仁见智就行

2.2 这里我开始都是用org.json,后面为了演示复杂json转java bean又导入了功能强大的Gjson,先附上两个maven依赖:

复制代码

复制代码

2.3 json数据的生成,也就是各种类型转json.(String,map和java bean)

复制代码

复制代码

2.3.1上面类缺少基本bean

Actor.java

复制代码

复制代码

2.4 json的解析,这里写了两种解析方式,同样,比较常用的还是第二种,json转java bean

DemoParseJson.java

复制代码

复制代码

2.4.1顺便附上执行结果:

复制代码

复制代码

ok,that’s it…

 

How to make a spring boot webservice

  • 从这个网址生成项目文件    https://start.spring.io/

对于webservice , 按下图选择 :

 

然后点击 Generate Project

  • 在 eclipse 导入项目后 , 新建 HelloController.java ,代码为:

代码用意为:  The class is flagged as a @RestController, meaning it’s ready for use by Spring MVC to handle web requests. @RequestMapping maps / to the index() method. When invoked from a browser or using curl on the command line, the method returns pure text. That’s because @RestController combines @Controller and @ResponseBody, two annotations that results in web requests returning data rather than a view.

 

注:这个Controller只实现了最简单对 “/” 的返回,是不带参数的request , 当用户调用时是这样的:

如果改成实际可用的带参数的request , 见下面这个例子

 

 

 

 

  • 接下来修改 项目自动生成的: FirstWebServiceApplication.java 文件

改为:

 

  • 接下来编译 :   到项目文件夹后, 在 command line type 如下命令:

  • 运行并调试 :

运行:  在 command line 模式到 F:\JavaProject\javaExercise\FirstWebService\build\libs 目录 , 执行

 

然后用 浏览器打 http://localhost:8080/

如果得到 Greetings from Spring Boot! 的输出 , 就说明完成 了。

来源自 https://spring.io/guides/gs/spring-boot/


接下来是如何让spring boot 能写log , 使用 log4j2 , 不用spring boot 自带的 logback

首先在 build.gradle  将 log back去除 ; 需要加上这一行:

并且在dependence加上

 

改之前的HelloController.java 为 :

 

在  src/main/resources  新建   log4j2-spring.xml 

内容为:

 

然后重新编译执行

日志部分来源与博客 : https://blog.scalyr.com/2018/07/started-quickly-spring-boot-logging/

 


如何将日志输出到文件

上一步是把日志输出到 console , 要写到文件就要改 log4j2-spring.xml ,

改为:

 

更详细的配置见: https://logging.apache.org/log4j/2.x/manual/appenders.html

 

 

How to use ZMQ

  • simple example :

server side :  http://zguide.zeromq.org/java:hwserver

client side :   http://zguide.zeromq.org/java:hwclient 

 

  • server side code

  • client code :

 

  • server pom.xml

 

How to search a certain word through whole project in eclipse

same problem from  stackoverflow is :

How to search in Eclipse through project for some word ? When I start CTRL-h I need to choose field, method or …., but I need to find all occurences in project no metter method, field or .. ! How to achieve that ?

 

Answer is :

 

CTRL + H -> File Search -> Containing Text ‘some word’
make sure the scope is set to workspace or enclosing projects. Choosing Selected Resources will narrow the result set to the folder or file you selected in the explorer.

Further if you know the file type that might contain the text you can use the File Name patterns with values like *.php, *.phtml, *.html

Jboss tutorial

  • Q&A
  1. what is J2EE    (Java 2 Platform, Enterprise Edition ) 

J2EE is a platform-independent, Java-centric environment from Sun for developing, building and deploying Web-based enterprise applications online. The J2EE platform consists of a set of services, APIs, and protocolsthat provide the functionality for developing multitiered, Web-based applications.

Some of the key features and services of J2EE:

  • At the client tier, J2EE supports pure HTML, as well as Java applets or applications. It relies on Java Server Pages and servletcode to create HTML or other formatted data for the client.
  • Enterprise JavaBeans (EJBs) provide another layer where the platform’s logic is stored. An EJB server provides functions such as threading, concurrency, security and memory management. These services are transparent to the author.
  • Java Database Connectivity (JDBC), which is the Java equivalent to ODBC, is the standard interface for Java databases.
  • The Java servlet API enhances consistency for developers without requiring a graphical user interface.

In simpler terms, J2EE is essentially a standard middleware architecture, proposed by Sun Microsystems for developing and deploying multitier, distributed, e-enabled, enterprise scale business applications. Applications written for J2EE standards enjoy certain inherent benefits such as portability, security, scalability, load-balancing, and reusability.

2)  How to install JBoss in eclipse 

download from this site :  http://jbossas.jboss.org/downloads 

in  eclipse , goto :  help —-> eclipse market ,  type “jboss” , select  “JBoss Tools 4.4.4.final” , and install it

after install , restart eclipse as require .

3) How to  build jboss server

 

 

 

 

 

How to use sql server jdbc in java application (for win 10)

  •  download  “Microsoft JDBC Driver 6.0 for SQL Server”  from this site :

https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774

 

  • 解压后,到这个目录          sqljdbc_6.0.8112.200_enu.tar\sqljdbc_6.0.8112.200_enu\sqljdbc_6.0\enu\jre8

用 command line 执行 :


这是因为maven没有sqljdbc的repository , 必须手动把 sqljdbc42.jar 加到本地的maven repository

此方法来源于 :

https://stackoverflow.com/questions/19537396/missing-artifact-com-microsoft-sqlserversqljdbc4jar4-0

执行成功的标志是: 可以在本地maven repository 看到 sqljdbc

 

  • 然后 ,在 maven 的pom.xml 文件加入dependency

 

  • 需要用到 sql server 时 ,加这句即可

 

  • 各种调用的例子, 包括直接执行sql语句,调用store procedure (带输入和输出参数) , 见这个网址, 例子比较全

https://programmaticponderings.com/2012/08/24/calling-sql-server-stored-procedures-with-java-using-jdbc

 

 


如果要持续的调用sql sp , 就不能每一次都新建connection , 要使用 connection pool  ,  选用 apache 的 dbcp 库 

 

  • 如何引用:  在 pom.xml 加

 

  • 在 package 新建 DataSource.java 文件

 

  • try to call a sp with one input and output parameter

 

from running , you can find when the second trying ,  this code

is much more faster  than the first time .

  •  apache dbcp 的例子见

https://www.javatips.net/blog/dbcp-connection-pooling-example

 

  • basicDataSource 是否已包含pool的特性 ?  已有, 详细解释见:

https://stackoverflow.com/questions/14467480/connection-pooling-with-apache-dbcp