ASIHTTPRequest for dummies

2019-03-21 06:34发布

问题:

I'm COMPLETE DUMMY in ASIHTTPRequest. I have just downloaded the library and added it to my project and now i'm trying to understand basics of working with it. I've found some pretty good example here

this little tutorial is about uploading images to a server. I tried it and things seem to work (but i'm confused with NSString returned by [request responseString], it returned HTML code of a whole web page ) but i would like to know how to download images (or anything uploaded) back from the server. After searching a bit i found this and it gave me no answers but more questions only, cause it's quite unclear for me. Let me enumurate my questions:

1) Could you please provide me with a little piece of code showing how to download data (no matter what kind of data: images,strings,numbers etc) from a server?

2)Should i know php code of the server side in order to work with a server?

3)If yes - how do i know php code of the server side?

4)How important is it if a request is proccessed by a php script or by any different means?

5)What is the role of responseString? Is it ok that it returned me a huge HTML code?

6)The main question. Is there a good tutorial for ASIHTTPRequest? Something like "ASIHTTPRequest for dummies"? I found official documentation here but it explains the stuff really poorly and i just can't find any other tutorials.

Any help is desperately needed.

回答1:

1) Could you please provide me with a little piece of code showing how to download data (no matter what kind of data: images,strings,numbers etc) from a server?

here you can find a very basic POST example:

NSURL *url = [NSURL URLWithString:@"http://localhost/webroot/index.php/test/signUp"];

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:@"Raul" forKey:@"name"];
[request addPostValue:@"Hello World" forKey:@"message"];

[request setCompletionBlock:^{
    NSString *responseString = [request responseString];
    NSLog(@"Response: %@", responseString);
}];
[request setFailedBlock:^{
    NSError *error = [request error];
    NSLog(@"Error: %@", error.localizedDescription);
}];

[request startAsynchronous];

it uses blocks, so that wverything is nicely packed together in one function and you don't have to deal with delegates and the rest. (source)

If you just want to GET a page, you can use:

NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
    NSString *responseString = [request responseString];
    NSLog(@"Response: %@", responseString);
}];
[request setFailedBlock:^{
    NSError *error = [request error];
    NSLog(@"Error: %@", error.localizedDescription);
}];

[request startAsynchronous];

2)Should i know php code of the server side in order to work with a server?

absolutely not necessary.

3)If yes - how do i know php code of the server side?

N.A.

4)How important is it if a request is proccessed by a php script or by any different means?

the language the server is written in is not relevant either.

5)What is the role of responseString? Is it ok that it returned me a huge HTML code?

responseString contains all the data that the server sent back to you as an NSString. It can be huge...

6)The main question. Is there a good tutorial for ASIHTTPRequest? Something like "ASIHTTPRequest for dummies"? I found official documentation here click but it explains the stuff really poorly and i just can't find any other tutorials.

I don't know of a tutorial "for dummies" like you say; you should try and follow the docs on the site; they may appear complex, but are not really.

On another note, you probably already know it: development of ASIHTTP has been ceased. If you are just starting learning about it, you might better think of considering an alternative. Look at the linked page for some alternatives.