我正在使用.Net Web Service使用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
答案 0 :(得分:0)
步骤1:创建一个与.net服务器返回相同的bean类 第2步:解析数据并创建该bean类的对象 第3步:添加到数组中 做任何你想做的事
答案 1 :(得分:0)
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));
}
答案 2 :(得分:0)
我得到了我想要的解决方案,感谢@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;
}
}
}
我从服务器获取的值列表现在存储在2D数组userDetails[][]
中,我可以将其传递给任何Activity
,并且可以轻松地使用for loop
访问它。