android 连接 asp.net webservice 简单记录
之前一直也沒有做過(guò)android的項(xiàng)目? java也是幾年前學(xué)過(guò)? 早忘記了??
但是用到的時(shí)候還是得學(xué)學(xué)? 所以對(duì)于android 就更加的是一個(gè)初學(xué)者了
只是針對(duì)于遇到的幾個(gè)問(wèn)題做個(gè)解釋
環(huán)境是這樣的:
Eclipse: 最新的最好
JDK: 1.7
Android SDK: 22.62
KSoap2:??? ksoap2-android-assembly-2.6.5-jar-with-dependencies.jar?? ,?? ksoap2-android.jar
至于為什么貼出來(lái)兩個(gè)KSoap2的jar包 其實(shí)呢 我也只是遇到一個(gè)問(wèn)題 需要兩個(gè)? 后續(xù)會(huì)說(shuō)明
創(chuàng)建自己的asp.net的 webservice
我們可以看到 在4.0的時(shí)候 是沒有asp.net web 服務(wù) 這個(gè)項(xiàng)目類型的? 其實(shí)4.0也是可以建立的 只不過(guò)要建立一個(gè)asp.net的空網(wǎng)站 但是在這里就不說(shuō)了 我們直接選擇4.0以下的版本 是有這個(gè)的 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using TestClass;[WebService(Namespace = "http://lizq.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請(qǐng)取消對(duì)下行的注釋。 [System.Web.Script.Services.ScriptService]public class Service : System.Web.Services.WebService {public Service () {//如果使用設(shè)計(jì)的組件,請(qǐng)取消注釋以下行 //InitializeComponent(); }[WebMethod]public string LoginUser(UserModel model){return new LoginService().LoginUser(model);}}
對(duì)于 傳入的參數(shù) UserMode? 是我們自己建立的一個(gè)對(duì)象類
最后就是把webservice?部署到IIS
我們來(lái)查看一下部署后的信息
?
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button loginButton=(Button)findViewById(R.id.button1);nameText=(EditText)findViewById(R.id.editText1);passText=(EditText)findViewById(R.id.editText2);StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork() // or .detectAll() for all detectable problems .penaltyLog().build());StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());loginButton.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stub Login();}});if (savedInstanceState == null) {getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();}}?
protected void Login(){String nameSpace = "http://lizq.org/";String methodName = "LoginUser";String soapAction = "http://lizq.org/LoginUser";String url = "http://192.168.1.42:5179/Service.asmx?WSDL";//后面加不加那個(gè)?wsdl參數(shù)影響都不大//建立webservice連接對(duì)象org.ksoap2.transport.HttpTransportSE transport = new HttpTransportSE(url,30000);transport.debug = true;//是否是調(diào)試模式//設(shè)置連接參數(shù)SoapObject soapObject = new SoapObject(nameSpace, methodName);UserModel user=new UserModel();user.setProperty(0, nameText.getText().toString());user.setProperty(1, passText.getText().toString());PropertyInfo pi = new PropertyInfo();pi.setName("model");pi.setValue(user);pi.setType(user.getClass());soapObject.addProperty(pi);//將自定參數(shù)加入請(qǐng)求對(duì)象中//設(shè)置返回參數(shù)SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);//soap協(xié)議版本必須用SoapEnvelope.VER11(Soap V1.1)envelope.dotNet = true;//注意:這個(gè)屬性是對(duì)dotnetwebservice協(xié)議的支持,如果dotnet的webservice 不指定rpc方式則用true否則要用falseenvelope.bodyOut = soapObject;//envelope.setOutputSoapObject(soapObject);//設(shè)置請(qǐng)求參數(shù)envelope.addMapping(nameSpace, "UserModel", user.getClass());//傳對(duì)象時(shí)必須,參數(shù)namespace是webservice中指定的, name是服務(wù)器類型的名稱, claszz是自定義類的類型try {transport.call(soapAction, envelope);Object sb = (Object)envelope.bodyIn;//服務(wù)器返回的對(duì)象存在envelope的bodyIn中Object us= (Object)envelope.getResponse();//直接將返回值強(qiáng)制轉(zhuǎn)換為已知對(duì)象if(us.toString()==""){}} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();} catch (XmlPullParserException e) {// TODO Auto-generated catch block e.printStackTrace();} catch(Exception ex){ex.printStackTrace();}return ;}?
由于時(shí)間問(wèn)題? 不寫了??
?
遇到問(wèn)題 去這幾個(gè)網(wǎng)站
http://blog.csdn.net/xiaochunyong/article/details/7765338
http://blog.csdn.net/java_chuan/article/details/6617776
http://blog.csdn.net/zjtbetter/article/details/12890831
http://my.eoe.cn/iceskysl/archive/4382.html
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/lizhiqiang/p/3630068.html
總結(jié)
以上是生活随笔為你收集整理的android 连接 asp.net webservice 简单记录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 利民保小孩子有必要保吗
- 下一篇: 小贷款平台不上征信的