jlcool / aliossflutter

阿里云oss flutter
Apache License 2.0
76 stars 47 forks source link

android.os.NetworkOnMainThreadException #12

Open liuchuancong opened 5 years ago

liuchuancong commented 5 years ago
android.os.NetworkOnMainThreadException
W/System.err(13460):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448)
W/System.err(13460):    at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:102)
W/System.err(13460):    at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90)
W/System.err(13460):    at java.net.InetAddress.getByName(InetAddress.java:743)
W/System.err(13460):    at com.alibaba.sdk.android.oss.common.utils.OSSUtils.isValidateIP(OSSUtils.java:781)
W/System.err(13460):    at com.alibaba.sdk.android.oss.OSSImpl.<init>(OSSImpl.java:117)
W/System.err(13460):    at com.alibaba.sdk.android.oss.OSSClient.<init>(OSSClient.java:100)
W/System.err(13460):    at com.alibaba.sdk.android.oss.OSSClient.<init>(OSSClient.java:88)
W/System.err(13460):    at com.jlcool.aliossflutter.AliossflutterPlugin.secretInit(AliossflutterPlugin.java:139)
W/System.err(13460):    at com.jlcool.aliossflutter.AliossflutterPlugin.onMethodCall(AliossflutterPlugin.java:103)
W/System.err(13460):    at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:222)
W/System.err(13460):    at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:96)
W/System.err(13460):    at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:643)
W/System.err(13460):    at android.os.MessageQueue.nativePollOnce(Native Method)
W/System.err(13460):    at android.os.MessageQueue.next(MessageQueue.java:325)
W/System.err(13460):    at android.os.Looper.loop(Looper.java:142)
W/System.err(13460):    at android.app.ActivityThread.main(ActivityThread.java:6938)
W/System.err(13460):    at java.lang.reflect.Method.invoke(Native Method)
W/System.err(13460):    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
W/System.err(13460):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
   private void secretInit() {
        endpoint = _call.argument("endpoint");
        final String accessKeyId = _call.argument("accessKeyId");
        final String accessKeySecret = _call.argument("accessKeySecret");
        final String _id = _call.argument("id");
        final String securityToken = _call.argument("securityToken");
        final Map<String, String> m1 = new HashMap();
        m1.put("result", "success");
        m1.put("id", _id);
        final OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider(accessKeyId, accessKeySecret, securityToken);
        oss = new OSSClient(registrar.context(), endpoint, credentialProvider);
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                channel.invokeMethod("onInit", m1);
            }
        });

    }
liuchuancong commented 5 years ago

虽然不影响使用,但是我改了之后但是还是这个问题。

jlcool commented 5 years ago

我也不知道如何修改

liuchuancong commented 5 years ago

好吧

liuchuancong commented 5 years ago
   private void secretInit() {
        new AsyncTask<MethodCall,Void,Void>(){
            @Override
            protected Void doInBackground(MethodCall... methodCalls) {
                MethodCall _call = methodCalls[0];
                final String accessKeyId = _call.argument("accessKeyId");
                final String accessKeySecret = _call.argument("accessKeySecret");
                final String _id = _call.argument("id");
                final String securityToken = _call.argument("securityToken");
                final OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider(accessKeyId, accessKeySecret, securityToken);
                oss = new OSSClient(registrar.context(), (String) _call.argument("endpoint"), credentialProvider);
                return null;
            }
        }.execute(_call);

        final String _id = _call.argument("id");
        final Map<String, String> m1 = new HashMap();
        m1.put("result", "success");
        m1.put("id", _id);
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                channel.invokeMethod("onInit", m1);
            }
        });
    }

你值得拥有!

jlcool commented 5 years ago

你可以试试pull requests 把你代码提交下

liuchuancong commented 5 years ago

自签名部分我修改源码了不适合,你可以根据此方法修改oninit() 顺便一提

alioss.responseFromUpload.listen((UploadResponse data) {
        if (data.success) {
          callback('http://$endPoint/$bucketName/${data.key}');
        } else {
          callback('0');
        }
      });

这个方法多次调用会将 之前的路径也返回 多次调用有时候会瞬间 返回好几条路径