我正在嘗試在Android 9,API 28中使用HttpURLConnection。它適用于Android 8.0.0,API 26,但不適用于API 28。 JSONObject jsonObject = new JSONObject(); jsonObject.accumulate("name", name); String json = jsonObject.toString(); URL url = new URL("http://website_link"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setDoOutput(true); urlConnection.setRequestMethod("POST"); urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); OutputStream os = urlConnection.getOutputStream(); os.write(json.getBytes("UTF-8")); os.close(); InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream()); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("utf-8")), 8); String line; json = ""; while ((line = reader.readLine()) != null) { json += line; } inputStream.close(); jsonObject = new JSONObject(json); inputStream.close(); urlConnection.disconnect(); ...我試圖使用以查看哪些代碼未執(zhí)行,我看到它停止在Log.dOutputStream os = urlConnection.getOutputStream();你知道問題出在哪里嗎?
2 回答

慕尼黑8549860
TA貢獻(xiàn)1818條經(jīng)驗 獲得超11個贊
試試這個添加Manifest.xml
cleartextTrafficPermitted="true"
它看起來像這樣 如何允許在Android(9)Pie中的所有網(wǎng)絡(luò)連接類型HTTP和HTTPS?

汪汪一只貓
TA貢獻(xiàn)1898條經(jīng)驗 獲得超8個贊
這是因為Apache HTTP客戶端的折舊,所以在標(biāo)簽中添加下面一行。
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
添加回答
舉報
0/150
提交
取消