gpt4 book ai didi

.net - 调用 .net webservice 时出现问题?

转载 作者:行者123 更新时间:2023-11-30 04:35:49 24 4
gpt4 key购买 nike

我创建了一个示例 android 应用程序,即调用简单的 .net webservice 并在 TextView 中显示,下面是代码前景:

网络服务代码:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 Service : System.Web.Services.WebService
{
public Service () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string MobileApp(string acNum)
{
return "Account number is"+acNum;
}

}

安卓应用程序代码:

import java.io.IOException;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;


public class AccountDetails extends TabActivity {
/** Called when the activity is first created. */
private static final String SOAP_ACTION = "http://tempuri.org/MobileApp";

private static final String METHOD_NAME = "MobileApp";

private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://localhost:58817/MobService/Service.asmx";
private TextView tv;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accountdet);
tv=(TextView)findViewById(R.id.tView);
ServiceCall();
}
public void ServiceCall()
{
try{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
request.addProperty("acNum", "123456");

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);


androidHttpTransport.call(SOAP_ACTION, envelope);

SoapObject result = (SoapObject)envelope.getResponse();
tv.setText(result.toString());

}
catch (final IOException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

} catch (final XmlPullParserException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

} catch (final Exception e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}


}

我还在 list 文件中添加了以下行:

<uses-permission android:name="android.permission.INTERNET" ></uses-permission>
<uses-sdk android:minSdkVersion="7" />

但是我没有在 TextView 中得到结果,所以任何人都可以帮助我。

@nagaraju。

最佳答案

首先检查您的服务是否正在运行——为此请在浏览器中尝试使用您的链接 link ,然后创建虚拟站点,在那里调用你的方法 MobileApp,如果它返回那么你的服务工作完美。

  SoapSerializationEnvelope envelopes = new SoapSerializationEnvelope(SoapEnvelope.VER11); // put all required data into a soap//// envelope
envelopes.dotNet = true;
envelopes.setOutputSoapObject(requestData);
AndroidHttpTransport httpTransport = new AndroidHttpTransport(APPURL);
httpTransport.debug = true;

try {
httpTransport.call(SOAP_ACTION1, envelopes);
responsesData = (SoapPrimitive) envelopes.getResponse();

} catch (SocketException ex) {
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

试试这个..

关于.net - 调用 .net webservice 时出现问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6872219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com