zoukankan      html  css  js  c++  java
  • android初使用

    *

    刚学习android,做了一个功能,记录遇到的问题

    1,使用listview时,设置适配器adapter,忘记在getCount(),getItem(),getItemId()方法内部具体去实现,导致window leaked错误

    开始还以为是有什么对话框没有hide

    2,从发请求到显示数据的过程

    android客户端发送请求,将数据封装成json格式,一般是启动一个子线程去请求,通过连接Webservice的接口,调用webservice发布的接口方法,

    解析json格式的数据参数,执行操作,如对数据库增删改查,返回结果时依然封装成json格式,在回调函数中处理返回的数据,根据需要修改显示页面

    下面是连接webservice的方法

    public static final void transCall(Handler handler,String methodName, int soapVersion, boolean isDotNet,
                Map<String, Object> property) {
            
            /**
             * 命名空间
             */
            String NAMESPACE = INSTANCE.getNameSpace();
            /**
             * webservice提供接口<br/>
             * 例如:java的wsdl——>http://192.168.1.117:10101/services?wsdl
             */
            String ENDPOINT = INSTANCE.getEndPoint();
            Message message = Message.obtain();
            message.what = WebServiceStateCode.RESPONSE_SERVER_ERROR;
            String soapAction = NAMESPACE + methodName;
    
            HttpTransportSE transport = new HttpTransportSE(ENDPOINT, 20000);
            // 指定WebService的命名空间和调用的方法名
            SoapObject rpc = new SoapObject(NAMESPACE, methodName);
            if (property != null && !property.isEmpty()) {
                Set<String> keys = property.keySet();
                for (String key : keys) {
                    rpc.addProperty(key, property.get(key));
                }
            }
            // 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    soapVersion);
    
            envelope.bodyOut = rpc;
            // 设置是否调用的是dotNet开发的WebService
            envelope.dotNet = isDotNet;
            // 等价于envelope.bodyOut = rpc;
            envelope.setOutputSoapObject(rpc);
            boolean called = false;//标识webService是否请求成功
            try {
                // 调用WebService
                transport.call(soapAction, envelope);
                transport.getConnection().disconnect();
                called = true;
            } catch (IOException e) {
                message.obj = "网络无法连接。";
            } catch (XmlPullParserException e) {
                message.obj = "解析服务端xml文件异常。";
            }
            if (called) {//webService请求成功
                // // 获取返回的数据
                SoapObject object = (SoapObject) envelope.bodyIn;
                SoapPrimitive  resultSoap = (SoapPrimitive)object.getProperty("result");
                if (resultSoap != null) {
                    try {
                        JSONObject result = new JSONObject(resultSoap.toString());
                        String statusCode = result.getString("statusCode");
                        if (statusCode.equals("200")) {// 服务端请求响应成功
                            if(result.has("msg")){
                                message.obj = result.getJSONObject("msg");
                            }
                            message.what = WebServiceStateCode.RESPONSE_OK;
                        } else if (statusCode.equals("403")) {
                            message.what = WebServiceStateCode.RESPONSE_FORBIDDEN;
                        } else if (statusCode.equals("500")) {
                            message.what = WebServiceStateCode.RESPONSE_SERVER_ERROR;
                        }
                    } catch (JSONException e) {
                        message.obj = "json格式数据获取异常。";
                    }
                }
            }
            handler.sendMessage(message);
        }

    我的理解: 

    首先,信从A送给B

    相当于设置信封上的地址,便于去找某个webservice的某个方法

    String soapAction = NAMESPACE + methodName;   //http://service.webservice.magnetic.com/login

    相当于邮递员

    HttpTransportSE transport = new HttpTransportSE(ENDPOINT, 20000);

    信封,写上了邮编

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
    soapVersion);

    SoapObject rpc = new SoapObject(NAMESPACE, methodName);

    放参数,构造信上的内容

    if (property != null && !property.isEmpty()) {
    Set<String> keys = property.keySet();
    for (String key : keys) {
    rpc.addProperty(key, property.get(key));
    }
    }

    envelope.bodyOut = rpc; 把内容写到信上

    // 调用WebService
    transport.call(soapAction, envelope); 邮递员去送信,手握信封地址(soapAction)和信封(envelope,已经写了内容)

    这样送信完成

    然后,B有回信给A

    // // 获取返回的数据
    SoapObject object = (SoapObject) envelope.bodyIn;  //获得回信内容

    一般按自己的封装原则如:

    findListForSQLResponse{result={"msg":{"result":[{"0":"aaa","1":"1.3","2":"/android/vmark_1.3.apk"}]},"statusCode":"200"}; }

    然后各种方法去除json格式数据

    最后别忘了handler.sendMessage(message);

    这是回调,给一直在等结果的人(handler)发送结果,然后自动调用handlemessage()方法

    3,做后台消息推送的时候,mainactivity中启动service,service的onStartCommand()方法中查询,查询返回的结果在回调处理类中已消息的形式推送这条数据

    调用webservice的方法不能正确执行,连断点都不能进

    原因是在AndroidManifest.xml中配置service出错,

    下面这种是调用第三方webservice使用

    <service
                android:name="com.baidu.location.f"
                android:enabled="true"
                android:process=":remote" >
            </service>

    下面这种是调用本地

     <service
                android:exported="false"
                android:name=".service.NewUpdateService"
                android:enabled="true" >
                <intent-filter>
                     <action android:name="com.magnetic.market.service.NewUpdateService" />  
                </intent-filter>
            </service>

    *

    有问题在公众号【清汤袭人】找我,时常冒出各种傻问题,然一通百通,其乐无穷,一起探讨


  • 相关阅读:
    适用于 Laravel 的内部收单模块
    适用于 Laravel API 的签名看守器
    适用于 Laravel 的百度搜索推送
    适用于Yii2的千万级数据秒分页
    PostMan 代理的一个大坑
    PHP 各种金融利息的计算方法
    软件工程之UML建模课
    Windows 通过 cmd 得到域名的dns
    在windows下,通过git-bash里的ssh,远程登陆虚拟机里的linux
    Java常见缩写
  • 原文地址:https://www.cnblogs.com/qingmaple/p/4211085.html
Copyright © 2011-2022 走看看