StrictMode java.lang.Throwable:检测到未标记的套接字

时间:2017-12-09 00:18:44

标签: android android-trafficstats android-strictmode

启用StrictMode后,我才开始收到此异常:

  

java.lang.Throwable:检测到未标记的套接字;使用TrafficStats.setThreadSocketTag()来跟踪所有网络使用情况

3 个答案:

答案 0 :(得分:3)

我的解决方案是致电初始化以下内容:

CREATE TABLE CLIENTE(
RUT VARCHAR2(10) CONSTRAINT CLIENTE_PK PRIMARY KEY,
NOMBRE VARCHAR2(20) CONSTRAINT NOMBRE_CLIENTE NOT NULL,
APELLIDOP VARCHAR2(30) CONSTRAINT APELLIDOP_CLIENTE NOT NULL,
APELLIDOM VARCHAR2(30) CONSTRAINT APPELIDOM_CLIENTE NOT NULL,
DIRECCION VARCHAR2(100) CONSTRAINT DIRECCION_CLIENTE NOT NULL,
TELEFONO NUMBER(8) CONSTRAINT TELEFONO_CLIENTE NOT NULL,
EMAIL VARCHAR2(255) CONSTRAINT EMAIL_CLIENTE NOT NULL,
FECHA_NACIMIENTO DATE CONSTRAINT FECHA_NACIMIENTO_CLIENTE NOT NULL,
SEXO CHAR(1) CONSTRAINT SEXO_CLIENTE NOT NULL,
COD_CIUDAD NUMBER(3) CONSTRAINT CLIENTE_CIUDAD_FK FOREIGN KEY (COD_CIUDAD) 
REFERENCES CIUDAD (COD_CIUDAD)
);

答案 1 :(得分:2)

有两种方法可以处理此异常。首先,您必须检查堆栈跟踪,并确保它是您的代码报告违规情况。例如,查看以下跟踪。

D/StrictMode: StrictMode policy violation: android.os.strictmode.UntaggedSocketViolation: Untagged socket detected; use TrafficStats.setThreadSocketTag() to track all network usage
    at android.os.StrictMode.onUntaggedSocket(StrictMode.java:2124)
    at com.android.server.NetworkManagementSocketTagger.tag(NetworkManagementSocketTagger.java:82)
    at libcore.io.BlockGuardOs.tagSocket(BlockGuardOs.java:52)
    at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:372)
    at libcore.io.ForwardingOs.socket(ForwardingOs.java:217)
    at libcore.io.IoBridge.socket(IoBridge.java:658)
    at java.net.PlainSocketImpl.socketCreate(PlainSocketImpl.java:128)
    at java.net.AbstractPlainSocketImpl.create(AbstractPlainSocketImpl.java:128)
    at java.net.ServerSocket.createImpl(ServerSocket.java:306)
    at java.net.ServerSocket.getImpl(ServerSocket.java:259)
    at java.net.ServerSocket.bind(ServerSocket.java:377)
    at java.net.ServerSocket.<init>(ServerSocket.java:237)
    at java.net.ServerSocket.<init>(ServerSocket.java:128)
    at com.java42.android03.binder.peer.messaging.J42PM_ServerConnection.run(J42PM_ServerConnection.java:52)
    at com.java42.base.components.utility.impl.Utility_Thread$1.run(Utility_Thread.java:137)
    at java.lang.Thread.run(Thread.java:919)
    at com.java42.utility.base.J42Thread.run(J42Thread.java:38)

违反的应用调用为:

J42PM_ServerConnection.run(J42PM_ServerConnection.java:52)

接下来,看看该代码:

public void run() {
    try (ServerSocket serverSocket = new ServerSocket(port)) {
        this.serverSocket = serverSocket;
        CallerId.identifyBasic("J42PSC0179D: listening Inbound....");
        while (!Thread.currentThread().isInterrupted()) {
        ...

由于这是服务器端循环,并且线程ID在服务器进程的生命周期内不会更改,因此可以通过使用当前线程ID向TrafficStats.setThreadStatsTag()添加调用来解决冲突。

public void run() {
    TrafficStats.setThreadStatsTag((int) Thread.currentThread().getId()); // <---
    try (ServerSocket serverSocket = new ServerSocket(port)) {
        this.serverSocket = serverSocket;
        CallerId.identifyBasic("J42PSC0179D: listening Inbound....");
        while (!Thread.currentThread().isInterrupted()) {
        ...

让我们看看另一条痕迹:

D/StrictMode: StrictMode policy violation: android.os.strictmode.UntaggedSocketViolation: Untagged socket detected; use TrafficStats.setThreadSocketTag() to track all network usage
    at android.os.StrictMode.onUntaggedSocket(StrictMode.java:2124)
    at com.android.server.NetworkManagementSocketTagger.tag(NetworkManagementSocketTagger.java:82)
    at libcore.io.BlockGuardOs.tagSocket(BlockGuardOs.java:52)
    at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:372)
    at libcore.io.ForwardingOs.socket(ForwardingOs.java:217)
    at libcore.io.IoBridge.socket(IoBridge.java:658)
    at java.net.PlainSocketImpl.socketCreate(PlainSocketImpl.java:128)
    at java.net.AbstractPlainSocketImpl.create(AbstractPlainSocketImpl.java:128)
    at java.net.Socket.createImpl(Socket.java:489)
    at java.net.Socket.<init>(Socket.java:446)
    at java.net.Socket.<init>(Socket.java:218)
    at com.java42.android03.binder.peer.messaging.J42PM_ClientConnection.run(J42PM_ClientConnection.java:54)
    at com.java42.android03.binder.peer.messaging.J42PM_Client$2.run(J42PM_Client.java:86)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:919)

与第一个示例一样,您确定违反的应用程序调用:

J42PM_ClientConnection.run(J42PM_ClientConnection.java:54)

接下来,看看该代码:

InetSocketAddress socketAddress = serviceResolver.getSocketAddress();
    connection = new J42PM_ClientConnection(socketAddress, responseHandler);
    executorService.execute(new Runnable() {
        @Override
        public void run() {
        connection.run();
        ...

请注意,此代码使用执行程序服务来运行客户端连接。在创建和销毁连接时,线程ID将在该过程的生命周期内更改。我不确定这是否会使流量报告产生偏差,但是我认为最好在此时使用恒定值来跟踪使用情况。选择一些唯一的号码。使用文件的行号很容易。

    InetSocketAddress socketAddress = serviceResolver.getSocketAddress();
    connection = new J42PM_ClientConnection(socketAddress, responseHandler);
    executorService.execute(new Runnable() {
        @Override
        public void run() {
        TrafficStats.setThreadStatsTag(42); // <--
        connection.run();
        ...

最后一条评论,如果您在堆栈跟踪中没有看到对代码的调用,则您无能为力阻止这种违反。问题出在另一个图书馆,除非您可以与图书馆所有者联系,否则只需忽略违规。

答案 2 :(得分:-1)

if (BuildConfig.DEBUG) {
    StrictMode.setThreadPolicy(
        StrictMode.ThreadPolicy.Builder()
            .detectDiskReads()
            .detectDiskWrites()
            .detectNetwork() // or .detectAll() for all detectable problems
            .detectAll() //                    .penaltyDeath()
            .penaltyLog()
            .build()
    )
    StrictMode.setVmPolicy(
        VmPolicy.Builder()
            .detectLeakedSqlLiteObjects()
            .detectLeakedClosableObjects()
            .detectAll()
            .penaltyLog() //                    .penaltyDeath()
            .build()
    )
}