ksoap2-android + .NET Web Service
February 10 2011, 9:41pm
ASP.NET上のWebサービスをAndroidから使ってみるテスト。 いろいろハマったので、メモ。
で、Android側はksoap2-android 2.5.2を使用します。
■ksoap2-androidの準備
こちらからjarをダウンロードして、Javaのビルドパスに追加 http://code.google.com/p/ksoap2-android/
■インターネット接続を許可
AndroidManifest.xmlに以下の行を追加。
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
■Webサービス側の実装(C#)
[WebMethod] public SearchResult SearchProduct(SearchCondition searchCondition);
こんなメソッドをAndroidから呼び出してみる。
■AndroidからSOAPでWebサービスを実行するサンプル(Java)
private static final String SOAP_ACTION = "http://tempuri.org/SearchProduct"; private static final String METHOD_NAME = "SearchProduct"; private static final String NAMESPACE = "http://tempuri.org/"; private static final String URL = "http://yourdomain/path/AndroidService.asmx"; private static final String SEARCH_CONDITION = "SearchCondition"; private static final String DEBUG = "debug";
public SearchProduct(SearchCondition condition) { try { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// 呼出し元のコードを掲載するのがメンドイので、ここで設定しちゃってます
condition = new SearchCondition(1,"","TEST",0,10,1);
// メソッドの引数を設定
request.addProperty(SEARCH_CONDITION, condition);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
// AndroidHttpTransportは非推奨
HttpTransportSE transport = new HttpTransportSE(URL);
transport.call(SOAP_ACTION, envelope);
SoapObject searchResult = (SoapObject)envelope.getResponse();
} catch (Exception e) { Log.v(DEBUG, e.getMessage()); } }
WebMethodの引数となるSearchConditionオブジェクトは、こんな感じ。
public class SearchCondition implements KvmSerializable { private int searchDiv; private String categoryCode; private String searchKeyword; private int rankID; private int pageSize; private int pageIndex;
public SearchCondition(){}
public SearchCondition(int searchDiv, String categoryCode, String searchKeyword, int rankID, int pageSize, int pageIndex) { this.searchDiv = searchDiv; this.categoryCode = categoryCode; this.searchKeyword = searchKeyword; this.rankID = rankID; this.pageSize = pageSize; this.pageIndex = pageIndex; }
// KvmSerializableで要求されるインターフェイスを実装 public Object getProperty(int arg0) { switch(arg0) { case 0: return searchDiv; case 1: return categoryCode; case 2: return searchKeyword; case 3: return rankID; case 4: return pageSize; case 5: return pageIndex; default: return null; } }
public int getPropertyCount() { return 6; }
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) { switch(index) { case 0: info.type = PropertyInfo.INTEGER_CLASS; info.name = "SearchDiv"; break; case 1: info.type = PropertyInfo.STRING_CLASS; info.name = "CategoryCode"; break; case 2: info.type = PropertyInfo.STRING_CLASS; info.name = "SearchKeyword"; break; case 3: info.type = PropertyInfo.INTEGER_CLASS; info.name = "RankID"; break; case 4: info.type = PropertyInfo.INTEGER_CLASS; info.name = "PageSize"; break; case 5: info.type = PropertyInfo.INTEGER_CLASS; info.name = "PageIndex"; break; } }
public void setProperty(int index, Object value) { switch(index) { case 0: this.searchDiv = Integer.parseInt(value.toString()); break; case 1: this.categoryCode = value.toString(); break; case 2: this.searchKeyword = value.toString(); break; case 3: this.rankID = Integer.parseInt(value.toString()); break; case 4: this.pageSize = Integer.parseInt(value.toString()); break; case 5: this.pageIndex = Integer.parseInt(value.toString()); break; } } }
戻り値の処理は端折っていますが、メソッドのパラメータや戻り値がプリミティブな型ではなく、クラスだったりすると、かなり面倒臭くなります。普通にRESTなインターフェイスのほうがいいですね。
- Tags:
- Programming

