我正在尝试在android中创建一个应用程序,通过web服务将数据插入sql server。
大家好,我是安卓新手,我正在尝试通过一个Web服务将安卓的一些数据保存到sql server中。 我已经在android中创建了一个示例应用程序和一个Web服务。 我得到了一个异常,但我无法在java中解决该异常。 这是我正在使用的代码 这就是我正在使用的Web服务。 工作正常我正在尝试在android中创建一个应用程序,通过web服务将数据插入sql server。,android,sql-server,web-services,Android,Sql Server,Web Services,大家好,我是安卓新手,我正在尝试通过一个Web服务将安卓的一些数据保存到sql server中。 我已经在android中创建了一个示例应用程序和一个Web服务。 我得到了一个异常,但我无法在java中解决该异常。 这是我正在使用的代码 这就是我正在使用的Web服务。 工作正常 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Runtime.Serialization;
namespace Common.Services
{
/// <summary>
/// Summary description for AndroidService
/// </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 AndroidService : System.Web.Services.WebService
{
[WebMethod]
public string InsertUserDetails(UserDetails userInfo)
{
string strMessage;
SqlConnection con = new SqlConnection("Data Source=106.205.11.220;Initial Catalog=mydb;User ID=usernm;Password=passwd");
con.Open();
SqlCommand cmd = new SqlCommand("insert INTO android_Sample (SampleName,SampleValue,AddDate) VALUES(@Name,@Value,dateadd(minute,330,getdate()))", con);
cmd.Parameters.AddWithValue("@Name", userInfo.UserName);
cmd.Parameters.AddWithValue("@Value", userInfo.UserAge);
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
strMessage = userInfo.UserName + " Details inserted successfully";
}
else
{
strMessage = userInfo.UserName + " Details not inserted successfully";
}
con.Close();
return strMessage;
}
public class UserDetails
{
string name = string.Empty;
string age = string.Empty;
[DataMember]
public string UserName
{
get { return name; }
set { name = value; }
}
[DataMember]
public string UserAge
{
get { return age; }
set { age = value; }
}
}
}
}
我得到了这个例外
package tecs.example.wsd;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
private static String SOAP_ACTION = "http://tempuri.org/InsertUserDetails";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "InsertUserDetails";
private static String URL = "http://192.168.1.137/common.ui/Services/AndroidService.asmx";
Button btnSend;
EditText txtName, txtVal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSend=(Button)findViewById(R.id.btnSend);
txtName=(EditText)findViewById(R.id.txtText);
txtVal=(EditText)findViewById(R.id.txtValue);
btnSend.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi=new PropertyInfo();
pi.setName("UserName");
pi.setValue(txtName.getText().toString());
pi.setType(String.class);
request.addProperty(pi);
pi=new PropertyInfo();
pi.setName("UserAge");
pi.setValue(txtVal.getText().toString());
pi.setType(String.class);
request.addProperty(pi);
//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION, envelope);
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject)envelope.bodyIn;
if(result != null)
{
Toast.makeText(getBaseContext(), result.getProperty(0).toString(), Toast.LENGTH_SHORT).show();
txtName.setText("");
txtVal.setText("");
}
else
{
Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
java.lang.ClassCastException:org.ksoap2.SoapFault
在这条线上
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject)envelope.bodyIn;
这是我的清单文件代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tecs.example.wsd"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="tecs.example.wsd.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>
</manifest>
这是我的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/txtText"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:gravity="top"
android:inputType="textMultiLine"
android:hint="@string/Text_Hint" />
<EditText
android:id="@+id/txtValue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="@string/Value_Hint" />
<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/btnSend_text" />
</LinearLayout>
这是我的字符串值文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">WSD</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="Text_Hint">Enter Text</string>
<string name="Value_Hint">Enter Value</string>
<string name="btnSend_text">Send</string>
</resources>
有人能帮我吗。
怎么了?
哪种方法是正确的?
如何解决此错误?我用于.Net Web服务的示例代码
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(webRequest);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION + METHOD_NAME, envelope);
Log.d("CheckLogin-SOAP ACTION", SOAP_ACTION + METHOD_NAME);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
Log.d("CheckLogin - Response", response.toString());
status= Boolean.valueOf(response.toString());
检查您的jar文件是否为ksoap2-android-assembly-2.4-jar-with-dependencies.jar,而不是添加到android项目中的ksoap2-j2me-core-2.1.2.jar文件。Waht是WebService for InsertUserDetails的结果,我认为它是布尔变量。或者CountAkshay Joy我检查了WebService响应,我猜是它的字符串。HTTP/1.1 200 OK内容类型:应用程序/soap+xml;charset=utf-8 Content-Length:Length字符串对不起,我没提到。我已经在我的项目中添加了这个jar文件。ksoap2-android-assembly-2.6.5-jar-with-dependencies.jarTry更改SoapObject结果=SoapObjectenvelope.bodyIn;to Object result=envelope.getResponse;我按照你说的更改了代码,但现在它给了我一个异常SoapFault-faultcode:'soap:Server'faultstring:'System.Web.Services.Protocols.SoapException:Server无法处理请求。-->System.NullReferenceException:对象引用未设置为对象的实例。在Common.Services.AndroidService.InsertUserDetailsUserInfo中,我想参数似乎存在一些问题,因为它们没有被发送或使用。请尝试将参数作为字符串发送,以查看数据是否至少到达webmethod。也不要忘记添加result.tostring以获得结果。我尝试了你的代码,但它引发了这个异常。SoapFault-faultcode:'soap:Server'faultstring:'System.Web.Services.Protocols.SoapException:服务器无法处理请求。-->System.NullReferenceException:对象引用未设置为对象的实例。在Common.Services.AndroidService.insertUserDetails用户详细信息中,我猜这些参数没有传递到web服务。嘿,我共享了示例代码来重新记录SAOp响应,告诉我什么是InsertUserDetails的输出。响应就像成功插入的详细信息或未插入的详细信息一样。我问插入成功时InsertUserDetails的结果是什么。您对Soap响应的期望是什么?抱歉。该参数数据被插入到android_示例表中