Initializes the URL to an OC string object.
NSString * urlstring = @ "Http://image.zcool.com.cn/56/13/1308200901454.jpg";
NSString * newurlstring = [urlstring stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
Building a Network URL object
Nsurl * URL = [Nsurl urlwithstring:newurlstring];
Create a network request
Nsurlrequest * request = [Nsurlrequest Requestwithurl:url cachepolicy:nsurlrequestreloadignoringlocalcachedata TIMEOUTINTERVAL:10];
Set up Proxy
[Nsurlconnection connectionwithrequest:request delegate:self];
Obey the agreement
@interface Viewcontroller () <NSURLConnectionDataDelegate>
@end
To implement the Proxy method:
triggered when a server response is received
-(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response
{
Receive File size
length = [Response expectedcontentlength];
NSLog (@ "%lld", length);
When a server response is received, space is opened for data, and then the server returns
Self.data = [Nsmutabledata data];
}
Triggered when a server returns data, possibly a resource fragment (the method repeats at this time)
-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data
{
Stitching data
[Self.data Appenddata:data];
CGFloat rate = self.data.length * 1.0/LENGTH;
NSLog (@ "%g", rate);
}
triggered when the server returns all data. Data return complete
-(void) connectiondidfinishloading: (nsurlconnection *) connection
{
self.data;//is all the data returned by the server
Analytical
Nsdictionary * dic = [nsjsonserialization JSONObjectWithData:self.data options:nsjsonreadingmutablecontainers Error: NIL];
NSLog (@ "%@", DIC);
At this point the data has been fully obtained, if the data is Tableviewcell set the contents of the cell, you need to refresh the cell
[TableView Reloaddata];
}
triggered when a server connection error occurs
-(void) connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error
{
NSLog (@ "%d", 4);
}
Asynchronous GEi (2) thread