首页 > 解决方案 > http url 连接设置中的异常

问题描述

我在 connection.connect() 上遇到异常。我已附加了我的 logcat 的输出。提到了运行时异常。我尝试在线程上运行此服务,但仍然发生异常。不知道是什么原因造成的。请告知为什么会发生此异常以及如何修复它。

代码:

public class TimeZoneDownloadingService extends Service {

    private IBinder mBinder  = new myBinder();

    @Override
    public void onCreate() {
        super.onCreate();
        showMessage("Service Started");


    }
    private void showMessage(String message)
    {
        Toast toast = Toast.makeText(this, message, Toast.LENGTH_LONG);
        toast.show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        showMessage("StartCommand");
        //return super.onStartCommand(intent, flags, startId);
        Thread thread = new Thread(new Runnable(){
            public void run(){
                StartDownloadTimezone();
            }
        });
        //thread.start();

        thread.start();
        return START_NOT_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        showMessage(" on Bind");
        return mBinder;
    }


    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        stopSelf();
    }
    public class myBinder extends Binder {

        TimeZoneDownloadingService getService()
        {
            return TimeZoneDownloadingService.this;
        }

    }
    public void StartDownloadTimezone()  {
        HttpURLConnection connection = null;
        InputStream  stream = null;
        try{
            URL url = new URL("https://api.timezonedb.com/v2.1/list-time-zone?key=P8B5V3KL22GA&format=json&country=US");
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.connect();

            stream = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
            StringBuffer buffer = new StringBuffer();

            String line = " ";
            while((line = reader.readLine()) != null)
            {
                buffer.append(line);
            }
            parse(buffer);
            showMessage("URL connection successful");


        }catch(Exception exception)
        {
            exception.printStackTrace();
        }

    }
    private void parse(StringBuffer buff)
    {
        showMessage("Parsing");
    }


}

日志猫:

06-12 21:40:42.398 16115-16115/? E/Zygote: v2
06-12 21:40:42.398 16115-16115/? I/libpersona: KNOX_SDCARD checking this for 10080
06-12 21:40:42.398 16115-16115/? I/libpersona: KNOX_SDCARD not a persona
06-12 21:40:42.398 16115-16115/? I/SELinux: Function: selinux_compare_spd_ram, SPD-policy is existed. and_ver=SEPF_SM-J500H_5.1.1 ver=48
06-12 21:40:42.398 16115-16115/? I/SELinux: Function: selinux_compare_spd_ram , priority [1] , priority version is VE=SEPF_SM-J500H_5.1.1_0048
06-12 21:40:42.398 16115-16115/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
06-12 21:40:42.398 16115-16115/? I/art: Late-enabling -Xcheck:jni
06-12 21:40:42.418 16115-16115/? D/TimaKeyStoreProvider: in addTimaSignatureService
06-12 21:40:42.418 16115-16115/? D/TimaKeyStoreProvider: TimaSignature is unavailable
06-12 21:40:42.418 16115-16115/? D/ActivityThread: Added TimaKesytore provider
06-12 21:40:42.448 16115-16115/com.example.worldclocktest W/ResourcesManager: getTopLevelResources: com.example.worldclocktest for user  0
06-12 21:40:42.458 16115-16115/com.example.worldclocktest I/InjectionManager: Inside getClassLibPath + mLibMap{0=, 1=}
06-12 21:40:42.458 16115-16115/com.example.worldclocktest I/InjectionManager: Inside getClassLibPath caller 
06-12 21:40:42.458 16115-16115/com.example.worldclocktest D/InjectionManager: InjectionManager
06-12 21:40:42.458 16115-16115/com.example.worldclocktest D/InjectionManager: fillFeatureStoreMap com.example.worldclocktest
06-12 21:40:42.468 16115-16115/com.example.worldclocktest I/InjectionManager: Constructor com.example.worldclocktest, Feature store :{}
06-12 21:40:42.468 16115-16115/com.example.worldclocktest I/InjectionManager: featureStore :{}
06-12 21:40:42.478 16115-16115/com.example.worldclocktest W/ResourcesManager: getTopLevelResources: com.example.worldclocktest for user  0
06-12 21:40:42.478 16115-16115/com.example.worldclocktest W/ResourcesManager: getTopLevelResources: com.example.worldclocktest for user  0
06-12 21:40:42.538 16115-16115/com.example.worldclocktest W/ResourcesManager: getTopLevelResources: com.example.worldclocktest for user  0
06-12 21:40:42.538 16115-16115/com.example.worldclocktest W/ResourcesManager: getTopLevelResources: com.example.worldclocktest for user  0
06-12 21:40:42.538 16115-16115/com.example.worldclocktest D/DisplayManager: DisplayManager()
06-12 21:40:42.548 16115-16115/com.example.worldclocktest W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-12 21:40:42.578 16115-16115/com.example.worldclocktest D/PhoneWindow: *FMB* installDecor mIsFloating : false
06-12 21:40:42.578 16115-16115/com.example.worldclocktest D/PhoneWindow: *FMB* installDecor flags : -2139029248
06-12 21:40:42.608 16115-16115/com.example.worldclocktest I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
06-12 21:40:42.608 16115-16115/com.example.worldclocktest I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
06-12 21:40:42.868 16115-16115/com.example.worldclocktest D/Activity: performCreate Call Injection manager
06-12 21:40:42.868 16115-16115/com.example.worldclocktest I/InjectionManager: dispatchOnViewCreated > Target : com.example.worldclocktest.MainActivity isFragment :false
06-12 21:40:42.908 16115-16131/com.example.worldclocktest D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-12 21:40:42.938 16115-16132/com.example.worldclocktest I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
06-12 21:40:42.938 16115-16132/com.example.worldclocktest I/System.out: (HTTPLog)-Static: isShipBuild true
06-12 21:40:42.938 16115-16132/com.example.worldclocktest I/System.out: (HTTPLog)-Thread-6283-58592290: SmartBonding Enabling is false, SHIP_BUILD is true, log to file is false, DBG is false
06-12 21:40:42.938 16115-16132/com.example.worldclocktest I/System.out: (HTTPLog)-Thread-6283-58592290: SMARTBONDING_FEATURE_ENABLED is false
06-12 21:40:42.938 16115-16132/com.example.worldclocktest I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
06-12 21:40:42.948 16115-16115/com.example.worldclocktest D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
06-12 21:40:42.948 16115-16115/com.example.worldclocktest D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
06-12 21:40:42.978 16115-16131/com.example.worldclocktest I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BR.1.1.3_RB1.05.01.00.032.018_msm8916_32_refs/tags/AU_LINUX_ANDROID_LA.BR.1.1.3_RB1.05.01.00.032.018__release_AU (I856e09677e)
    OpenGL ES Shader Compiler Version: E031.25.03.04
    Build Date: 05/03/15 Sun
    Local Branch: 
    Remote Branch: refs/tags/AU_LINUX_ANDROID_LA.BR.1.1.3_RB1.05.01.00.032.018
    Local Patches: NONE
    Reconstruct Branch: NOTHING
06-12 21:40:42.978 16115-16131/com.example.worldclocktest I/OpenGLRenderer: Initialized EGL, version 1.4
06-12 21:40:42.998 16115-16131/com.example.worldclocktest D/OpenGLRenderer: Enabling debug mode 0
06-12 21:40:43.008 16115-16115/com.example.worldclocktest E/RecyclerView: No adapter attached; skipping layout
06-12 21:40:43.048 16115-16115/com.example.worldclocktest I/InjectionManager: dispatchCreateOptionsMenu :com.example.worldclocktest.MainActivity
06-12 21:40:43.048 16115-16115/com.example.worldclocktest I/InjectionManager: dispatchPrepareOptionsMenu :com.example.worldclocktest.MainActivity
06-12 21:40:43.118 16115-16115/com.example.worldclocktest E/RecyclerView: No adapter attached; skipping layout
06-12 21:40:43.128 16115-16115/com.example.worldclocktest I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@329f0535 time:88098473
06-12 21:40:44.498 16115-16132/com.example.worldclocktest W/System.err: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
06-12 21:40:44.498 16115-16132/com.example.worldclocktest W/System.err:     at android.os.Handler.<init>(Handler.java:200)
06-12 21:40:44.498 16115-16132/com.example.worldclocktest W/System.err:     at android.os.Handler.<init>(Handler.java:114)
06-12 21:40:44.498 16115-16132/com.example.worldclocktest W/System.err:     at android.widget.Toast$TN.<init>(Toast.java:569)
06-12 21:40:44.498 16115-16132/com.example.worldclocktest W/System.err:     at android.widget.Toast.<init>(Toast.java:129)
06-12 21:40:44.498 16115-16132/com.example.worldclocktest W/System.err:     at android.widget.Toast.makeText(Toast.java:427)
06-12 21:40:44.498 16115-16132/com.example.worldclocktest W/System.err:     at com.example.worldclocktest.TimeZoneDownloadingService.showMessage(TimeZoneDownloadingService.java:31)
06-12 21:40:44.498 16115-16132/com.example.worldclocktest W/System.err:     at com.example.worldclocktest.TimeZoneDownloadingService.parse(TimeZoneDownloadingService.java:103)
06-12 21:40:44.498 16115-16132/com.example.worldclocktest W/System.err:     at com.example.worldclocktest.TimeZoneDownloadingService.StartDownloadTimezone(TimeZoneDownloadingService.java:91)
06-12 21:40:44.498 16115-16132/com.example.worldclocktest W/System.err:     at com.example.worldclocktest.TimeZoneDownloadingService$1.run(TimeZoneDownloadingService.java:41)
06-12 21:40:44.498 16115-16132/com.example.worldclocktest W/System.err:     at java.lang.Thread.run(Thread.java:818)
06-12 21:40:49.868 16115-16115/com.example.worldclocktest D/DisplayManager: DisplayManager()
06-12 22:33:16.304 19546-19573/com.example.worldclocktest W/System.err: Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb89447a0: Failure in SSL library, usually a protocol error

标签: android

解决方案


推荐阅读