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;
}
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;
}
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);
}
}
}
Aggregations