In this tutorial we will have a look at the pending
part for creating a web service.
Summary from the past tutorial: We will be creating a
web service in dot net that will be used to do some calculation and post the
result in JSON format, we have already created the web service class and the
response class in the past tutorial and in this tutorial we will be making the
use of it.
Note for iOS developers: Web services are nothing but
methods that we declare and implement in our .h and .m files the only
difference is that they act as a media to pull the data from the server and
present it in a particular format. JSON is nothing but a string representation
of the data that you pull from a server.
For our first step we will have a look at simple web
service which would accept a parameter from the user and return the data
entered by the user in JSON.
Select the .cs file of the web service and just add a function to print the
data entered by the user, for this you should know the syntax of C# and I am
sure if you have a look at the syntax for creating function then it would be a
piece of cake.
Code Explanation: First I am giving memory to the
response class, since it’s the response class that we will be using to print
any kind of success or failure message to the user.
In the second line of code I am having a check whether
the user has entered some data or not and If not then in that case what I am
doing is with the help of the properties in the response class I am notifying
the user that something is pending.
Note: In C# the properties can be accessed with the
help of the DOT operator.
If the user has entered the required filed data then the
code knows what to do and will throw the JSON response accordingly filling in
all the properties from the response class and displaying the appropriate data
to the user.
The final step is to convert the output in JSON format
and to do this I am using the JavaScriptSerializer class which has a unique method
named Serialize to convert an object in JSON format as per the data given in
the object. So again cheers to dot net and Microsoft for such beautiful
function.
Note: The concept is pretty much straight forward:
-
Whatever data has been entered by the user
goes through the validation code.
-
If there are no issues in the validations
I proceed with my normal execution.
-
During the execution process I fill in the
properties of the response class so that later I can encapsulate them in JSON
string.
-
Encapsulation of the JSON string is done
with the help of JavaScriptSerializer class Serialize method which comes from
the framework using System.Web.Script.Serialization.
The last task is to run
the web service and this is done with the help of a small play button that you
can see above, asp.net will automatically create a new development server for your application
and will run your application.
Note: When you are done
with creating the web service do remember to host these web service on your
companies test server so that you can test the services and do stuff and once
you are done then host these services to the live server.
When you hit the run
button you will see a small pop up indicating that the ASP.NET has loaded your
application in its default development server which is used by the dot net developers
for testing the code that they have wrote.
After running the
application you will see that the application will get loaded in your default
web browser (Chrome or Safari etc) and will look something like the one given
in the below image.
From the list of the
service we have to select the web service that we want to test locally and
since we have created the print something web service its time to test the web
service so click on it and you will see a page which would look something like
the one given below
Given below is the
explanation of what you are seeing in the image
Now let’s try to hit the
web service without entering any data in the parameter box and see what kind of
response we get from the service
Response Explanation: As
you can see from the response you have an ErrorCode which is screaming that you
have forgot to enter some data inside the parameter box and all the other
response flags are 0 since they don’t have any data to display.
Now let’s try to hit the
web service by entering some data in the parameter box.
And when you hit the
invoke button then let’s see the response
Response Explanation: As
you can see now the response seems to be bit different from the earlier
response, SuccessFlag is 1 which means that the code has executed successfully
and we have the response value filled with the data that the user has entered
in the parameter box.
So as you can see how
simple the entire scenario was, similarly you can prepare the code for your
calculator service as well.
Given here is the updated code copy of this project with
basic calculator functions, and given below is the snap shot for the same.
If you want to know how
to call these services from iphone when they are successfully hosted in any web
server then you can refer this tutorial of mine which I wrote earlier and if
you are having any sort of queries then do let me know via mails or via
comments.
Join us on facebook iPhone by radix Facebook group.
Join us on facebook iPhone by radix Facebook group.
Until then Happy iCoding
and have a great day.
Comments
Post a Comment