-2

I need to send a POST request to a website to load more results of the page (There is about 200 results that I want to parse (using TFHpple) but the page is showing just 40 and you need to click "Show More Results" at the button to load more).

I tried to search the web how to send a POST request and I tried but it hasn't succeeded :( Can anyone help me figure it out?

General information:

Remote Address:212.117.156.55:80

Request URL:http://www.d.co.il/DOTNET/ajax/GetMoreResults

Request Method:POST

Status Code:200 OK

File Name: GetMoreResults

If there is any additional information required, just write in and I'll give you it.

Thank you very much!

Am I done this right?

NSURL *url=[NSURL URLWithString:@"http://www.d.co.il/SearchResults/?query=%D7%A9%D7%95%D7%A4%D7%A8%D7%A1%D7%9C&type=tag&location="];
NSData *data=[NSData dataWithContentsOfURL:url];

TFHpple *parser=[TFHpple hppleWithHTMLData:data];

NSString *XpathQueryString=@"//p[contains(@class, \"result-contact-address\")]";

NSArray *Nodes=[parser searchWithXPathQuery:XpathQueryString];



NSError *error;

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *postUrl = [NSURL URLWithString:@"http://www.d.co.il/DOTNET/ajax/GetMoreResults"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:postUrl
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:60.0];

[request addValue:@"GetMoreResults" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"GetMoreResults" forHTTPHeaderField:@"Accept"];

[request setHTTPMethod:@"POST"];
NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"1", @"batchNumber",
                         @"IOS TYPE", @"typemap",
                         nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
[request setHTTPBody:postData];


NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

}];

[postDataTask resume];

int i=1;
for (TFHppleElement *element in Nodes) {
    NSString *address = element.text;
    NSLog(@"%@,%d",address,i);
    i++;
}
Yhper
  • 183
  • 2
  • 14

1 Answers1

1

Step 1 : You need to build a service which takes the batchNumber and feeds you data belonging to that batch. So, initially you would send batchNumber = 0 and server returns you first 40 records.

Step 2 : On UI side, return 1 count more than the count of data records you have in hand to show. That additional count is for the last cell Show More.

Step 3 : On tap on Show More, increase your current batchNumber by 1 and make the server call to get the next batch records.

Step 4 : Continue step 2 & 3 util all records are loaded.

Finally, this is how you load server data using a post request:

NSError *error;

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"[JSON SERVER"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:60.0];

[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];

[request setHTTPMethod:@"POST"];
NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"TEST IOS", @"name",
                     @"IOS TYPE", @"typemap",
                     nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
[request setHTTPBody:postData];


NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

}];

[postDataTask resume];
miken32
  • 42,008
  • 16
  • 111
  • 154
Abhinav
  • 37,684
  • 43
  • 191
  • 309
  • I understand the code I shared so that portion looks good. I'm not sure as why you are getting data from server, parsing and logging on console. Also, as I suggested, you need to build `batchNumber` logic which is missing here. – Abhinav Oct 26 '15 at 12:25
  • I want to get all the addresses and insert it to an NSMutableArray (here, for example, I'm just printing it in the log). – Yhper Oct 26 '15 at 12:41
  • Where should I insert this batchNumber? Can you give me an example with the information that I provided in the post? Thanks!! – Yhper Oct 26 '15 at 12:42
  • Then thats ok. But add the `batchNumber` logic. – Abhinav Oct 26 '15 at 12:42
  • `batchNumber` should go in one of the key in `mapData ` dictionary. And your server should know about it and send data accordingly. – Abhinav Oct 26 '15 at 12:43
  • Yes - but that value can't be fixed to fun. It has to be dynamic. – Abhinav Oct 26 '15 at 12:53
  • I think this should be the accepted answer now. I've explained more than you originally requested :)! – Abhinav Oct 26 '15 at 12:56
  • Just for the example I typed 1, but It doesn't worked either, it's still the same 40, do you know why? – Yhper Oct 26 '15 at 12:58
  • It shows me half different, one or two addresses are different and the other is the same, why is it? – Yhper Oct 26 '15 at 13:06