Asynchronous GEi (2) thread

Source: Internet
Author: User

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

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.