大家好我是android的新手,试图通过webservice从android保存sql server中的一些数据。 我在android和webservice中创建了一个示例应用程序。 我收到异常,但我无法解决java中的异常。 这是我正在使用的代码。
这是我正在使用的网络服务。 工作正常。
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; }
}
}
}
}
这是MainActivity.java文件。 我在这里得到一个例外
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>
任何人都可以帮助我。 怎么了? 这是正确的方法吗? 如何解决此错误?
答案 0 :(得分:0)
我用于.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());
答案 1 :(得分:0)
检查你的jar文件是否是ksoap2-android-assembly-2.4-jar-with-dependencies.jar
而不是ksoap2-j2me-core-2.1.2.jar
文件添加到你的android项目中。