Android Studio is used to call. NET Webservice.

Source: Internet
Author: User

Android Studio is used to call. NET Webservice.

I am. NET was born, but the leaders had to make research on the Android shell and then prepare html5. When the experimental soldiers were really pitfall, but there was no way we had to study it, simply failing to live up to the expectations of the Leadership (I have been studying for two or three days), I finally figured out that the app's local storage and Android Studio were calling Webservice, and there were several problems in the middle, baidu has not solved my problem for a long time, and all of their answers are copied and pasted without testing at all. I am very angry if they can be pasted out. The following is to make it easier for new users and those who want to study it, share your problems with how to solve them so that you can get a deeper impression.

If any is incorrect, please do not spray it. @ me!

1. First, I did a simple webserivce and can debug it, so I created an empty Website:

The name can be obtained at will.

2. It is best to create a web form so that you have an initial page at the time of release, so that there will be no initial Page.

Write down the text: initial Index

3. Create a WebService: to facilitate testing, just return a few words:

The above is the creation of C # VS webservice, and the following is the main play: Start of Android Studio

Before writing code, you need to download and import the jar tool package.

Google for Android platform development Web Service client provides a ksoap2-android project, but this project is not directly integrated in the Android platform, but also need developers to download. To add a ksoap2-android to the Android app, follow these steps:

1.1 log on to the logon Terminal. (Goodbye, Google Code, hello, GitHub)

The latest version is 3.6.1. I downloaded 3.3.0.

1.2 switch to Project panel

  

Copy and paste the ksoap2JAR package to the app-> libs directory.

  

Right-click Add as Library ..., In the pop-up dialog box, OK.

  

 

 

<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Tools: context = "com. example. user. app_webservice3.MainActivity">
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Hello World! "
Android: id = "@ + id/textView"/>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "display"
Android: id = "@ + id/button"
Android: layout_below = "@ + id/textView"
Android: layout_alignParentLeft = "true"
Android: layout_alignParentStart = "true"
Android: layout_marginTop = "109dp"/>
</RelativeLayout>
Shows the design interface:

4. MainActivity. java
// Add @ SuppressLint ("NewApi") to the network ")
Protected void onCreate (Bundle savedInstanceState ){
// Allow the use of webervice to enable network access at the same time
StrictMode. ThreadPolicy policy = new StrictMode. ThreadPolicy. Builder (). permitAll (). build ();
StrictMode. setThreadPolicy (policy );
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
QueryButton = (Button) findViewById (R. id. button );
QueryButton. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
GetRemoteInfo ();
}
});}
Public void getRemoteInfo (){
// NameSpace String nameSpace = "http://tempuri.org /";
// Call the method name String methodName = "HelloWorld ";
// EndPoint String endPoint = "http: // 192.168.16.39: 1215/WebService. asmx ";
// SOAP Action String soapAction = "http://tempuri.org//HelloWorld ";
// Specify the WebService namespace and the method name to call
SoapObject rpc = new SoapObject (nameSpace, methodName );
// Set the parameters mobileCode and userId that need to be passed in to call the WebService Interface
// Rpc. addProperty ("mobileCode", phoneSec );
// Rpc. addProperty ("userId ","");
// Generate the SOAP request information that calls the WebService method, and specify the SOAP version
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (SoapEnvelope. VER12 );
Envelope. bodyOut = rpc;
// Set whether to call the WebService developed by dotNet.
Envelope. dotNet = true;
(New kerberalbase64 (). register (envelope );
// Equivalent to envelope. bodyOut = rpc;
Envelope. setOutputSoapObject (rpc );
HttpTransportSE transport = new HttpTransportSE (endPoint );
Transport. debug = true;
Try {
// Call WebService
Transport. call (soapAction, envelope );
If (envelope. getResponse ()! = Null ){
System. out. println (envelope. getResponse ());
Result = String. valueOf (envelope. getResponse ());
Toast. makeText (MainActivity. this, result, Toast. LENGTH_SHORT). show ();
} Catch (Exception e) {e. printStackTrace ();}
Error summary:

When webserivce is called, an error is returned: the server fails to recognize the SOAPAction value of the HTTP header: http://tempuri.org/helloworld/......ellipsis.

Set the value based on the Local webservice SOAP version of the request:
If it is a request and response example of SOAP 1.2, SoapEnvelope also needs to be changed: 12. If not, try 11 or 10;
Another error: android. OS. NetworkOnMainThreadException: The exception generated by the Network request in MainThread.
The reason is that network access cannot be used at the same time in the onCreate () method.
According to the search reason, starting from Honeycomb SDK (3.0), google no longer allows network requests (HTTP, Socket) and other related operations to be directly in the Main Thread class. In fact, this should not have been done,
Network operations directly on the UI thread will block the UI and the user experience is rather bad!
Now my code has solved this problem so that it can be called without a thread. A simple test is acceptable. If you are in a large project, please query it in detail. I will not say much about it.

 
 
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.