我有一个问题,同时调用web服务。 我在服务器上有一个.NET Web服务,而且我在使用Android中KSOAP2(ksoap2-J2SE的全2.1.2)。 当运行程序我得到一个运行时异常,如“org.ksoap2.serialization.SoapPrimitive”。 我该怎么办?
这里是我的代码。
package projects.ksoap2sample;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.*;
import android.os.*;
import android.widget.TextView;
public class ksoap2sample extends Activity {
/** Called when the activity is first created. */
private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
private static final String METHOD_NAME = "HelloWorld";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://192.168.1.19/TestWeb/WebService.asmx";
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.text1);
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//request.addProperty("prop1", "myprop");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = (Object)envelope.getResponse();
String[] results = (String[]) result;
tv.setText( ""+results[0]);
}
catch (Exception e) {
tv.setText(e.getMessage());
}
}
}
这是非常简单的。 您所得到的结果为Object
这是一个原始的一种。
您的代码:
Object result = (Object)envelope.getResponse();
正确的代码:
SoapObject result=(SoapObject)envelope.getResponse();
//To get the data.
String resultData=result.getProperty(0).toString();
// 0 is the first object of data.
我想,这肯定应该工作。
请问你的NET Web服务是什么样子?
我一直在使用同样的效果KSOAP 2.3从code.google.com 。 我跟着在教程的代码项目 (这是伟大BTW)。
每次我用
Integer result = (Integer)envelope.getResponse();
得到我的web服务的结果(不分类型,我试过对象,字符串,INT)我跑进org.ksoap2.serialization.SoapPrimitive
例外。
我找到了解决方法(解决方法)。 我必须做的第一件事就是从我的web服务方法删除“SoapRpcMethod()属性。
[SoapRpcMethod(), WebMethod]
public Object GetInteger1(int i)
{
// android device will throw exception
return 0;
}
[WebMethod]
public Object GetInteger2(int i)
{
// android device will get the value
return 0;
}
然后,我改变了我的Android代码:
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
不过,我得到一个SoapPrimitive对象,其中有一个“值”提起那是私人的。 幸运的是,价值是通过传递toString()
方法,所以我用Integer.parseInt(result.toString())
让我的价值,这是对我来说足够了,因为我没有任何复杂类型,我需要得到从我的Web服务。
下面是完整的源代码:
private static final String SOAP_ACTION = "http://tempuri.org/GetInteger2";
private static final String METHOD_NAME = "GetInteger2";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://10.0.2.2:4711/Service1.asmx";
public int GetInteger2() throws IOException, XmlPullParserException {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi = new PropertyInfo();
pi.setName("i");
pi.setValue(123);
request.addProperty(pi);
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
return Integer.parseInt(result.toString());
}
类型转换信封SoapPrimitive:
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
String strRes = result.toString();
它会工作。
您可以使用下面的代码来调用Web服务,并得到响应。使确保您的Web服务,如果你。如果你您在使用MySQL中 ,你需要从SQL Server数据库中使用的数据返回的数据表格式 ..This你的代码的帮助响应改变一件事刚刚从句子替换词NewDataSet obj2=(SoapObject) obj1.getProperty("NewDataSet");
通过DocumentElement
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://localhost/Web_Service.asmx?"; // you can use IP address instead of localhost
private static final String METHOD_NAME = "Function_Name";
private static final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("parm_name", prm_value); // Parameter for Method
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope); //call the eb service Method
} catch (Exception e) {
e.printStackTrace();
} //Next task is to get Response and format that response
SoapObject obj, obj1, obj2, obj3;
obj = (SoapObject) envelope.getResponse();
obj1 = (SoapObject) obj.getProperty("diffgram");
obj2 = (SoapObject) obj1.getProperty("NewDataSet");
for (int i = 0; i < obj2.getPropertyCount(); i++) //the method getPropertyCount() return the number of rows
{
obj3 = (SoapObject) obj2.getProperty(i);
obj3.getProperty(0).toString(); //value of column 1
obj3.getProperty(1).toString(); //value of column 2
//like that you will get value from each column
}
如果您有任何关于此问题的任何你可以写我..
如果一个以上的结果,预计,则getResponse()
方法将返回一个Vector
含各种响应。
在这种情况下有问题的代码变为:
Object result = envelope.getResponse();
// treat result as a vector
String resultText = null;
if (result instanceof Vector)
{
SoapPrimitive element0 = (SoapPrimitive)((Vector) result).elementAt(0);
resultText = element0.toString();
}
tv.setText(resultText);
答案基础上, ksoap2-的Android(mosabua叉)
我认为你不能打电话
androidHttpTransport.call(SOAP_ACTION, envelope);
在主线程。
网络运营应该在不同的线程来完成。
创建另一个线程或的AsyncTask调用该方法。