Android如何从Web服务向sql插入数据 - 获取错误有效的soap操作需要

1 android soap web-services .net-4.0

我试过跟踪每个谷歌如何做到这一点,但我收到错误

android无法在没有有效动作参数的情况下处理请求.请提供有效的肥皂行动

我的网络服务完成是.net 4.0我有一个在端口9022上的asmx,这是我的asmx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;


namespace MyFirstWebService
{
    /// <summary>
    /// Summary description for Math
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Math : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public int Add(int a, int b)
        {
            return (a + b);
        }

        [WebMethod]
        public System.Single Subtract(System.Single A, System.Single B)
        {
            return (A - B);
        }

        [WebMethod]
        public System.Single Multiply(System.Single A, System.Single B)
        {
            return A * B;
        }

        [WebMethod]
        public System.Single Divide(System.Single A, System.Single B)
        {
            if (B == 0)
                return -1;
            return Convert.ToSingle(A / B);
        }

        [WebMethod]
        public void InsertComment(string value)
        {
            SqlParameter sqlParameter = new SqlParameter();
            sqlParameter.ParameterName = "NewComment";
            sqlParameter.Value = value;
            List<SqlParameter> sqlParam = new List<SqlParameter>();
            sqlParam.Add(sqlParameter);
            SQLOperations.executeStoredProcedure("InsertNewComment", sqlParam);

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace MyFirstWebService
{
    public class SQLOperations
    {
        public static bool checkSQLInjection(string sql)
        {
            bool isSafe = true;
            return isSafe;

        }

        private static SqlConnection webServiceConnection()
        {
            SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["SQL"].ConnectionString);
            sqlCon.Open();
            return sqlCon;

        }

        private static SqlCommand sqlCmd(string cmdName, SqlConnection sqlCon, List<SqlParameter> sqlParameters)
        {



            // 1.  create a command object identifying the stored procedure
            SqlCommand _sqlCmd = new SqlCommand(cmdName, sqlCon);

            // 2. set the command object so it knows to execute a stored procedure
            _sqlCmd.CommandType = CommandType.StoredProcedure;

            // 3. add parameter to command, which will be passed to the stored procedure
            if (sqlParameters != null)
                for (int i = 0; i < sqlParameters.Count; i++)
                {
                    _sqlCmd.Parameters.Add(sqlParameters[i]);
                }

            return _sqlCmd;

        }

        public static void executeStoredProcedure(string cmdName,List<SqlParameter> sqlParameters)
        {

            using (SqlConnection conn = webServiceConnection())
            {


                SqlCommand cmd = sqlCmd(cmdName, conn, sqlParameters);


                cmd.ExecuteNonQuery();


            }

        }

    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的MainActivity.java

package com.teachingperiod.android.testwebservice;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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

import java.net.Proxy;


public class MainActivity extends ActionBarActivity {


    private String TAG = "PGGURU";
    private static String celcius;
    private static String fahren;
    Button b;
    TextView tv;
    EditText et;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Celcius Edit Control

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private String NAMESPACE = "http://example.net:9020/";

    private String SOAP_ACTION = "http://example.net:9020/InsertComment";

    private String METHOD_NAME = "InsertComment";

    private String URL="http://example.net:9020/Math.asmx?";

    Object  resultRequestSOAP = null;
    public void onClick(View v) {
        new Thread() {
            @Override
            public void run() {//Create request
                try {

                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

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

                //Declare the version of the SOAP request
                SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);


                //Needed to make the internet call
                HttpTransportSE androidHttpTransport = getHttpTransportSE();

                    //this is the actual part that will call the webservice
                androidHttpTransport.call(SOAP_ACTION, envelope);

                    // Get the SoapResult from the envelope body.
                resultRequestSOAP = (Object) envelope.getResponse();

                } catch (Exception e) {
                    Log.w("myApp", e.getMessage());
                    Log.w("myApp", e.getCause());

                }



            }
        }.start();
    }

    private final SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request) {
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        envelope.bodyOut = request;
        return envelope;
    }

    private final HttpTransportSE getHttpTransportSE() {
        HttpTransportSE ht = new HttpTransportSE(Proxy.NO_PROXY, URL,60000);
        return ht;
    }


}
Run Code Online (Sandbox Code Playgroud)

这是我的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.teachingperiod.android.testwebservice" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

</manifest>
Run Code Online (Sandbox Code Playgroud)

这是我的activity_mail.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="Tell me your new comment"
        android:textSize="30dp" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:singleLine="true" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:text="Insert Comment"
        android:onClick="onClick"/>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="" android:textSize="26dp"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
Create Database WebServiceDB;

USE [WebServiceDB]
GO

/****** Object:  Table [dbo].[tblComments]    Script Date: 5/27/2015 10:54:01 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[tblComments](
    [CommentId] [int] IDENTITY(1,1) NOT NULL,
    [Comment] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_tblComments] PRIMARY KEY CLUSTERED 
(
    [CommentId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
Run Code Online (Sandbox Code Playgroud)

这是我wsdl的肥皂信息

POST /Math.asmx HTTP/1.1
Host: example.net
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://example.net/InsertComment"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <InsertComment xmlns="http://example.net/">
      <value>string</value>
    </InsertComment>
  </soap:Body>
</soap:Envelope>

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <InsertCommentResponse xmlns="http://example.net/" />
  </soap:Body>
</soap:Envelope>

SOAP 1.2

The following is a sample SOAP 1.2 request and response. The placeholders shown need to be replaced with actual values.

POST /Math.asmx HTTP/1.1
Host: example.net
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <InsertComment xmlns="http://example.net/">
      <value>string</value>
    </InsertComment>
  </soap12:Body>
</soap12:Envelope>

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <InsertCommentResponse xmlns="http://example.net/" />
  </soap12:Body>
</soap12:Envelope>
Run Code Online (Sandbox Code Playgroud)

任何帮助都会很精彩!

int*_*ika 5

调试和解决方案:

解决方案1:

您声明I have an asmx that is on port 9022但在您的代码中您使用的是9020

private String NAMESPACE = "http://example.net:9020/";
private String SOAP_ACTION = "http://example.net:9020/InsertComment";
private String URL="http://example.net:9020/Math.asmx?";
Run Code Online (Sandbox Code Playgroud)

解决方案2:

2.1编辑代码:

除掉 "?" 来自您的URL变量

private String URL="http://example.net:9020/Math.asmx?";
Run Code Online (Sandbox Code Playgroud)

2.2编辑代码:

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
Run Code Online (Sandbox Code Playgroud)

代替 SoapEnvelope.VER12

2.3编辑代码:

SoapObject resultRequestSOAP = null;resultRequestSOAP = (SoapObject) envelope.getResponse();

而不是Object resultRequestSOAP = null;resultRequestSOAP = (Object) envelope.getResponse();

2.4编辑代码:

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,60000);
Run Code Online (Sandbox Code Playgroud)

代替 HttpTransportSE androidHttpTransport = getHttpTransportSE();

解决方案3:

我编辑并尝试了这个修改后的代码版本:

private String URL="http://example.net:9020/Math.asmx";

//...

//Object  resultRequestSOAP = null;
public void onClick(View v) {
    new Thread() {
        @Override
        public void run() {//Create request

            //Variables 
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            //Use this to add parameters - reactivate this line bellow to match your needs after testing this at first 
            //request.addProperty("NewComment", "cell");
            //Could also try the following instead of the line bellow //SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12); //envelope.dotNet = true; //envelope.setOutputSoapObject(request);
            SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,60000);

            //if does not work you may decommand the line bellow
            //androidHttpTransport.debug = true;

            try {

                androidHttpTransport.call(SOAP_ACTION, envelope);
                SoapObject resultRequestSOAP = (SoapObject)envelope.bodyIn;

            } catch (Exception e) {
                Log.w("myApp", e.getMessage());
                Log.w("myApp", e.getCause());

            }

        }
    }.start();
}
Run Code Online (Sandbox Code Playgroud)

代替

private String URL="http://example.net:9020/Math.asmx?";

//...

    Object  resultRequestSOAP = null; //this line should not be here
    public void onClick(View v) {
          //...
    }
Run Code Online (Sandbox Code Playgroud)

解决方案4:

检查/重置您的Web服务asmx和端口9022.