Search in sources :

Example 1 with NetworkOnMainThreadException

use of android.os.NetworkOnMainThreadException in project Fast-Android-Networking by amitshekhariitbhu.

the class Utils method getErrorForNetworkOnMainThreadOrConnection.

public static ANError getErrorForNetworkOnMainThreadOrConnection(Exception e) {
    ANError error = new ANError(e);
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && e instanceof NetworkOnMainThreadException) {
        error.setErrorDetail(ANConstants.NETWORK_ON_MAIN_THREAD_ERROR);
    } else {
        error.setErrorDetail(ANConstants.CONNECTION_ERROR);
    }
    error.setErrorCode(0);
    return error;
}
Also used : ANError(com.androidnetworking.error.ANError) NetworkOnMainThreadException(android.os.NetworkOnMainThreadException)

Example 2 with NetworkOnMainThreadException

use of android.os.NetworkOnMainThreadException in project android-mdm-agent by flyve-mdm.

the class MqttDownloadSSL method doInBackground.

protected Boolean doInBackground(Object... object) {
    String mBroker = (String) object[0];
    Integer mPort = (Integer) object[1];
    Context ctx = (Context) object[2];
    FlyveLog.v("Trying to load certificate from : %s", mBroker + ":" + mPort);
    String cert = "";
    // create custom trust manager to ignore trust paths
    TrustManager trm = new X509TrustManager() {

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public void checkClientTrusted(X509Certificate[] certs, String authType) {
        }

        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        }
    };
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, new TrustManager[] { trm }, null);
        SSLSocketFactory factory = sc.getSocketFactory();
        SSLSocket socket = (SSLSocket) factory.createSocket(mBroker, mPort);
        socket.startHandshake();
        SSLSession session = socket.getSession();
        // get all certificates
        java.security.cert.Certificate[] certs = session.getPeerCertificates();
        for (int i = 0; i < certs.length; i++) {
            cert = cert + "-----BEGIN CERTIFICATE-----\n" + Base64.encodeToString(certs[i].getEncoded(), Base64.DEFAULT) + "-----END CERTIFICATE-----\n\n";
        }
        FlyveLog.v("Find certificates : %s", cert);
    } catch (NetworkOnMainThreadException ex) {
        FlyveLog.e(this.getClass().getName() + ", NetworkOnMainThreadException", ex.toString());
    } catch (Exception ex) {
        FlyveLog.e(this.getClass().getName() + ", Exception", ex.toString());
    }
    // try to save certificates into file
    try {
        String filename = "broker_cert";
        FileOutputStream outputStream;
        outputStream = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
        outputStream.write(cert.getBytes());
        outputStream.flush();
        outputStream.close();
    } catch (FileNotFoundException ex) {
        FlyveLog.e(this.getClass().getName() + ", FileNotFoundException", ex.toString());
    } catch (IOException ex) {
        FlyveLog.e(this.getClass().getName() + ", IOException", ex.toString());
    }
    return true;
}
Also used : Context(android.content.Context) SSLContext(javax.net.ssl.SSLContext) SSLSocket(javax.net.ssl.SSLSocket) SSLSession(javax.net.ssl.SSLSession) FileNotFoundException(java.io.FileNotFoundException) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate) IOException(java.io.IOException) NetworkOnMainThreadException(android.os.NetworkOnMainThreadException) FileNotFoundException(java.io.FileNotFoundException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) FileOutputStream(java.io.FileOutputStream) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) X509Certificate(java.security.cert.X509Certificate) NetworkOnMainThreadException(android.os.NetworkOnMainThreadException)

Example 3 with NetworkOnMainThreadException

use of android.os.NetworkOnMainThreadException in project OkHttp3 by MrZhousf.

the class HttpHelper method doRequestSync.

/**
 * 同步请求
 */
HttpInfo doRequestSync(OkHttpHelper helper) {
    Call call = null;
    final HttpInfo info = httpInfo;
    Request request = helper.getRequest();
    String url = info.getUrl();
    if (!checkUrl(url)) {
        return retInfo(info, HttpInfo.CheckURL);
    }
    request = request == null ? buildRequest(info, helper.getRequestType(), helper.getProgressCallback()) : request;
    showUrlLog(request);
    helper.setRequest(request);
    OkHttpClient httpClient = helper.getHttpClient();
    try {
        httpClient = httpClient == null ? super.httpClient : httpClient;
        call = httpClient.newCall(request);
        BaseActivityLifecycleCallbacks.putCall(requestTag, call);
        Response res = call.execute();
        return dealResponse(helper, res);
    } catch (IllegalArgumentException e) {
        return retInfo(info, HttpInfo.ProtocolException);
    } catch (SocketTimeoutException e) {
        if (null != e.getMessage()) {
            if (e.getMessage().contains("failed to connect to"))
                return retInfo(info, ConnectionTimeOut);
            if (e.getMessage().equals("timeout"))
                return retInfo(info, WriteAndReadTimeOut);
        }
        return retInfo(info, WriteAndReadTimeOut);
    } catch (UnknownHostException e) {
        if (!helperInfo.getOkHttpUtil().isNetworkAvailable()) {
            return retInfo(info, HttpInfo.CheckNet, "[" + e.getMessage() + "]");
        } else {
            return retInfo(info, HttpInfo.CheckURL, "[" + e.getMessage() + "]");
        }
    } catch (NetworkOnMainThreadException e) {
        return retInfo(info, HttpInfo.NetworkOnMainThreadException);
    } catch (Exception e) {
        return retInfo(info, HttpInfo.NoResult, "[" + e.getMessage() + "]");
    } finally {
        // 普通网络请求结束时自动取消请求,文件下载或上传需要在异步返回时取消,避免因提前取消请求导致无法回调的问题
        if (helper.getBusinessType() == BusinessType.HttpOrHttps) {
            BaseActivityLifecycleCallbacks.cancel(requestTag, call);
        }
    }
}
Also used : HttpInfo(com.okhttplib.HttpInfo) Response(okhttp3.Response) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) Request(okhttp3.Request) NetworkOnMainThreadException(android.os.NetworkOnMainThreadException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) NetworkOnMainThreadException(android.os.NetworkOnMainThreadException)

Aggregations

NetworkOnMainThreadException (android.os.NetworkOnMainThreadException)3 IOException (java.io.IOException)2 Context (android.content.Context)1 ANError (com.androidnetworking.error.ANError)1 HttpInfo (com.okhttplib.HttpInfo)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 SocketTimeoutException (java.net.SocketTimeoutException)1 UnknownHostException (java.net.UnknownHostException)1 X509Certificate (java.security.cert.X509Certificate)1 SSLContext (javax.net.ssl.SSLContext)1 SSLSession (javax.net.ssl.SSLSession)1 SSLSocket (javax.net.ssl.SSLSocket)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 TrustManager (javax.net.ssl.TrustManager)1 X509TrustManager (javax.net.ssl.X509TrustManager)1 Call (okhttp3.Call)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1