安卓开发遇到的问题集(一)

刚刚接触安卓开发,碰到各种各样的问题、记录下来

客户端ANDROID 服务端ASP.NET

1、安卓访问ASP.NET的WEBSERVICES

好像不能直接访问,我是参考了网上的做法

引入第三方的类包ksoap2,

ANDROID代码

 SoapObject soapObject  = new SoapObject(nameSpace, methodName);
   soapObject.addProperty("userName", "XXX");
   soapObject.addProperty("password", "123");

   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
     SoapEnvelope.VER11);
   
   envelope.dotNet = false;
   envelope.bodyOut = soapObject;
   envelope.setOutputSoapObject(soapObject);
   org.ksoap2.transport.HttpTransportSE transport = new HttpTransportSE(SERVER_URL);
    transport.debug = true;//是否是调试模式

   
     transport.call(soapAction, envelope);
     SoapObject so = (SoapObject)envelope.bodyIn;//服务器返回的对象存在envelope的bodyIn中
       //  User us= (User)envelope.getResponse();//直接将返回值强制转换为已知对象
       String res =  envelope.getResponse().toString();

服务端注意点

 服务类必须要入    [SoapRpcService]//指定使用rpc方式 特性

方法名也要加入[SoapRpcMethod]特性

    /// <summary>
    /// MobileService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [SoapRpcService]//指定使用rpc方式
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class MobileService : System.Web.Services.WebService
    {

        [SoapRpcMethod, WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }

2、做自动更新模块发,发现的下载存储SD卡与存储当前程序存储的区别

参考链接:http://www.cnblogs.com/coolszy/archive/2012/04/27/2474279.html

SDK下载存储代码段

     // 获得存储卡的路径
                    String sdpath = Environment.getExternalStorageDirectory() + "/";
                    mSavePath
= sdpath + "download"
;
                    URL url
= new URL(mHashMap.get("url"));
                   
// 创建连接
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.connect();
                   
// 获取文件大小
                    int length = conn.getContentLength();
                   
// 创建输入流
                    InputStream is = conn.getInputStream();

                    File file
= new File(mSavePath);
                   
// 判断文件目录是否存在
                    if (!file.exists())
                    {
                        file.mkdir();
                    }
                    File apkFile
= new File(mSavePath, mHashMap.get("name"));
                    FileOutputStream fos
= new
FileOutputStream(apkFile);
                   
int count = 0;

SD卡读取代码段

File apkfile = new File(mSavePath, mHashMap.get("name"));
       
if (!apkfile.exists())
        {
           
return;
        }
       
// 通过Intent安装APK文件
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setDataAndType(Uri.parse(
"file://" + apkfile.toString()), "application/vnd.android.package-archive");
        mContext.startActivity(i);

如果有的机器没有SD卡,我自动更新时候放在当前应用的存储

                    String apkUrl = VersionManager.getServiceLastedVersionUrl(mContext);
                    URL url = new URL(apkUrl);
                    // 创建连接
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.connect();
                    // 获取文件大小
                    int length = conn.getContentLength();
                    // 创建输入流
                    InputStream is = conn.getInputStream();

                    FileOutputStream fos = mContext.openFileOutput(VersionManager.getServiceLastedVersionName(mContext)+".apk", mContext.MODE_WORLD_READABLE);

注意:MODE要选MODE_WORLD_READABLE

读取

 String absolutePath = mContext.getFileStreamPath(VersionManager.getServiceLastedVersionName(mContext)+".apk").getAbsolutePath(); 

     File apkfile = new File(absolutePath);
        if (!apkfile.exists())
        {
            return;
        }
        // 通过Intent安装APK文件
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");
        mContext.startActivity(i);

3、自动更新,解析包错误与应用程序未安装的问题

1)解析包错误,下载的安装包必须以.apk作为后缀结尾,这样android系统才能识别

2)自动更新安装失败,应用程序未安装的问题:我们打包时候有签名的打包与未签名的打包,用签名的打包必须用到签名文件

我用的是debug.keystore 路径为 SDK安装路径\.android\debug.keystore

4、打包androdi的APK文件

项目点右键——AndroidTool——Use existkeystore 路径选择\.android\debug.keystore 密码android 然后finish就OK啦

参考:http://www.chendw.cn/programming/214.html

5、IIS无法下载APK文件

有可能的原因是网站应用没有配置MIME类型

.apk   application/vnd.android 
打开Internet 服务管理器Internet 服务管理器-->网站属性-->HTTP头(MIME类型)-->新建
扩展名.apk   类型(MIME) application/vnd.android

原文地址:https://www.cnblogs.com/xfoolishpig/p/3081243.html