我是 android 开发的新手,正在努力创建一个可以使用我的 wcf 服务的 android 客户端。下面是我的 wcf 服务接口。我正在为这两个函数返回一个硬编码的值。WCF 服务正在使用 BasicHttpBinding。
[ServiceContract]
public interface IAgent
{
[OperationContract]
int TestMethod();
[OperationContract]
string TestMethodParameter(int key);
}
下面是android客户端代码
package com.example.test;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity
extends Activity
implements OnClickListener
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
private static final String TEST_METHOD = "IAgent/TestMethod";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://odchistorian.cloudapp.net/HistorianAgent.svc";
private static final String SOAP_ACTION1 = NAMESPACE + TEST_METHOD;
Button connectButton = null;
EditText editBox = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connectButton = (Button) this.findViewById(R.id.button1);
editBox = (EditText) this.findViewById(R.id.editText1);
connectButton.setOnClickListener(this);
StrictMode.setThreadPolicy(policy);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View arg0) {
SoapObject request = new SoapObject(NAMESPACE,TEST_METHOD);
final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
final HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION1, envelope);
SoapPrimitive response = null;
response = (SoapPrimitive) envelope.getResponse();
String resultData= response.toString();
editBox.setText(resultData);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
堆栈跟踪 (LogCat)
12-07 17:19:20.244: W/System.err(1180): java.lang.IllegalArgumentException: size <= 0
12-07 17:19:20.261: W/System.err(1180): at java.io.BufferedInputStream.<init>(BufferedInputStream.java:94)
12-07 17:19:20.271: W/System.err(1180): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:226)
12-07 17:19:20.271: W/System.err(1180): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:114)
12-07 17:19:20.281: W/System.err(1180): at com.example.test.MainActivity.onClick(MainActivity.java:103)
12-07 17:19:20.281: W/System.err(1180): at android.view.View.performClick(View.java:4202)
12-07 17:19:20.291: W/System.err(1180): at android.view.View$PerformClick.run(View.java:17340)
12-07 17:19:20.291: W/System.err(1180): at android.os.Handler.handleCallback(Handler.java:725)
12-07 17:19:20.291: W/System.err(1180): at android.os.Handler.dispatchMessage(Handler.java:92)
12-07 17:19:20.301: W/System.err(1180): at android.os.Looper.loop(Looper.java:137)
12-07 17:19:20.301: W/System.err(1180): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-07 17:19:20.311: W/System.err(1180): at java.lang.reflect.Method.invokeNative(Native Method)
12-07 17:19:20.311: W/System.err(1180): at java.lang.reflect.Method.invoke(Method.java:511)
12-07 17:19:20.321: W/System.err(1180): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-07 17:19:20.321: W/System.err(1180): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-07 17:19:20.331: W/System.err(1180): at dalvik.system.NativeStart.main(Native Method)
每次运行此代码时,我总是在“androidHttpTransport.call(SOAP_ACTION1,envelope);”行得到 IllegalArgumentException。我浏览了几个博客,但没有运气。无法得到究竟缺少什么。请帮忙。