Swift sent through HTTP JSON-RPC command

While our Web development,Would inevitably have to obtain data from the Web server,In many cases also need to submit the data to the server,Which at the moment,We are used to using JSON formatted data,Because it is easy,This time we look,How to use Swift to send JSON-RPC command and get response。

JSON-RPC

JSON-RPC is a free and lightweight remote procedure call(RPC)protocol。 This specification defines the data structure and the associated processing rules。It is allowed to run based on socket, Many different messaging environments such as http in the same process。The use ofJSONRFC 4627) As a data format。

When we need to get data from the server when,Either by a URL with information,Or need to use a specific method for submission of information to the server,Common HTTP POST 。

We send JSON requests via HTTP POST to the server object to represent a single RPC call,According to the JSON-RPC rules,We need to pass 4 A basic member:

All right.,An overview of this information,We can try to write a RPC call:

About formatting rules part we introduce so much,For more information, please see the article on JSON-RPC the end of "further reading" links。

Swift's networking requests

In Swift,We use a network request will still be using the Cocoa API,We used NSURLConnection ,Use it sendSynchronousRequest(_:returningResponse:) Approach to network requests,But, unfortunately this method will terminate in OS x 10.11 Got it,So we should try not to use it。

NSURLSession

It was the complete reconstruction of the NSURLConnection ,In iOS 7 Introducing,We use it to replace NSURLConnection ,but,Since reconstruction,Then use a little too different,Now we're going to try to look at using NSURLSession Get the data。

First talk NSURLSession Is not a class,But a factory,It will return a NSURLSessionDataTask ,And we need to call the task .resume() Method to start link,Here we use the NSURLSession of dataTaskWithRequest(_:) Method to produce NSURLSessionDataTask Example。

Here we create a pocketed Blog Home Request ,And from the NSURLSession of sharedSession() Method to get the default configuration NSURLSession Example,And then call the dataTaskWithRequest(_:) Technique,The incoming Request ,Because this stuff is parallel way, we call resume() Let it start now。

…… For a while,So perform as if nothing? Yes, that's right,Because we need to get the JSON object returned by the server,So we're going to give dataTaskWithRequest(_:) Method to append a closure-if you don't know what a closure,It does not matter,Is to pass in a function as a parameter,This function is used to get the data:

According to dataTaskWithRequest(_:) Method declaration:

Function that we will need to receive three parameters,And cannot return a value,So we actually can also be declared as:

Anyway,In accordance with the above code execution,We can see on the Terminal to get pocketed Home the HTML data。

For more data to be sent in,use NSURLRequest Is not enough,Here we are using a NSMutableURLRequest ,It is NSURLRequest A subclass of,With more features,Adding HTTP headers,Add HTTP Body we need to submit information and:

Later we'll submit it to the server。

Swift in JSON

Says,Swift handling of JSON has been disappointing,But we just need a simple RPC call,So it is also not so difficult。

NSJSONSerialization

We pass this object to a JSON object class will be Swift,NSString Swift、NSNumber、NSArray、NSDictionary、NSNull and JSON conversion,Here, we use only NSDictionary Can,Because this is the most convenient。

We just need to write an NSDictionary and then to dataWithJSONObject Method:

Such,We finished with a JSON-RPC command!

Be JSON-RPC remote call via HTTP

Last thing to do is to send it to!

but,The tests you might need to do in the project rather than a Playground,Because in iOS 9 Apple introduces new features after App Transport Security (ATS),Blocks unsafe default HTTP links,If you test the address is HTTPS does not matter,If your address is HTTP,You need to disable this security mechanism。

Disable App Transport Security (ATS)

Find items in your project properties,And then enter the Info tab,Add a NSAppTransportSecurity Typed Dictionary ,Then NSAppTransportSecurity It adds a NSAllowsArbitraryLoads Key,Corresponding values for Boolean Typed YES

If you try to use the CLI project testing,That you could not find Info.plist File ... ... So don't try,Otherwise you will not only not get the desired results,Even the error won't be。

Package type

All right.,Do the preliminary preparation,We'll look at how to package a class to make RPC calls:

First, we write a program called JSONRPC Class,Here write a typed methods post

Method of this type to receive a NSDictionary The actual parameters,It is that we want to send the data;Then the server address,Finally receiving a closure,It is used to create callback,The closures include:

One succeeded For you to judge the success of, msg Returns the HTTP Response,If no wrong,The data Contains the returned JSON。

First our incoming information to prepare the data:

Then we put data into the,While throwing a closure to perform our function to receive based on the results of the function(That is, the callback function)

Anyway,End type method is the:

Test

All right.,Eventually we had to test the,Here I run locally a Twisterd client process,If you don't know what it is ... ... See further reading at the end of the article ... ... Anyway,Send RPC command us to it:

Final,We will get the results:

Next time you manipulate the results:)

Further reading

Swift sent through HttpRequest request data in JSON format

NSURLSession, instructions for use and back-office work flow analysis

IOS9 HTTP does not work solution

(Interpret) JSON-RPC 2.0 Specifications(Chinese version)

 

Original article written by LogStudio:R0uter's Blog » Swift sent through HTTP JSON-RPC command

Reproduced Please keep the source and description link:https://www.logcg.com/archives/1554.html

About the Author

R0uter

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Leave a Reply

Your email address will not be published. Required fields are marked *