If you ever used Shadowsocks X-NG on Mac, (mine is version 1.7.1 (1)) and once use the Global Mode, it will automatically add the following command:
~/.gradle/gradle.propertries:
#Mon Nov 18 03:59:09 CST 2019
systemProp.http.proxyHost=127.0.0.1
systemProp.https.proxyPort=1086
systemProp.https.proxyHost=127.0.0.1
systemProp.http.proxyPort=1086

When you close Shadowsocks X-NG, Android studio still use the proxy, and that is why it shows
Gradle sync failed: Connection refused
Please comment those lines out, and rebuild the project again.
## For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
#
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx1024m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
#
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
#Mon Nov 18 03:59:09 CST 2019
#systemProp.http.proxyHost=127.0.0.1
#systemProp.https.proxyPort=1086
#systemProp.https.proxyHost=127.0.0.1
#systemProp.http.proxyPort=1086