如何在Android 9 Api 28中使用HttpsUrlConnection写入远程服务器上的.txt文件

如何在Android 9 Api 28中使用HttpsUrlConnection写入远程服务器上的.txt文件,android,Android,以下来自Android开发者网站的示例代码展示了如何从远程服务器使用HttpsUrlConnection读取.html网站或.txt文件,并在Android 9 Api28中完美运行,但是没有关于如何使用HttpsUrlConnection写入.txt文件的示例,我的代码在android 5,6,7,8 api 23,25,27中使用UrlConnection非常有效,但在android 9 api 28中不起作用,我找不到任何关于如何修复android 9 api 28的信息 // works

以下来自Android开发者网站的示例代码展示了如何从远程服务器使用HttpsUrlConnection读取.html网站或.txt文件,并在Android 9 Api28中完美运行,但是没有关于如何使用HttpsUrlConnection写入.txt文件的示例,我的代码在android 5,6,7,8 api 23,25,27中使用UrlConnection非常有效,但在android 9 api 28中不起作用,我找不到任何关于如何修复android 9 api 28的信息

// works perfect in android 9 api28
protected Void doInBackground(Void... params) {        
try{
URL url = new URL("https://somesite/test.txt");
res= downloadUrl(url);            
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {            
}
return null;
}
private String downloadUrl(URL url) throws IOException {
InputStream stream = null;
HttpsURLConnection  connection = null;
String result = null;
try {
connection = (HttpsURLConnection) url.openConnection();connection.setReadTimeout(3000);connection.setConnectTimeout(3000);connection.setRequestMethod("GET");connection.setDoInput(true);connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode != HttpsURLConnection.HTTP_OK) {
throw new IOException("HTTP error code: " + responseCode);
}
stream = connection.getInputStream();
if (stream != null) {
result = readStream(stream);
}
} finally {
if (stream != null) {
stream.close();
}
if (connection != null) {
connection.disconnect();
}
}
return result;
}
public String readStream(InputStream stream) throws IOException, UnsupportedEncodingException {
Reader reader = null;
int readSize;
reader = new InputStreamReader(stream, "cp1251");
char[] rawBuffer = new char[100000];
StringBuffer buffer = new StringBuffer();
while (((readSize = reader.read(rawBuffer)) != -1)) {
buffer.append(rawBuffer, 0, readSize);
}
return buffer.toString();
}
// code wich works perfect in android 5,6,7,8, but doesn't work in android 9 api 28
protected Void doInBackground(Void... params) {        
URLConnection connection;int timeout = 10000;   
URL url;
try {
url = new URL("ftp://username:password@somesite/test.txt");
connection = url.openConnection();            connection.setConnectTimeout(timeout);connection.setReadTimeout(timeout);connection.setDoInput(true);connection.setDoOutput(true);connection.connect();
BufferedWriter out=new BufferedWriter (new OutputStreamWriter(connection.getOutputStream(),"cp1251"));
out.write("some text");out.flush();out.close();
} catch (IOException e) {
}
return null;
}

禁止来自Android 9的明文通信。您需要手动允许它将下面的代码添加到清单中

android:networkSecurityConfig="@xml/network_security_config"
另外,创建xml/network\u security\u config.xml文件并添加以下代码以允许明文通信:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>


欢迎来到StackOverflow!“我的代码使用UrlConnection在android 5,6,7,8 api 23,25,27中运行完美,但在android 9 api 28中不起作用。”-请澄清“不起作用”的含义。这段代码现在有什么问题?这意味着它对非SDK接口有限制。一般来说,应用程序应该只使用SDK中正式记录的类部分。特别是,这意味着您不应该计划在通过反射等语义与类交互时访问SDK中未列出的方法或字段。这就是为什么我上面代码的最后一部分在android9 Api28中不起作用的原因,它是一种去擦洗的方法,没有关于如何在远程服务器上写入文件的示例,只是关于如何像我在上面代码的第一部分中写的那样读取文件。您可以在这里和这里阅读关于如何使用我在上面代码的第一部分中写的HttpsUrlConnection读取文件的示例。谢谢你的帮助。