1、将InputStream转化到String,可用第三方jar包:
// 引入apache的io包
import org.apache.commons.io.IOUtils;
InputStream in = con.getInputStream();
String result = IOUtils.toString(in, "UTF-8");
2、AIDL相关:
1)AIDL(跨进程调用Service):
Android Interface Definition Language
2)AIDL的语法与Java的接口,存在的几点差异:
● AIDL定义的接口源代码必须以.aidl结尾;
● AIDL接口中用到数据类型,除了基本类型、String、List、Map、CharSequence之外,其他类型
全部都需要导包,即使它们在同一个包。
3、AIDL接口只是定义进程之间的接口,Service端、客户端都需要使用Android SDK目录下的
platform-tools下的aidl.exe工具为该接口提供实现。
4、定义AIDL接口后,定义一个Service的实现类,该Service的onBind()方法所返回的IBinder对象是ADT所生成的
XXX.Stub的子类的实例。
5、Manifest.xml配置AIDL_Service:
<service
android:name="com.download.sdk.DownloadService"
android:label=" Download Service"
android:process=":download">
<intent-filter android:priority="1000">
<action android:name="default_value" />
</intent-filter>
</service>
6、客户端绑定远程Service步骤:
● 创建ServiceConnection对象;
● 以ServiceConnection对象作为参数,调用Context的bindService()方法绑定远程Service即可。
7、远程绑定的Service的ServiceConnection并不能直接获取Service的onBind()方法所返回的对象,只能返回onBind()
方法所返回的对象的代理。因此在ServiceConnection的onServiceConnected方法中需要做如下处理:
// 服务绑定器
private ServiceConnection sConnect = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Logger.d(TAG, "onServiceDisconnected() called");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 绑定到服务
Logger.d(TAG, "onServiceConnected() called");
downloadService = IDownloadService.Stub.asInterface(service);
try {
downloadService.registerCallback(mCallback);
} catch (RemoteException e) {
Logger.e(TAG, e);
}
}
};