android 连接 asp.net webservice 简单记录
之前一直也沒有做過android的項目? java也是幾年前學過? 早忘記了??
但是用到的時候還是得學學? 所以對于android 就更加的是一個初學者了
只是針對于遇到的幾個問題做個解釋
環境是這樣的:
Eclipse: 最新的最好
JDK: 1.7
Android SDK: 22.62
KSoap2:??? ksoap2-android-assembly-2.6.5-jar-with-dependencies.jar?? ,?? ksoap2-android.jar
至于為什么貼出來兩個KSoap2的jar包 其實呢 我也只是遇到一個問題 需要兩個? 后續會說明
創建自己的asp.net的 webservice
我們可以看到 在4.0的時候 是沒有asp.net web 服務 這個項目類型的? 其實4.0也是可以建立的 只不過要建立一個asp.net的空網站 但是在這里就不說了 我們直接選擇4.0以下的版本 是有這個的 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 從腳本中調用此 Web 服務,請取消對下行的注釋。 [System.Web.Script.Services.ScriptService]public class Service : System.Web.Services.WebService {public Service () {//如果使用設計的組件,請取消注釋以下行 //InitializeComponent(); }[WebMethod]public string LoginUser(UserModel model){return new LoginService().LoginUser(model);}}
對于 傳入的參數 UserMode? 是我們自己建立的一個對象類
最后就是把webservice?部署到IIS
我們來查看一下部署后的信息
?
@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";//后面加不加那個?wsdl參數影響都不大//建立webservice連接對象org.ksoap2.transport.HttpTransportSE transport = new HttpTransportSE(url,30000);transport.debug = true;//是否是調試模式//設置連接參數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);//將自定參數加入請求對象中//設置返回參數SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);//soap協議版本必須用SoapEnvelope.VER11(Soap V1.1)envelope.dotNet = true;//注意:這個屬性是對dotnetwebservice協議的支持,如果dotnet的webservice 不指定rpc方式則用true否則要用falseenvelope.bodyOut = soapObject;//envelope.setOutputSoapObject(soapObject);//設置請求參數envelope.addMapping(nameSpace, "UserModel", user.getClass());//傳對象時必須,參數namespace是webservice中指定的, name是服務器類型的名稱, claszz是自定義類的類型try {transport.call(soapAction, envelope);Object sb = (Object)envelope.bodyIn;//服務器返回的對象存在envelope的bodyIn中Object us= (Object)envelope.getResponse();//直接將返回值強制轉換為已知對象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 ;}?
由于時間問題? 不寫了??
?
遇到問題 去這幾個網站
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
?
?
轉載于:https://www.cnblogs.com/lizhiqiang/p/3630068.html
總結
以上是生活随笔為你收集整理的android 连接 asp.net webservice 简单记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利民保小孩子有必要保吗
- 下一篇: 小贷款平台不上征信的