我正在學習如何使用創建Android應用程序和互聯網服務: 的Eclipse IDE ksoap2版本2.6.5具有依賴性 Visual Studio 2010中的Android ksoap2微軟web服務 - 無法將參數傳遞給web服務
我的方法是遵循這個偉大的教程:http://www.youtube.com/watch?v=v9EowBVgwSo&feature=related
該教程適用於w3schools測試web服務。
然後我試圖寫我自己的web服務,並使用Visual Studio中提供的helloworld webservice來啓動我。我推薦我的代碼指向新的web服務(發佈在我的開發筆記本電腦上),並以調試模式在手機上運行它。
調用HelloWorld()webservice時,與webservice的通信很好。它甚至從simpleMethod()返回一個結果,但它不包含我通過它的參數。例如當調用simpleMethod時,Iam返回了以下字符串:「你好」
我認爲這是一個web服務的問題,因爲它適用於w3schools測試web服務,但我堅持,並希望得到一些幫助。
我的代碼如下:
的Webservice:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WMCMobileWebService
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://testuri.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";
}
[WebMethod]
public string simpleMethod(String srt)
{
return "Hello " + srt;
}
}
}
Android應用MainActivity:
package com.clcltd.soaptest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class SoapTestActivity extends Activity {
private static final String SOAP_ACTION = "http://testuri.org/simpleMethod";
private static final String METHOD_NAME = "simpleMethod";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://192.168.2.22/wmcmobilewebservice/Service1.asmx";
PropertyInfo pi;
TextView tv, tvc;
String str;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
tvc = (TextView) findViewById(R.id.textView2);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
str = "Andy";
pi = new PropertyInfo();
pi.setName("srt");
pi.setValue(str);
pi.type = PropertyInfo.STRING_CLASS;
request.addProperty(pi);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(URL);
try{
aht.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive resultString = (SoapPrimitive) soapEnvelope.getResponse();
tv.setText("Status: " + resultString);
tvc.setText(request.toString());
}catch (Exception e)
{
e.printStackTrace();
tv.setText("Error" + e.toString());
}
}
}
當運行上面的代碼,我得到我的屏幕上輸出如下:
身份:您好 simpl eMethod {SRT =安迪; }
我希望的結果是:
狀態:您好安迪
任何想法?
您是否嘗試用SOAP客戶端(如SOAPuI)調用此服務?什麼是SAOP響應? – OguzOzkeroglu
還沒有,但在您的建議下現在下載SOAPUI。我確實使用了內置的webservice測試工具,它成功返回「Hello Andy」。但是,這是在開發筆記本電腦上本地執行的,而不是從android模擬器/手機中執行的。 – agrodude
使用SOAPUI,我從webservice獲得了成功的結果。 – agrodude