从Web服务调用中检索信息

时间:2010-05-16 03:10:30

标签: android web-services

我正在尝试从Web服务调用中检索信息。以下是我到目前为止的情况。在我的文本视图中,它显示

Map {item=anyType{key=TestKey; value=2;}; item=anyType{key=TestField; value=adsfasd; };}

当我在调试器中运行它时,我可以在变量tempvar中看到上面的信息。但问题是,如何检索信息(即每个阵列位置中“key”和“value”的实际值)?是的,我知道onCreate会有很多事情发生,我稍后会修复它。

提前致谢, 蒙

我的代码如下,

import java.util.Vector;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;


public class ViewHitUpActivity extends Activity {
private static final String SOAP_ACTION = "test_function";
private static final String METHOD_NAME = "test_function";
private static final String NAMESPACE = "http://www.monteandjanicechan.com/";
private static final String URL = "http://www.monteandjanicechan.com/ws/test_ws.cfc?wsdl";
// private Object resultRequestSOAP = null;
private TextView tv;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
tv = (TextView)findViewById(R.id.people_view);

//SoapObject
request.addProperty("test_item", "1");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);


AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
try
{
androidHttpTransport.call(SOAP_ACTION, envelope);
/*
resultRequestSOAP = envelope.getResponse();
Vector tempResult = (Vector) resultRequestSOAP("test_functionReturn");
*/
SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;

Vector tempResult = (Vector) resultsRequestSOAP.getProperty("test_functionReturn");

int testsize = tempResult.size();

// SoapObject test = (SoapObject) tempResult.get(0);

//String[] results = (String[]) resultRequestSOAP;

Object tempvar = tempResult.elementAt(1);

tv.setText(tempvar.toString());
}
catch (Exception aE)
{
aE.printStackTrace ();
tv.setText(aE.getClass().getName() + ": " + aE.getMessage());
}
}
} 

2 个答案:

答案 0 :(得分:0)

您应该查看Ksoap2的文档,以便完全了解正在发生的事情。但是,我猜你的Object tempvar可能会成为java.util.Map,它有读取密钥和提取每个密钥值的方法。

答案 1 :(得分:0)

我实际上找到了另一种方法。我使用cfproperty重新构建了我的Web服务,以定义Web服务中的属性。然后在Web服务中,我使用以下内容来获取属性,

SoapObject test =(SoapObject)tempResult.get(0);

       String temp_string = (String) test.getProperty("TestKey"); 
       temp_string = temp_string + " " + (String) test.getProperty("TestField");

现在,一切都按照应有的方式发挥作用。