首页 文章 精选 留言 我的

精选列表

搜索[android things],共10000篇文章
优秀的个人博客,低调大师

Android:Toast提示框

Toast提示框: 1 2 3 4 5 6 7 8 9 10 findViewById(R.id.button1).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { //Toast.makeText(context对象, text提示内容, duration时间长短),记得加show()方法 Toast.makeText(MainActivity. this , "下载完成" , Toast.LENGTH_SHORT).show(); } }); 自定义Toast方法: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 private void clickToast() { Toast toast = new Toast( this ); // 设置View LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.toast_layout, null ); //通过布局 toast.setView(layout); //设置显示位置和显示时间 toast.setGravity(Gravity.CENTER, 60 , 0 ); toast.setDuration(Toast.LENGTH_SHORT); toast.show(); } private void clickToast1() { Toast toast = new Toast( this ); // 设置View TextView textView = new TextView( this ); textView.setBackgroundColor(Color.BLUE); textView.setText( "定制toast" ); toast.setView(textView); //设置显示位置和显示时间 toast.setGravity(Gravity.CENTER, 60 , 0 ); toast.setDuration(Toast.LENGTH_SHORT); toast.show(); } 本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1198603,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android ignore https certificate verification

通过Https访问的时候经常会遇到"Not trusted Server Certificate"的问题,有人说在3.0上面没有这个问题,可能已经改进了,在2.2及以前的版本中有这个问题。 开始想的是采用安装证书的方法(Trusting SSL certificates),最后也没有成功,不知道是证书的原因还是其他,有人说安装证书只能在WIFI上使用,没有找到官方文档,用户可能在GPRS上使用,只能放弃。 StackOverflow上也有相关的方案,我整理了一下。 我将注册的步骤封装到DefaultHttpClient子类中了,这样看上去更清晰一些,你也可以 直接实例化DefaultHttpClient的方法。 SchemeRegistryschemeRegistry=newSchemeRegistry();schemeRegistry.register(newScheme("https",sslf,443)); SingleClientConnManagercm=new SingleClientConnManager(post.getParams(),schemeRegistry); HttpClientclient=newDefaultHttpClient(cm,post.getParams()); /** *@authorBrant *@decription */ publicclassSSLHttpClientextendsDefaultHttpClient{ @Override protectedClientConnectionManagercreateClientConnectionManager(){ SchemeRegistryregistry=newSchemeRegistry(); registry.register(newScheme("http",PlainSocketFactory .getSocketFactory(),80)); //443是Https的默认端口,如果网站配置的端口不一样,这里要记着改一下 registry.register(newScheme("https",newEasySSLSocketFactory(),443)); returnnewSingleClientConnManager(getParams(),registry); } publicstaticSSLHttpClientgetInstance(){ SSLHttpClientclient=newSSLHttpClient(); client.setCookieStore(mCookie); returnclient; } } EasySSLSocketFactory: importjava.io.IOException; importjava.net.InetAddress; importjava.net.InetSocketAddress; importjava.net.Socket; importjava.net.UnknownHostException; importjavax.net.ssl.SSLContext; importjavax.net.ssl.SSLSocket; importjavax.net.ssl.TrustManager; importorg.apache.http.conn.ConnectTimeoutException; importorg.apache.http.conn.scheme.LayeredSocketFactory; importorg.apache.http.conn.scheme.SocketFactory; importorg.apache.http.params.HttpConnectionParams; importorg.apache.http.params.HttpParams; /** *Thissocketfactorywillcreatesslsocketthatacceptsselfsigned *certificate * *@authorolamy *@version$Id:EasySSLSocketFactory.java7653552009-04-1520:59:07Zevenisse *$ *@since1.2.3 */ publicclassEasySSLSocketFactoryimplementsSocketFactory, LayeredSocketFactory{ privateSSLContextsslcontext=null; privatestaticSSLContextcreateEasySSLContext()throwsIOException{ try{ SSLContextcontext=SSLContext.getInstance("TLS"); context.init(null,newTrustManager[]{newEasyX509TrustManager( null)},null); returncontext; }catch(Exceptione){ thrownewIOException(e.getMessage()); } } privateSSLContextgetSSLContext()throwsIOException{ if(this.sslcontext==null){ this.sslcontext=createEasySSLContext(); } returnthis.sslcontext; } /** *@seeorg.apache.http.conn.scheme.SocketFactory#connectSocket(java.net.Socket, *java.lang.String,int,java.net.InetAddress,int, *org.apache.http.params.HttpParams) */ publicSocketconnectSocket(Socketsock,Stringhost,intport, InetAddresslocalAddress,intlocalPort,HttpParamsparams) throwsIOException,UnknownHostException,ConnectTimeoutException{ intconnTimeout=HttpConnectionParams.getConnectionTimeout(params); intsoTimeout=HttpConnectionParams.getSoTimeout(params); InetSocketAddressremoteAddress=newInetSocketAddress(host,port); SSLSocketsslsock=(SSLSocket)((sock!=null)?sock:createSocket()); if((localAddress!=null)||(localPort>0)){ //weneedtobindexplicitly if(localPort<0){ localPort=0;//indicates"any" } InetSocketAddressisa=newInetSocketAddress(localAddress, localPort); sslsock.bind(isa); } sslsock.connect(remoteAddress,connTimeout); sslsock.setSoTimeout(soTimeout); returnsslsock; } /** *@seeorg.apache.http.conn.scheme.SocketFactory#createSocket() */ publicSocketcreateSocket()throwsIOException{ returngetSSLContext().getSocketFactory().createSocket(); } /** *@seeorg.apache.http.conn.scheme.SocketFactory#isSecure(java.net.Socket) */ publicbooleanisSecure(Socketsocket)throwsIllegalArgumentException{ returntrue;//不判断socket,直接返回true } /** *@seeorg.apache.http.conn.scheme.LayeredSocketFactory#createSocket(java.net.Socket, *java.lang.String,int,boolean) */ publicSocketcreateSocket(Socketsocket,Stringhost,intport, booleanautoClose)throwsIOException,UnknownHostException{ //return getSSLContext().getSocketFactory().createSocket(); //will get java.io.IOException: SSL handshake failure: I/O error //during system call, Broken pipe returngetSSLContext().getSocketFactory().createSocket(socket,host, port,autoClose); } //------------------------------------------------------------------- //javadocinorg.apache.http.conn.scheme.SocketFactorysays: //BothObject.equals()andObject.hashCode()mustbeoverridden //forthecorrectoperationofsomeconnectionmanagers //------------------------------------------------------------------- publicbooleanequals(Objectobj){ return((obj!=null)&&obj.getClass().equals( EasySSLSocketFactory.class)); } publicinthashCode(){ returnEasySSLSocketFactory.class.hashCode(); } } EasyX509TrustManager: importjava.security.KeyStore; importjava.security.KeyStoreException; importjava.security.NoSuchAlgorithmException; importjava.security.cert.CertificateException; importjava.security.cert.X509Certificate; importjavax.net.ssl.TrustManager; importjavax.net.ssl.TrustManagerFactory; importjavax.net.ssl.X509TrustManager; /** *@authorolamy *@version$Id:EasyX509TrustManager.java7653552009-04-1520:59:07Zevenisse *$ *@since1.2.3 */ publicclassEasyX509TrustManagerimplementsX509TrustManager{ privateX509TrustManagerstandardTrustManager=null; /** *ConstructorforEasyX509TrustManager. */ publicEasyX509TrustManager(KeyStorekeystore) throwsNoSuchAlgorithmException,KeyStoreException{ super(); TrustManagerFactoryfactory=TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); factory.init(keystore); TrustManager[]trustmanagers=factory.getTrustManagers(); if(trustmanagers.length==0){ thrownewNoSuchAlgorithmException("notrustmanagerfound"); } this.standardTrustManager=(X509TrustManager)trustmanagers[0]; } /** *@seejavax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[], *StringauthType) */ publicvoidcheckClientTrusted(X509Certificate[]certificates, StringauthType)throwsCertificateException{ standardTrustManager.checkClientTrusted(certificates,authType); } /** *@seejavax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[], *StringauthType) */ publicvoidcheckServerTrusted(X509Certificate[]certificates, StringauthType)throwsCertificateException{ if((certificates!=null)&&(certificates.length==1)){ certificates[0].checkValidity(); }else{ standardTrustManager.checkServerTrusted(certificates,authType); } } /** *@seejavax.net.ssl.X509TrustManager#getAcceptedIssuers() */ publicX509Certificate[]getAcceptedIssuers(){ returnthis.standardTrustManager.getAcceptedIssuers(); } } 然后直接实例化SSLHttpClient就可以想Http一样执行HttpGet和HttpPost方法了,希望能有所帮助:)。 本文转自 breezy_yuan 51CTO博客,原文链接:http://blog.51cto.com/lbrant/795636,如需转载请自行联系原作者

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册