【问题标题】:despite of adding internet permission i'am getting "No Network Security Config specified, using platform default" in my logcat尽管添加了 Internet 权限,但我在我的 logcat 中得到“未指定网络安全配置,使用平台默认值”
【发布时间】:2017-07-13 08:59:28
【问题描述】:

我已将 Internet 权限代码添加到我的清单中,但我仍然在我的 logcat 中收到 No Network Security Config specified, using platform default。通过我的代码,我试图在按下按钮时从 url 获取 html 代码,但不幸的是我的应用程序崩溃了。 我在这里粘贴我的清单代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rekhahindwar.linksaver" >
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".UrlFetcher" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

我的 java 类在这里-

public class UrlFetcher extends AppCompatActivity implements View.OnClickListener {

EditText url;
Button fetch;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initialize();
}

private void initialize() {
    url = (EditText) findViewById(R.id.url);
    fetch = (Button) findViewById(R.id.fetch);
    fetch.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    String baseurl = url.getText().toString();
    Document doc = null;
    try {
        doc = Jsoup.connect(baseurl).get();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

【问题讨论】:

标签: java android android-manifest


【解决方案1】:

创建一个单独的 asynctask 类,如下所示。 并使用它从活动中调用您的异步类。

new FetchURLAsyncTask().execute();



public class FetchURLAsyncTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        StringBuffer buffer = new StringBuffer();
        try {
            Document doc = Jsoup.connect("https://www.google.co.in").get();
            String title = doc.title();
            System.out.println("Url !!!! " + title);
        }
        catch(Throwable t) {
            t.printStackTrace();
        }
        return null;
    }
}

【讨论】:

    【解决方案2】:

    也许你的防火墙阻止了连接,禁用它然后再试一次,我也认为你可能需要将它添加到你的清单 xml 文件中,因为你正在使用 jsoup 来解析一些 html

        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    

    【讨论】:

    • 对我不起作用....我禁用了我的防火墙并添加了您提到的权限,但仍然遇到同样的问题.... :(
    • 我推荐的权限只是为了将来使用而不是解决您的问题。你能显示你的错误日志吗?
    • D/NetworkSecurityConfig:未指定网络安全配置,使用平台默认值 07-13 15:01:55.421 2543-2550/com.example.rekhahindwar.linksaver W/art:暂停所有线程占用:19.453 ms 07-13 15:01:55.422 2543-2543/com.example.rekhahindwar.linksaver D/AndroidRuntime: 关闭 VM--------- 崩溃开始 07-13 15:01:55.491 2543-2543 /com.example.rekhahindwar.linksaver E/AndroidRuntime:致命异常:主进程:com.example.rekhahindwar.linksaver,PID:2543 android.os.NetworkOnMainThreadException
    • 嘿,请查看我的回答评论以获得清晰的 logcat 视图
    • 首先消息没有问题 D/NetworkSecurityConfig: No Network Security Config specified, using platform default because D/表示这是一条调试消息。我想你没有。您应该在 OnCreate() 方法中调用您的方法。这是我的想法,我可能错了
    猜你喜欢
    • 2017-09-15
    • 2017-02-17
    • 2020-03-28
    • 2019-04-28
    • 2019-05-27
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    相关资源
    最近更新 更多