Android with Emulator Shell

1. Change the HOSTS
First step, start my emulator.

Then, remount the device image as writable
>adb remount

Then, save a copy of the hosts file on my own machine
>adb pull /system/etc/hosts /Users/carl/data

Then, edit the hosts as I like

Then, save and push back the hosts file to the emulator
>adb push /Users/carl/data/hosts /system/etc

2. Connect to HTTPS
Error Message when I try to connect to HTTPS
POST: javax.net.ssl.SSLException: Not trusted server certificate
javax.net.ssl.SSLException: Not trusted server certificate

Solution:
Some solutions are about disable the SSL based on HTTP-Client or KSOAP2.
Disable SSL check based on HTTPCLIENT
http://blog.syedgakbar.com/2012/07/21/android-https-and-not-trusted-server-certificate-error/
Disable SSL check based on ksoap2
http://lanyan-lan.iteye.com/blog/1523821

But I am planning to install the CERT on my emulator.
http://intrepidusgroup.com/insight/2011/08/setting-up-a-persistent-trusted-ca-in-an-android-emulator/

Step 1: Set up OS environment
Android is using a specific keystone type called Bouncycastle KeyStore(BKS).
I download the jar package and place it in my JAVA_HOME lib directory
http://www.bouncycastle.org/archive/141/bcprov-jdk16-141.jar

On my MAC laptop, the directory is here:
/System/Library/Frameworks/JavaVM.framework/Home/lib/ext

>cd /System/Library/Frameworks/JavaVM.framework/Home/lib/ext
>sudo cp /Users/carl/data/bcprov-jdk16-141.jar ./

Try to manually install the CERT
>adb pull /system/etc/security/cacerts.bks /Users/carl/data/

Prepare the CERT file, open the URL with Firefox.
[Add Exception…] ---> [View] ---> [Details] ----> [Export] ----> File name sillycat with [X.509 Certificate (PEM)]
>cd /Users/carl/data
>mv sillycat sillycat.cert
Check the certificate file
>keytool -printcert -file sillycat.cer

Example:
keytool -keystore cacerts.bks -storetype BKS -provider org.bouncycastle.jce.provider.BouncyCastleProvider -storepass changeit -importcert -trustcacerts -alias somealias -file ca.cer -noprompt

>keytool -keystore cacerts.bks -storetype BKS -provider org.bouncycastle.jce.provider.BouncyCastleProvider -storepass changeit -importcert -trustcacerts -alias sillycat -file sillycat.cer -noprompt

I used to think that I do not have password, so I get rid of the line -storepass changeit, I got
Error Message:
keytool error: java.lang.NullPointerException
Solution:
The default password is changeit, but not empty.

Change the permission on AVD
>adb shell chmod 777 /system/etc/security/cacerts.bks

Push the changed file to AVD
>adb push cacerts.bks /system/etc/security/cacerts.bks
>adb shell ls /system/etc/security/

Restart the AVD and try again.
>adb shell stop
>adb shell start

Much Better, the error message changed
Error Message:
POST: java.lang.NullPointerException
java.lang.NullPointException
at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketFactoryImpl.createSocket(OpenSSLSocketFactoryImpl.java:83

Solution:
Maybe, that because I get the CER file by wrong way.
>echo | openssl s_client -connect api.local.sillycat.com:443 2>&1 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > s1.pem


Import the PEM file again
>keytool -keystore cacerts.bks -storetype BKS -provider org.bouncycastle.jce.provider.BouncyCastleProvider -storepass changeit -importcert -trustcacerts -alias s1 -file s1.pem -noprompt

Push the file again
>adb push cacerts.bks /system/etc/security/cacerts.bks

Restart the AVD
>adb shell stop
>adb shell start

But still not working.

It is not working for my project which is an old project. Maybe I can try in the newly project in the future.
I will just change the https to http on my local machine this time.

References:
http://www.bradcurtis.com/2011/02/13/hosts-file-google-android-emulator/
http://stackoverflow.com/questions/14109319/avd-emulator-browser-wont-use-hosts-file

http://stackoverflow.com/questions/5687082/obtain-root-access-via-su-on-the-android-emulator
http://allencch.wordpress.com/2012/02/29/learn-to-root-android-using-emulator/

http://androidsu.com/superuser/
http://www.bradcurtis.com/2011/02/13/hosts-file-google-android-emulator/

Disable SSL check based on HTTPCLIENT
http://blog.syedgakbar.com/2012/07/21/android-https-and-not-trusted-server-certificate-error/
Disable SSL check based on ksoap2
http://lanyan-lan.iteye.com/blog/1523821

http://blog.antoine.li/2010/10/22/android-trusting-ssl-certificates/

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android(安卓)中intent传递序列化信息(传
  2. Android(安卓)文件的保存与读取之SDCard(S
  3. android内存泄露
  4. Android中的消息机制
  5. android getResources的作用
  6. ❤️【Android精进之路-04】Android核心组
  7. 分享Android(安卓)KTX + Kotlin协程 组合
  8. android ndk 开发
  9. Unity和Android通信系列文章1——总览
  10. Android百度地图(二):百度地图sdk显示位