首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将数组传递给SOAP web服务以插入到远程数据库中

将数组传递给SOAP web服务以插入到远程数据库中
EN

Stack Overflow用户
提问于 2011-11-10 14:12:16
回答 2查看 5.2K关注 0票数 0

我想使用可用的ksoap库将一个数组从我的Android应用程序传递到我的SOAP webservice。

我的数组看起来像这样:

代码语言:javascript
运行
复制
String[] values={"abc","def"};

如何将其作为参数传递给我的web服务调用?

有谁能帮帮我吗?

这是我的web服务代码:

代码语言:javascript
运行
复制
  public class Service1 : System.Web.Services.WebService
   {
    [WebMethod]

    public String getnames(String[] values)
    {

        try
        {

            using (SqlConnection myConnection = new SqlConnection(@"Data Source=.\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";
       }
     }
  }

我的logcat:

代码语言:javascript
运行
复制
   11-10 12:26:56.159: INFO/System.out(334):  ----null
   11-10 12:26:56.159: DEBUG/AndroidRuntime(334): Shutting down VM
   11-10 12:26:56.159: WARN/dalvikvm(334): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
   11-10 12:26:56.179: ERROR/AndroidRuntime(334): FATAL EXCEPTION: main
   11-10 12:26:56.179: ERROR/AndroidRuntime(334): java.lang.NullPointerException
   11-10 12:26:56.179: ERROR/AndroidRuntime(334):     at com.example.display.call2(display.java:193)
   11-10 12:26:56.179: ERROR/AndroidRuntime(334):     at com.example.display$3.onClick(display.java:146)
   11-10 12:26:56.179: ERROR/AndroidRuntime(334):     at android.view.View.performClick(View.java:2408)
   11-10 12:26:56.179: ERROR/AndroidRuntime(334):     at android.view.View$PerformClick.run(View.java:8816)
   11-10 12:26:56.179: ERROR/AndroidRuntime(334):     at android.os.Handler.handleCallback(Handler.java:587)
   11-10 12:26:56.179: ERROR/AndroidRuntime(334):     at android.os.Handler.dispatchMessage(Handler.java:92)
   11-10 12:26:56.179: ERROR/AndroidRuntime(334):     at android.os.Looper.loop(Looper.java:123)
   11-10 12:26:56.179: ERROR/AndroidRuntime(334):     at android.app.ActivityThread.main(ActivityThread.java:4627)
   11-10 12:26:56.179: ERROR/AndroidRuntime(334):     at java.lang.reflect.Method.invokeNative(Native Method)
   11-10 12:26:56.179: ERROR/AndroidRuntime(334):     at java.lang.reflect.Method.invoke(Method.java:521)
   11-10 12:26:56.179: ERROR/AndroidRuntime(334):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
   11-10 12:26:56.179: ERROR/AndroidRuntime(334):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
   11-10 12:26:56.179: ERROR/AndroidRuntime(334):     at dalvik.system.NativeStart.main(Native Method)
   11-10 12:26:56.189: WARN/ActivityManager(58):   Force finishing activity com.example/.display

提前感谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-10 14:25:34

这是读取SOAP web服务的示例代码,希望这能对您有所帮助。

代码语言:javascript
运行
复制
private static String SOAP_ACTION = "http://tempuri.org/HelloWorld";

private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "HelloWorld";

private static String URL = "http://bimbim.in/Sample/TestService.asmx?WSDL";

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

    //Initialize soap request + add parameters
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);        

    //Use this to add parameters
    request.addProperty("Parameter","Value");

    //Declare the version of the SOAP request
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);

    //Needed to make the internet call
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    try {
        //this is the actual part that will call the webservice
        androidHttpTransport.call(SOAP_ACTION, envelope);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Get the SoapResult from the envelope body.
    SoapObject result = (SoapObject)envelope.bodyIn;

    if(result != null){
        TextView t = (TextView)this.findViewById(R.id.resultbox);

        //Get the first property and change the label text
        t.setText("SOAP response:\n\n" + result.getProperty(0).toString());
    }

}

另请参阅

Android Lists IV: Accessing and Consuming a SOAP Web Service I

The Droid Chronicles – Web Services: Using kSOAP2 to Pass Complex Objects

编辑:

这是KSOAP2 for Android库的一个已知问题,目前它根本不支持数组。问题描述如下:

http://code.google.com/p/ksoap2-android/issues/detail?id=19

第三方补丁、解决方案和示例可在此处找到:

http://people.unica.it/bart/ksoap2-patch/

看看这些链接,你可以在上面找到你的答案。

谢谢。

票数 2
EN

Stack Overflow用户

发布于 2011-11-10 14:15:58

序列化为JSON或XML,将JSON或XML发送到服务器,在服务器上解码该JSON或XML。不确定在您的案例中将使用的确切方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8075626

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档