我的数组定义如下:
ArrayList<String> items = new ArrayList<String>();
我现在已经填充了该数组,我希望通过Web服务将其发送到我的远程数据库。
以下是我的Android代码中执行ksoap工作的方法
public String[] call2()
{
SoapPrimitive responsesdata = null;
SoapObject request = new SoapObject(namespace, method_NAME);
request.addProperty("names",items);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(url);
androidHttpTransport.debug = true;
try
{
androidHttpTransport.call(soap_ACTION, envelope);
//responsesdata = (SoapPrimitive) envelope.getResponse();
SoapObject result = (SoapObject)envelope.bodyIn;
if(result != null)
{
System.out.println("SOAP response:\n\n" + result.getProperty(0).toString());
}
System.out.println(" --- response ---- " + responsesdata);
} catch (SocketException ex) {
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println( " ----" + responsesdata );
String serviceResponse= responsesdata .toString();
System.out.println(serviceResponse);
String[] temp;
String delimiter = ",";
temp= serviceResponse.split(delimiter);
return temp;
}
然而,当我运行我的代码时,我得到Null Pointer Exception,请参阅下面的logcat:
11-10 15:22:50.496: WARN/System.err(295): java.lang.RuntimeException: Cannot serialize: []
11-10 15:22:50.496: WARN/System.err(295): at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:629)
11-10 15:22:50.505: WARN/System.err(295): at org.ksoap2.serialization.SoapSerializationEnvelope.writeProperty(SoapSerializationEnvelope.java:613)
11-10 15:22:50.515: WARN/System.err(295): at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:582)
11-10 15:22:50.515: WARN/System.err(295): at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:566)
11-10 15:22:50.515: WARN/System.err(295): at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:623)
11-10 15:22:50.515: WARN/System.err(295): at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:547)
11-10 15:22:50.515: WARN/System.err(295): at org.ksoap2.SoapEnvelope.write(SoapEnvelope.java:192)
11-10 15:22:50.525: WARN/System.err(295): at org.ksoap2.transport.Transport.createRequestData(Transport.java:74)
11-10 15:22:50.555: INFO/System.out(295): ----null
11-10 15:22:50.565: DEBUG/AndroidRuntime(295): Shutting down VM
11-10 15:22:50.565: WARN/dalvikvm(295): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-10 15:22:50.586: ERROR/AndroidRuntime(295): FATAL EXCEPTION: main
11-10 15:22:50.586: ERROR/AndroidRuntime(295): java.lang.NullPointerException
11-10 15:22:50.586: ERROR/AndroidRuntime(295): at com.example.display.call2(display.java:200)
11-10 15:22:50.586: ERROR/AndroidRuntime(295): at com.example.display$3.onClick(display.java:145)
11-10 15:22:50.586: ERROR/AndroidRuntime(295): at android.view.View.performClick(View.java:2408)
我的网络服务代码:
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public String getnames(String[] values)
{
try
{
using (SqlConnection myConnection = new SqlConnection(@"Data S ource=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123"))
{
int count = 1;
int rows = 0;
myConnection.Open();
foreach (string student in values)
{
count++;
using (SqlCommand myCommand = new SqlCommand())
{
myCommand.Connection = myConnection;
myCommand.CommandText = "insert into record values(@pCount, @pStudent)";
SqlParameter param = myCommand.CreateParameter();
param.ParameterName = "@pCount";
param.Value = count;
myCommand.Parameters.Add(param);
param = myCommand.CreateParameter();
param.ParameterName = "@pSudent";
param.Value = student;
rows = myCommand.ExecuteNonQuery();
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return "an error occured";
}
return "success";
}
}
}
答案 0 :(得分:2)
这是 KSOAP2 for Android库的已知问题,目前它根本不支持数组。问题描述如下:
http://code.google.com/p/ksoap2-android/issues/detail?id=19
可在此处找到第三方补丁,解决方案和示例:
http://people.unica.it/bart/ksoap2-patch/
查看这些链接,您可以找到答案。
感谢。
答案 1 :(得分:1)
请参阅此链接以了解如何发送数组&amp; pojo使用ksoap2进行Web服务。
http://code.google.com/p/ksoap2-android/wiki/CodingTipsAndTricks