//
Viewcontroller.m
NetWork 1
//
Created by Lenny on 3/21/15.
Copyright (c) Lenny. All rights reserved.
//
#import "ViewController.h"
#import "Mbprogresshud+mj.h"
@interface Viewcontroller () <</span>NSURLConnectionDataDelegate>
@property (Weak, nonatomic) Iboutlet Uitextfield *namefield;
@property (Weak, nonatomic) Iboutlet Uitextfield *pwdfield;
-(ibaction) Loginbtnclick;
@property (Nonatomic,strong) nsmutabledata * RESPONSEDATA;
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
}
-(ibaction) Loginbtnclick {
NSString *username = Self.nameField.text;
NSString * pwd = Self.pwdField.text;
if (Username.length = = 0) {//no user name input
[Mbprogresshud showerror:@ "Enter the user name"];
Return
}
if (Pwd.length = = 0) {
[Mbprogresshud showerror:@ "Enter the pwd"];
Return
}
The bullet box is being logged in ....
[Mbprogresshud showmessage:@ "is desperately loading for you ...."];
2. Send the request to the server (account name and user password)
GET request: Request line \ request Header
2.1 Setting the Request path
NSString * Urlstr = [NSString stringwithformat:@ "http://192.168.1.200:8080/mjserver/login?username=%@&pwd=%@", USERNAME,PWD];
transcoding operation if there is a Chinese character then turn it away
URLSTR = [Urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
The URL can not contain Chinese
Nsurl * url = [Nsurl urlwithstring:urlstr];
2.2 Creating a Request object
Nsmutableurlrequest * request = [Nsmutableurlrequest requestwithurl:url];//default is the way to get requests
Request.timeoutinterval = 10;//Set Request timed out
Send a request note that the request here is an asynchronous request
[Self sendasync:request];
NSLog (@ "request has been issued");
}
-(void) SendAsync: (Nsurlrequest *) request
{
Here the queue is using the main thread
Nsoperationqueue * queue = [Nsoperationqueue mainqueue];
[Nsurlconnection sendasynchronousrequest:request queue:queue completionhandler:^ (NSURLResponse *response, NSData * Data, Nserror *connectionerror) {////When the request is over (the server has been received, the request failed)
Hide the HUD (Refresh UI, be sure to use it in the main thread, and not use it in a sub-thread)
[Mbprogresshud Hidehud];
if (data) {//Request succeeded
Nsdictionary * dict = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves Error:nil];
NSString * error = dict[@ "Error"];
if (error) {//= Login failed
[Mbprogresshud Showerror:error];
}else
{
[Mbprogresshud showsuccess:dict[@ "Success"];
}
}else{
[Mbprogresshud showerror:@ "network busy, please try again later ..."];
}
}];
}
-(void) SENDASYNC2: (Nsurlrequest *) request
{
nsurlconnection * conn = [nsurlconnection connectionwithrequest:request delegate:self];
[Conn start];//Asynchronous execution begins
}
#pragma mark-nsurlconnectiondatadelegate
-(void) connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error
{
NSLog (@ "Connection:didfailwitherror");
[Mbprogresshud Hidehud];
[Mbprogresshud showerror:@ "network busy, please try again later"];
}
-(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response
{
NSLog (@ "Connection:didreceiveresponse");
Initializing the data tells the container that you can receive the data and get the data ready.
Self.responsedata = [Nsmutabledata data];
}
-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data
{
NSLog (@ "Connection:didreceivedata");
Put the received data into the container
[Self.responsedata Appenddata:data];
}
-(void) connectiondidfinishloading: (nsurlconnection *) connection
{
NSLog (@ "connectiondidfinishloading");
Hide HUD
[Mbprogresshud Hidehud];
Parsing the data returned by the server
Nsdictionary * dict = [nsjsonserialization JSONObjectWithData:self.responseData options:nsjsonreadingmutableleaves Error:nil];
NSString *error = dict[@ "Error"];
if (error) {//Login failed
[Mbprogresshud Showerror:error];
}else{
NSString * success = dict[@ "Success"];
[Mbprogresshud showsuccess:success];
}
}
@end
//
Viewcontroller.m
POST
//
Created by Lenny on 3/21/15.
Copyright (c) Lenny. All rights reserved.
//
#import "ViewController.h"
#import "Mbprogresshud+mj.h"
@interface Viewcontroller ()
@property (Weak, nonatomic) Iboutlet Uitextfield *namefield;
@property (Weak, nonatomic) Iboutlet Uitextfield *pwdfield;
-(ibaction) Loginbtnclick;
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Additional setup after loading the view, typically from a nib.
}
-(ibaction) Loginbtnclick {
NSString *username = Self.nameField.text;
NSString * pwd = Self.pwdField.text;
if (Username.length = = 0) {//no user name input
[Mbprogresshud showerror:@ "Enter the user name"];
Return
}
if (Pwd.length = = 0) {
[Mbprogresshud showerror:@ "Enter the pwd"];
Return
}
The bullet box is being logged in ....
[Mbprogresshud showmessage:@ "is desperately loading for you ...."];
2. Send the request to the server (account name and user password)
GET request: Request line \ request Header
2.1 Setting the Request path
NSString * Urlstr = [NSString stringwithformat:@ "Http://192.168.1.200:8080/LKServer/login"];
transcoding operation if there is a Chinese character then turn it away
URLSTR = [Urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
The URL can not contain Chinese
Nsurl * url = [Nsurl urlwithstring:urlstr];
2.2 Creating a Request object
Nsmutableurlrequest * request = [Nsmutableurlrequest requestwithurl:url];//default is the way to get requests
Request. HttpMethod = @ "POST";//How to make a POST request
Tell the server client type by the request header
[Request setvalue:@ "IOS" forkey:@ "user-agent"];
Set the request body
NSString * param = [nsstring stringwithformat:@ "username=%@&pwd=%@", username, pwd];
Request. Httpbody = [param datausingencoding:nsutf8stringencoding];
Request.timeoutinterval = 5;//Set Request timed out
Send a request note that the request here is an asynchronous request
Send data request
Nsoperationqueue * queue = [Nsoperationqueue mainqueue];
[Nsurlconnection sendasynchronousrequest:request queue:queue completionhandler:^ (NSURLResponse *response, NSData * Data, Nserror *connectionerror) {////When the request is over (the server has been received, the request failed)
Hide Hud (Refresh UI, be sure to put it on the main thread, not on child threads)
[Mbprogresshud Hidehud];
if (data) {//Request succeeded
Nsdictionary * dict = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves Error:nil];
NSString * error = dict[@ "Error"];
if (error) {//Login failed
[Mbprogresshud Showerror:error];
}else
{
[Mbprogresshud showsuccess:dict[@ "Success"];
}
}else{
[Mbprogresshud showerror:@ "network busy, please try again later"];
}
}];
}
@end
IOS Network programming GET and POST