HTTP Request with nested dictionary - iphone

I need to access a web service with a POST method, with nested dictionary to receive relevant data. But I keep getting empty response. What am I doing wrong?
NSURL *url = [NSURL URLWithString:#"http://appzander.com/gateway.php"];
ASIFormDataRequest *_request = [ASIFormDataRequest requestWithURL:url];
__weak ASIFormDataRequest *request = _request;
AppDelegate *app = [[UIApplication sharedApplication] delegate];
NSDictionary *paramsDic = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithDouble:app.myLocation.longitude],#"longitude",
[NSNumber numberWithDouble:app.myLocation.latitude],#"latitude",
#"500000",#"radius",
#"1000",#"maxResults",
nil];
NSDictionary *requestDic = [[NSDictionary alloc] initWithObjectsAndKeys:
#"GetNearestStations",#"function",
#"false",#"debug",
paramsDic,#"params",
nil];
[request addRequestHeader:#"Content-Type" value:#"application/json"];
[request appendPostData:[[SBJsonWriter new] dataWithObject:requestDic]];
request.requestMethod = #"POST";
[request setDelegate:self];
[request setCompletionBlock:^{
NSData *responseString = [request responseData];// responseString];
NSLog(#"Response: %#", responseString);
[self plotCrimePositions:responseString];
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(#"Error: %#", error.localizedDescription);
}];
// 6
[request startAsynchronous];

Use ASIFormDataRequest
NSURL *url = [NSURL URLWithString:#"yourUrlhere"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request setPostValue:email forKey:#"username"];
[request setPostValue:pasw forKey:#"pass"];
[request setDelegate:self];
[request startAsynchronous];

Solved- I wasn't sending the parameters properly:
NSURL *url = [NSURL URLWithString:#"http://appzander.com/gateway.php"];
ASIFormDataRequest *_request = [ASIFormDataRequest requestWithURL:url];
__weak ASIFormDataRequest *request = _request;
AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSDictionary *paramsDic = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithDouble:32.071738],#"longitude",
[NSNumber numberWithDouble:34.791295],#"langitude",
#"500000",#"radius",
#"1000",#"maxResults",
nil];
[request setPostValue:#"false" forKey:#"debug"];
[request setPostValue:#"GetNearestStations" forKey:#"function"];
SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];
NSString *paramsDicJSON = [jsonWriter stringWithObject:paramsDic];
[request setPostValue:paramsDicJSON forKey:#"params"];
request.requestMethod = #"POST";
[request setDelegate:self];
[request setCompletionBlock:^{
NSString *responseString = [request responseString];
[self plotBikes:responseString];
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(#"Error: %#", error.localizedDescription);
}];
[request startAsynchronous];

Related

How to show UIProgressView with ASIFormDataRequest

I want to show uiprogress bar with ASIFormDataRequest. I googled it but i can't find regarding uiprogressView with ASIFormDataRequest, I just find uiprogressView with NSURLConnection. just let me know if it is possible.
// So this my method where i upload the data.
-(NSMutableArray*)getUploadVideo:(NSString*)titleName userID:(NSString*)userId token:(NSString*)tokenID description:(NSString*)desc videoID:(NSString*)video_id privateOrPublic:(NSString*)privacyID pathOfVideoFile:(NSString*)path mode:(NSString*)potLand;
{
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:#"http://%#/aircastdev/RestServices/fileUpload"],kId];
[request setDelegate:self];
[request setDidFailSelector:#selector(uploadFailed:)];
[request setDidFinishSelector:#selector(uploadFinished:)];
[request setDidFinishSelector:#selector(requestFailed:)];
[request setDidFinishSelector:#selector(requestFinished:)];
[request setPostValue:titleName forKey:#"title"];
[request setPostValue:userId forKey:#"user_id"];
[request setPostValue:tokenID forKey:#"token"];
[request setPostValue:desc forKey:#"desc"];
[request setPostValue:video_id forKey:#"video_id"];
[request setPostValue:privacyID forKey:#"is_private"];
[request setPostValue:potLand forKey:#"mode"];
NSString *url = [[NSURL fileURLWithPath:path] path];
[request setFile:url withFileName:[NSString stringWithFormat:#"%#.mov",titleName] andContentType:#"video/MOV" forKey:#"file"];
[request setTimeOutSeconds:50000];
[request setRequestMethod:#"POST"];
// EDITED LINE
[request setUploadProgressDelegate:myProgressView];
[request startSynchronous];
SBJSON *sbJason = [[SBJSON alloc] init];
getUploadArray = [sbJason objectWithString:self.response];
return getUploadArray;
}
// when upload is done this method call.
- (void)requestFinished:(ASIHTTPRequest *)request
{
// alert: request is successfully sent
self.response = [request responseString]; //stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
use this:
[request setDownloadProgressDelegate:progressView];
or
[request setUploadProgressDelegate:myProgressIndicator];
Where progressView is a UIProgressView
also have a look here

Sending Mp4 Video to PHP server via ASIFormDataRequest

Here I am sending the file path or file with some parameters. But the server didn't get the video. So any problem in that code? Or anything you want to add something. Then please tell me. Any delegate method you want to tell me where I get error or setting in response .
NSString *path = [[NSBundle mainBundle] pathForResource:#"hangover" ofType:#"mp4"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:#"http://156.75.28.172:52/aircas/RestServices/fileUpload"]];
[request setPostValue:filename forKey:#"name"];
[request setPostValue:#"GUnit" forKey:#"title"];
[request setPostValue:#"133" forKey:#"user_id"];
[request setPostValue:#"8953d0e1c97ef83c9f0aff47" forKey:#"token"];
[request setPostValue:#"video song" forKey:#"desc"];
[request setPostValue:#"34" forKey:#"video_id"];
[request setPostValue:#"0" forKey:#"is_private"];
[request setFile:path forKey:#"video"];
[request setTimeOutSeconds:500];
[request setRequestMethod:#"POST"];
[request startSynchronous];
Try this...
NSString *path = [[NSBundle mainBundle] pathForResource:#"hangover" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:path];
NSData *movieData = [NSData dataWithContentsOfURL:fileURL];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:#"http://156.75.28.172:52/aircas/RestServices/fileUpload"]];
[request setPostValue:filename forKey:#"name"];
[request setPostValue:#"GUnit" forKey:#"title"];
[request setPostValue:#"133" forKey:#"user_id"];
[request setPostValue:#"8953d0e1c97ef83c9f0aff47" forKey:#"token"];
[request setPostValue:#"video song" forKey:#"desc"];
[request setPostValue:#"34" forKey:#"video_id"];
[request setPostValue:#"0" forKey:#"is_private"];
[self.request setData:movieData withFileName:#"hangover.mp4" andContentType:#"multipart/form-data" forKey:#"video"];
[request setTimeOutSeconds:500];
[request setRequestMethod:#"POST"];
[request startSynchronous];
Hope it helps.
Add following code in your code and than you can write 2 delegate methods and can handle response.
[request setDelegate:self];
[request setDidFailSelector:#selector(uploadFailed:)];
[request setDidFinishSelector:#selector(uploadFinished:)];
- (void) uploadFailed:(ASIHTTPRequest *)request
{
//Upload succeeded;
}
- (void) uploadFinished:(ASIHTTPRequest *)request
{
//Upload failed;
NSError *error = [request error];
NSLog(#"Error Description-->%#",[error localizedDescription]);
}
To have indepth view for ASIHttpRequest please see documentation. Its very well documented.

Getting an ARC warning for capturing an ojbect strongly

I'm using ARC and getting a warning saying Capturing 'request' strongly in this block is likely to a retain cycle.
__block ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
[request setCompletionBlock:^{
NSString *responseString = [request responseString];
self.appointmentArray = [responseString JSONValue];
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(#"%#", error.description);
}];
I'm assuming request is declared somewhere before the blocks. You need to declare it as __weak, or set a second, weakly-declared variable to it.
This question is similar. Try this:
__block ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
__weak ASIHTTPRequest *request_b = request;
[request setCompletionBlock:^{
NSString *responseString = [request_b responseString];
self.appointmentArray = [responseString JSONValue];
}];
[request setFailedBlock:^{
NSError *error = [request_b error];
NSLog(#"%#", error.description);
}];
Simply replacing:
__block ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
with:
__weak ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
will suffice.

Post Data but GET Method json -ios

how can i use the get method to post data on an api link with 6 values and a json format?
im really fuzzled. im doing this for days . is it best to have 6 textfields that will be used to write the value and a button to indicate post data?
check out my code and please help.
-(IBAction)postDataPressed
{
NSString *urlString = #"http://192.168.18.8/apisample2/friendb.php?fm=jsn";
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:user.text forKey:#"un"];
[request setPostValue:pass.text forKey:#"pd"];
[request setPostValue:gender.text forKey:#"gd"];
[request setPostValue:age.text forKey:#"ag"];
[request setPostValue:status.text forKey:#"st"];
[request setPostValue:lookfor.text forKey:#"lf"];
[request setRequestMethod:#"GET"];
[request setCompletionBlock:^{
NSString *responseString = [request responseString];
NSLog(#"Response: %#", responseString);
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(#"Error: %#", error.localizedDescription);
}];
[request setDidFinishSelector:#selector(requestFinished:)];
[request setDidFailSelector:#selector(requestFailed:)];
[request startAsynchronous];
}
i hope someone will help.
NSString *request_url = [NSString stringWithFormat: #"http://192.168.18.8/apisample2/friendb.php?fm=jsn&un=%#&pd=%#&gd=%#&ag=%#&st=%#&lf=%#",
user.text, pass.text, gender.text, age.text,status.text,lookfor.text];
NSURL *url = [NSURL URLWithString:request_url];
ASIHttpRequest *request = [ASIHttpRequest requestWithURL:url];
request.delegate = self;
....
HTTP Get method only accept data in url, called query string, so you have to build query string yourself.

Iphone ASIHTTP setDidFinishSelector issue

i have the following code:
- (void)imageDownloaded:(ASIHTTPRequest *) request idDisco:(NSNumber *)iddisco
NSLog(#"%d",[idPlace intValue]);
}
And
NSURL *url = [NSURL URLWithString:[item objectForKey:#"image"]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:[self performSelector:#selector(imageDownloaded:idDisco:)withObject:request withObject:disco.id_disco]];
But the compiler tells me that "passing argument 1 of setDidFinishSelector from incompatible pointe type"
It works fine but i don't know what i'm doing wrong.
Thanks
You have to pass #selector(someMethod:) there:
And the signature of the method should be
- (void)methodName:(ASIHTTPRequest *)request;
Look at this example:
- (IBAction)grabURLInTheBackground:(id)sender
{
if (![self queue]) {
[self setQueue:[[[NSOperationQueue alloc] init] autorelease]];
}
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestDone:)];
[request setDidFailSelector:#selector(requestWentWrong:)];
[[self queue] addOperation:request]; //queue is an NSOperationQueue
}
- (void)requestDone:(ASIHTTPRequest *)request
{
NSString *response = [request responseString];
}
- (void)requestWentWrong:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}