Java back end deployment and communication with Android

The cold 2021-01-21 13:44:31
java end deployment communication android

1 summary

This article lists some Android+ Back end Java signal communication / Deployment issues and considerations , Coverage issues include, but are not limited to, security groups 、 database 、 Path, etc , If your readers Android The client can't access it properly Java Back end , I hope the solution here can help you .

2 classification

There are three types of problems :

  • Java End problem
  • Android End problem
  • Other miscellaneous issues

Let's take a look first Java Problems that may arise at the end .

3 Java End

Include :

  • database
  • Security group / A firewall
  • 404

3.1 database

3.1.1 drive

Be careful MySQL5.7 And MySQL8 It's not the same when registering drivers ,MySQL5.7 yes :


MySQL8 Above is :


In addition, pay attention to JAR The package version is correct .

3.1.2 user name / password / jurisdiction

First of all, you need to make sure that the user name and password for accessing the database in the configuration file are correct , Otherwise, there may be various null pointer errors , In addition, you need to ensure that the user has the corresponding permissions on the target database or table .

3.1.3 Spring Boot Encryption configuration in

stay application.yaml/application.yml/ Configure the corresponding user name and password in , Generally, plaintext is OK , This is for the use of Jasypt Encryption library (Jasypt Can encrypt Spring Boot Configuration file in , You can refer to here ) Speaking of .

Because use Jasypt The post configuration file is ciphertext , If the encrypted password cannot be read correctly at this time, an error will be reported directly , So we can aim at Jasypt Encryption method of ( Simple password encryption 、 Non plaintext password 、 Asymmetric encryption, etc ) Check the configuration file for errors , Or whether the corresponding environment variables are configured .

3.2 Security group / A firewall

3.2.1 Without a firewall

Check the firewall can be used :

systemctl status firewalld
# or
systemctl status iptables

If it's not turned on , Then just deal with the security group .

Generally, server manufacturers provide security groups , Open the corresponding port and IP that will do :

 Insert picture description here

3.2.2 With firewall firewalld

With firewall , You can check which services and rules are opened first :

firewall-cmd --list-services
firewall-cmd --list-ports

And add corresponding rules as needed , For example, opening up 8080 Port to all IP

# Add rules
firewall-cmd --add-port=8080/tcp --permanent
# Reload
firewall-cmd --reload

If you remove the rules, you can use :

# Remove rule
firewalld-cmd --remove-port=8080/tcp --permanent
# It also needs to be reloaded
firewall-cmd --reload iptables

Check the status :

systemctl status iptables

Add rules , In order to open 8080 Port as an example , modify /etc/sysconfig/iptables

vim /etc/sysconfig/iptables

And add :

-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT

Restart service takes effect :

systemctl restart iptables

3.3 404

appear 404 The main reason is that there is a problem with the path , There are two situations to discuss here :

  • Use JavaWeb Servlet form
  • Spring Boot

3.3.1 Servlet

Use Servlet Words , Local operation requires configuration Tomcat Or others Web The server , So if the IDEA Middle configuration Tomcat Words , Need to be in Application context Here is the configuration path :

 Insert picture description here

Of course, it's not impossible not to configure , By default, there is a path to the project name , It's usually hard to remember for a long time , It is suggested to change it to a path easy to remember .

If it's deployed on the server Tomcat, You need to pay attention to the packaged WAR The name of the bag , For example, after packing, it is demo.war, place webapps after , start-up Tomcat Will be automatically unpacked as webapps/demo, At this time, the path needs to correspond to demo Visit , such as :

3.3.2 Spring Boot

Use SpringBoot Basically, there will be no path error , Thanks to the SpringBoot Powerful default configuration , Generally, write... On the corresponding method :


Then you can access it .

4 Android End

The investigation is over Java Let's take a look at the problem Android End of the problem , Include :

  • HTTP
  • Threads
  • AVD
  • jurisdiction
  • IP

4.1 HTTP

from Android P Start with the default requirement to use HTTPS, By default HTTP The following exceptions will occur :

W/System.err: Cleartext HTTP traffic to **** not permitted CLEARTEXT communication ** not permitted by network security policy

There are two solutions :

  • Use HTTPS
  • modify AndroidManifest.xml To allow HTTP Connect

Using the latter method , stay AndroidManifest.xml Add the following statement :


4.2 Threads

Android 4.0 At first, it is required that the network cannot be operated in the main thread , All networking operations require a new thread , So it's easier to deal with , Just put the network operation in the new thread , Examples are as follows :

FutureTask<String> task = new FutureTask<>(new NetworkThread());
Thread thread = new Thread(task);
String result = task.get();
public class NetworkThread implements Callable<String> {
public String call(){
// Network operating
return result;

utilize FutureTask, Add an implementation Callable<T>T The type of is the return type of the thread operation , For example, here is the return String) Class into which ,start() After thread ,get() Get the results .

4.3 jurisdiction

stay AndroidManifest.xml Add network permissions :

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

4.4 AVD

The error log is as follows :

 Insert picture description here

StackOverflow The solution on the Internet is uninstall APP And restart AVD

 Insert picture description here

This method is tested by the author, but it is not guaranteed to be 100% effective , I guess this is AVD The problem of .

4.5 IP

about Android Come on , Local tests cannot be used localhost or, That is, you can't use :


It's using the intranet IP, such as 192.168 At the beginning , or 10.253 At the beginning , have access to

ip addr
# or

see .

5 Other questions

5.1 HTTP signal communication

This is mainly for Java Web Speaking of , Because you need to manually configure all kinds of HTTP To configure , such as Content-Type And coding and so on , So if the settings are wrong or Android End and Java End inconsistency leads to Android The client cannot send the correct parameter or Java The client cannot get the correct parameters .

It's in the original HttpURLConnection For example , stay Android End , The settings for sending requests are as follows :

String url = "";
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
String data = "name="+ URLEncoder.encode(name, StandardCharsets.UTF_8.toString())+"&password="+URLEncoder.encode(password,StandardCharsets.UTF_8.toString());
byte [] bytes = new byte[1024];
int len = connection.getInputStream().read(bytes);
return new String(bytes,0,len,StandardCharsets.UTF_8);

After opening the connection , Through splicing and URLEncoder.encode() Get the data to be sent in the same way , Get the output stream and write it to .

stay Java The corresponding code is as follows :

public class XXXServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
String name = request.getParameter("name");
String password = request.getParameter("password");

First set up the encoding UTF8, Then get the parameters , You need to set the return type before returning , Here is application/json.

5.2 JSON serialize

JSON There are many serialization frameworks , Common are :

  • Gson
  • Jackson
  • fastjson

Here I use it when testing Jackson Simultaneous coordination Lombok, Note the need to add @Setter as well as @Getter, Otherwise / Deserialization doesn't work .

In addition, there needs to be a default construction method , When using a serialized response body , Examples are as follows :

public class ResponseBody{
private Object data;
private int code;

If there are other construction methods, please add @NoArgsConsturctor, Generate a parameterless construction method .

stay Kotlin You need to specify a default value for , such as :

class ResponseBody(var code:Int,var data:Any)

It doesn't work like this , Just add the default value :

class ResponseBody(var code:Int,var data:Any)

6 Last

I hope readers can see the final solution to the problem , In addition, if there are other problems or other solutions to the problem, please comment and add .

本文为[The cold]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云