gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:11:59 25 4
gpt4 key购买 nike

我已经尝试按照每个谷歌搜索如何执行此操作,但我收到错误

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

我的 web 服务是 .net 4.0 我有一个 asmx 端口 9022,这是我的 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

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


}

这是我的 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

<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 中的 soap 信息

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>

任何帮助都会很棒!

最佳答案

调试与解决方案:

解决方案 1:

你说我有一个在端口 9022 上的 asmx 但在你的代码中你使用的是 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:

2.1 将您的代码编辑为:

删除“?”来自您的 URL 变量

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

2.2 将您的代码编辑为:

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

代替 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);

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

代替

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

//...

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

解决方案 4:

检查/重置您的网络服务 asmx 和端口 9022。

关于Android 如何从 web 服务将数据插入 sql - 需要获取错误有效的 soap 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30496402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com