私は以下を使用してAndroidアプリとWebサービスを作成する方法を学んでいます:Eclipse IDE ksoap2バージョン2.6.5と依存関係Visual Studio 2010
私の方法は、この素晴らしいチュートリアルに従うことでした: http://www.youtube.com/watch?v=v9EowBVgwSo&feature=related
このチュートリアルは、w3schools テスト Web サービスで問題なく動作します。
次に、独自の Web サービスの作成を試み、Visual Studio で提供されている helloworld Web サービスを使用して開始しました。新しい Web サービス (開発用ラップトップで公開) を指すようにコードを修正し、ハンドセットでデバッグ モードで実行しました。
HelloWorld() Web サービスを呼び出す場合、Web サービスとの通信は問題ありません。simpleMethod() からの結果も返しますが、渡したパラメーターは含まれていません。たとえば、simpleMethod を呼び出すと、次の文字列が返されました: "Hello "
これは w3schools テスト Web サービスで機能するため、Web サービスの問題だと思いますが、行き詰まっており、助けていただければ幸いです。
私のコードは次のとおりです。
ウェブサービス:
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());
}
}
}
上記のコードを実行すると、画面に次の出力が表示されます。
ステータス: こんにちは simpleMethod{srt=Andy; }
結果は次のようになると思います。
ステータス: こんにちはアンディ
何か案は?