将代码放在app目录中的build.gradle的最外层,每次编译时会自动从网络获取数据保存至你的本地路径
def response = getConnection('请求地址http....')
replaceFileText(response.toString(), '你要替换文件内容的目录')
def getConnection(String url){
//发送Http请求
def connect = new URL(url).openConnection()
connect.setRequestMethod('GET')
connect.connect()
def response = connect.content.text
JSONObject jsonObject = new JSONObject(response)
return jsonObject
}
def replaceFileText(String object, String path) {
def dest = new File(path)
if (!dest.exists()) {
dest.createNewFile()
}
dest.withPrintWriter { printWriter ->
printWriter.print(object)
}
}