您好,我在过去 5 个小时里一直在尝试解决这个问题,现在我的 Web 服务可以正常工作,直到我最近安装了 Windows 8 并设置了 eclipse 和 android SDK。
我的问题是我添加了外部 jar ksoap 并为测试创建了一个示例 Web 服务。
调用 Soap 响应异常时创建
“11-01 11:10:00.161: E/dalvikvm(304): 找不到类 'org.ksoap2.serialization.SoapObject', 引用自方法 com.example.testweb.MainActivity 。发送”
下面是我的mainActvity.xml的 xml 代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="194dp"
android:text="TextView" />
</RelativeLayout>
MainActivity.class
package com.example.testweb;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://10.0.2.2:34253/Service1.asmx";
private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
private static final String METHOD_NAME = "HelloWorld";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Send();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void Send()
{
SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, 5000);
try
{
androidHttpTransport.call(SOAP_ACTION, envelope);
// SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
//Hello World; }
String result = resultsRequestSOAP.toString();
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
}
catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
AndroidMenifest.XML
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testweb"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Ksoap.jar 的屏幕截图
我在 localhost 上运行的ASP.NET 3.5 Webservice代码
/// <summary>
/// Summary description for Service1
/// </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 Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
我正在使用 google api 8,我的 ADT 是 20.0.3,我的 ksoap.jar 是 2.6.5
编辑 伙计们,我的 Jar 问题现在解决了,我收到的一个新错误出现在此链接上
androidHttpTransport.call(SOAP_ACTION, envelope);
try catch 中的异常给了我这个错误
org.xmlpull.v1.XmlPullParserException: expected: START_TAG {schemas.xmlsoap.org/soap/envelope}Envelope (position:START_TAG <HTML>@2:7 in java.io.InputStreamReader@44ef85f0)
提前致谢