JSONException 自 Android Api 28(字符串结果 =“”)

JSONException since Android Api 28 (String result = "")

已升级到 Android Api 28。现在我收到一个 curios JSON 异常。实际上它不清楚我如何解决这个问题。我知道为什么会这样,但我现在不知道如何解决它。

这部分应该产生错误 "String result = "";"在这部分的第 74 行 "JSONObject json = new JSONObject(result);" JSON 崩溃了

GetJsonResults

import java.io.InputStreamReader;

public class GetJsonResults extends AsyncTask<String, Void, String> {
    private final static String LOG_TAG = GetJsonResults.class.getSimpleName();

    private static String GET(String url) {
        InputStream inputStream;
        String result = "";
        try {

            // create HttpClient
            HttpClient httpclient = new DefaultHttpClient();

            // make GET request to the given URL
            HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

            // receive response as inputStream
            inputStream = httpResponse.getEntity().getContent();

            // convert inputstream to string
            if (inputStream != null)
                result = convertInputStreamToString(inputStream);
            else
                result = "Did not work!";

        } catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());
        }

        return result;
    }

    private static String convertInputStreamToString(InputStream inputStream) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        String result = "";
        while ((line = bufferedReader.readLine()) != null)
            result += line;

        inputStream.close();
        return result;

    }

    @Override
    protected String doInBackground(String... urls) {

        return GET(urls[0]);
    }

    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {
        //Toast.makeText(getBaseContext(), "Daten erfolgreich empfangen!", Toast.LENGTH_LONG).show();
        try {
            JSONObject json = new JSONObject(result);
2019-07-11 16:59:02.919 10072-10072/com.radio.xxxxxxSRAPPID D/SplashActivity: Get JSON Results
2019-07-11 16:59:04.312 10072-10072/com.radio.xxxxxxSRAPPID D/GetJsonResults: There was an JSONException
2019-07-11 16:59:04.312 10072-10072/com.radio.xxxxxxSRAPPID W/System.err: org.json.JSONException: Value http of type java.lang.String cannot be converted to JSONObject
2019-07-11 16:59:04.312 10072-10072/com.radio.xxxxxxSRAPPID W/System.err:     at org.json.JSON.typeMismatch(JSON.java:112)
2019-07-11 16:59:04.312 10072-10072/com.radio.xxxxxxSRAPPID W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:163)
2019-07-11 16:59:04.312 10072-10072/com.radio.xxxxxxSRAPPID W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:176)
2019-07-11 16:59:04.312 10072-10072/com.radio.xxxxxxSRAPPID W/System.err:     at com.radio.xxxxxx.task.GetJsonResults.onPostExecute(GetJsonResults.java:74)
2019-07-11 16:59:04.312 10072-10072/com.radio.xxxxxxSRAPPID W/System.err:     at com.radio.xxxxxx.task.GetJsonResults.onPostExecute(GetJsonResults.java:21)

发生这种情况是因为在 API 28 中默认禁用 http 调用,您可以使用以下解决方法在 API 28 中启用 http 调用,

在你的AndroidManifest.xml中,放

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
    <application android:networkSecurityConfig="@xml/network_security_config">




    </application>
</manifest>

在资源目录中创建 network_security_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>

在应用中仅使用 android:usesCleartextTraffic="true" class

这将允许在您的应用中进行 http 调用,有关详细信息,请查看,

这里还有文档,

https://developer.android.com/about/versions/pie/android-9.0-changes-28