【问题标题】:how to store SoapObject data to an array?如何将 SoapObject 数据存储到数组中?
【发布时间】:2013-11-27 05:54:58
【问题描述】:


我正在使用 .Net Web 服务使用 ksoap2 从我在 Android 中的服务器获取数据。
我可以使用以下代码将数据提取到 SoapObject

    public class MainActivity extends Activity {
        private static final String _SOAP_ACTION = "http://tempuri.org/getUserDetails";
        private static final String _METHOD_NAME = "getUserDetails";
        private static final String _NAMESPACE = "http://tempuri.org/";
        private static final String _URL = "http://sush19.in/myservice/myservice.asmx";
        SoapObject _response;

        public void getUserInfo() {
            SoapObject request = new SoapObject(_NAMESPACE, _METHOD_NAME);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet=true;
            envelope.setOutputSoapObject(request);
            try
            {
                AndroidHttpTransport aht=new AndroidHttpTransport(_URL);
                aht.call(_SOAP_ACTION,envelope);
                _response=(SoapObject)envelope.getResponse();
                String s1 = (String)_response.getProperty(0);
                String s2 = (String)_response.getProperty(1);
                String s3 = (String)_response.getProperty(2);
                String s4 = (String)_response.getProperty(3);
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
                Toast.makeText(getBaseContext(), "Oops..."+ex, Toast.LENGTH_LONG).show();
            }
        }
    }

我可以通过使用上面的_response.getProperty得到每个值,我得到的值是正确的
anyType{anyType=user1; anyType=pswd1; anyType=user2; anyType=pswd2; anyType=user3; anyType=pswd3; anyType=user4; anyType=pswd4; anyType=user5; anyType=pswd5; anyType=user6; anyType=pswd6; }
我获取的数据是用户名和密码。
我想将它存储在一个数组中以进行进一步的操作,例如将其存储到 Sqlite DB 中。我的数据库表有两列作为

username    userPassword

我希望我的上述数据采用以下格式:

user1   pswd1
user2   pswd2
user3   pswd3
user4   pswd4
user5   pswd5
user6   pswd6

这样就很容易找到用户名和相应的密码。如何将SoapObject 转换为Array

【问题讨论】:

    标签: android arrays sqlite


    【解决方案1】:

    第 1 步:创建一个与您的 .net 服务器返回相同的 bean 类 第 2 步:解析数据并创建该 bean 类的对象 第三步:加入数组 做你想做的事

    【讨论】:

    • 感谢您的回复,我是 android 新手,所以我不明白您所说的,但我已经解决了它以使用 2D 数组获取输出。我已经发布我的回答..
    【解决方案2】:
    int propertyCount = _response.getPropertyCount();
    if(propertyCount % 2 != 0) {
    // The response is not even so you have many option remove last count or return error
    }
    HashMap<String, String> map = new HashMap<String, String>();
    for(int index = 0 ; intdex > propertyCount/2 ; index +=2) 
    {
       map.put((String)_response.getProperty(index), (String)_response.getProperty(index + 1));
    }
    

    【讨论】:

    • 感谢您的回复...我尝试了您在上面提供的代码,但我没有得到如何从 map 获取数据,我通过 HashMap 找到了第一个字符串是Key,其中第二个是value。所以我从你的代码中得到了一些想法,我用二维数组解决了它。我已经发布了我的答案。谢谢你...
    【解决方案3】:

    感谢@Basbous,我得到了我想要的解决方案,我从你提供的代码中得到了一些想法..
    最后我通过添加以下代码解决了它:

    public class MainActivity extends Activity {
            private static final String _SOAP_ACTION = "http://tempuri.org/getUserDetails";
            private static final String _METHOD_NAME = "getUserDetails";
            private static final String _NAMESPACE = "http://tempuri.org/";
            private static final String _URL = "http://sush19.in/myservice/myservice.asmx";
            SoapObject _response;
    
            public Boolean getUserInfo() {
                SoapObject request = new SoapObject(_NAMESPACE, _METHOD_NAME);
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.dotNet=true;
                envelope.setOutputSoapObject(request);
                try
                {
                    AndroidHttpTransport aht=new AndroidHttpTransport(_URL);
                    aht.call(_SOAP_ACTION,envelope);
                    _response=(SoapObject)envelope.getResponse();
                    int propertyCount = _response.getPropertyCount();
                    String[][] userDetails = new String[propertyCount/2][2];
                    int temp = 0;
                    if(propertyCount % 2 != 0) {
                        return false;
                    }
                    else
                    {
                        for(int i = 0 ; i < propertyCount/2 ; i++) 
                        {
                            for(int j = 0; j < 2; j++)
                            {
                                userDetails[i][j] = (String)_response.getProperty(temp);
                                temp = temp + 1;
                            }
                        }
                    }
                    return true;
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                    Toast.makeText(getBaseContext(), "Oops..."+ex, Toast.LENGTH_LONG).show();
                    return false;
                }
            }
        }
    

    我从服务器获得的值列表现在存储在二维数组userDetails[][] 中,我可以将其传递给任何Activity,并且可以使用for loop 轻松访问它。

    【讨论】:

      猜你喜欢
      • 2021-04-09
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      • 2016-10-06
      • 2019-11-10
      • 2020-02-04
      • 2012-04-28
      • 2012-01-13
      相关资源
      最近更新 更多