机器人如何将数据插入SQL从web服务 - 让错误有效的肥皂所需的措施所需、肥皂、如何将、机器人

由网友(菊外人)分享简介:我已经试过以下每个谷歌如何做到这一点,但我收到错误I've tried following every google on how to do this, but I'm receiving the error 机器人无法处理没有一个有效的操作参数的要求。请提供一个有效的SOAP动作android Unable t...

我已经试过以下每个谷歌如何做到这一点,但我收到错误

I've tried following every google on how to do this, but I'm receiving the error

机器人无法处理没有一个有效的操作参数的要求。请提供一个有效的SOAP动作

android Unable to handle request without a valid action parameter. Please supply a valid soap action

我的web服务做是.NET 4.0中,我有一个ASMX是在端口9022,这是我的ASMX

My webservice was done is .net 4.0 I have an asmx that is on port 9022, here is my 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);

        }
    }
}

和这里是我的方法

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();


            }

        }

    }
}

这是我的MainActivity.java

here is my 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;
    }


}

这是我的Andr​​oidManifest.xml

here is my 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>

和这里是我的activity_mail.xml

and here is my 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>

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

下面是我的肥皂信息从我的WSDL

Here is my soap information from my 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>

任何帮助将是美好的!

Any help would be wonderful!

推荐答案

的解决方案1:的

Debug & Solutions :

Solution 1 :

您说我有一个ASMX是在端口9022 但在你的code使用的是9020

You stated I have an asmx that is on port 9022 but in your code you are using 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?";

的解决方案2:的

Solution 2 :

2.1编辑您的code到:

2.1 Edit your code to :

删除?从您的网址变量

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

2.2编辑您的code到:

2.2 Edit your code to :

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

,而不是 SoapEnvelope.VER12

2.3编辑您的code到:

2.3 Edit your code to :

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

而不是对象resultRequestSOAP = NULL; resultRequestSOAP =(对象)envelope.getResponse();

2.4编辑您的code到:

2.4 Edit your code to :

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,60000);

而不是 HttpTransportSE androidHttpTransport = getHttpTransportSE()的;

的解决方案3:的

Solution 3 :

我编辑,并试图对你的code这个修改后的版本:

I edited and tried this modified version of your code :

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();
}

而不是的

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

//...

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

的解决方案4:的

Solution 4 :

检查/重置您的Web服务ASMX和端口9022。

Check/Reset your webservice asmx and port 9022.

阅读全文

相关推荐

最新文章