我是Eclipse和Android编程的新手,但我的任务是为我的公司创建一个应用程序,它将连接到我们的SQL Server,以读取一些数据作为概念证明。我已经下载并安装了Eclipse Eplison和Java / Android SDK。我想开始简单,所以我创建了一个带有按钮和视图的应用程序,它构建并且工作正常。然后我在.NET中创建了一个简单的WebService(vs2010),并在我的localhost上运行它。导航到它的工作找到(通过IE),它按计划工作。这是webservice的代码(称为Service1.asmx):
[WebMethod]
public string DisplayText(string TextToDisplay)
{
return TextToDisplay;
}
现在我正在尝试让按钮调用webservice并显示传入的任何文本。这是我在Eclipse中使用的代码:
package com.test.android;
import org.ksoap2.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;
import android.app.*;
import android.os.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class ServiceUI extends Activity {
/** Called when the activity is first created. */
private static final String SOAP_ACTION = "http://localhost/MyWebService/DisplayText";
private static final String MethodName = "DisplayText";
private static final String NameSpace = "http://tempuri.org/"; // "http://localhost/testservice/";
private static final String URL = "http://localhost/MyWebServices/Service1.asmx?wsdl";
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.tvMessage);
Button btn = (Button)findViewById(R.id.btnPress);
btn.setOnClickListener(btnListener);
}
public void CallWebService()
{
try {
SoapObject request = new SoapObject(NameSpace, MethodName);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
request.addProperty("TextToDisplay", "This is coming from android");
envelope.setOutputSoapObject(request);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject)envelope.getResponse();
tv.setText( ""+result);
}
catch (Exception e) {
tv.setText(e.getMessage());
}
}
private OnClickListener btnListener = new OnClickListener()
{
public void onClick(View v)
{
CallWebService();
}
};
}
当我在AVD中运行它时,出现以下错误: localhost / 127.0.0.1:80 - 拒绝连接
任何想法为什么?我在这里看了很多关于asmx和eclipse的链接,但我找不到引用我的“连接被拒绝”错误的链接。
非常感谢任何帮助!