Frage

I am sending request to using NSMutableURLRequest but not getting the response can anyone suggest me the proper way to do that.

War es hilfreich?

Lösung

The code below is the solution.

-(void)setName:(NSString *)name withValue:(NSString *)value onBody:(NSMutableData *)body{
NSString *boundary = @"---------------------------14737809831466499882746641449";
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",name] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[value dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}



-(BOOL)SetRequest: (NSString *)offerID UserID: (NSString*) userID imageData:(NSData *) imageData RespData: (NSDictionary **)respData {

NSMutableData *body=[NSMutableData data];

//NSArray *paramNames = [NSArray arrayWithObjects:@"offerid", @"userid", nil];
//NSArray *paramDatas = [NSArray arrayWithObjects:offerID, userID, nil];

//NSDictionary *dict = [[NSDictionary alloc] initWithObjects:paramDatas forKeys:paramNames];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"API URL"]]];

NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];


[self setName:@"userid" withValue:userID onBody:body];

[self setName:@"offerid" withValue:offerID onBody:body];

// Title text parameter

NSString *fileName = [NSString stringWithFormat:@"%@_%@.jpg",userID,offerID];
// NSData *uploadData = imageData;

//uploadData = UIImageJPEGRepresentation([UIImage imageWithData:imageData], 1.0f);

[self setName:@"parameterName" withFileName:fileName withValue:imageData onBody:body];


[request setHTTPBody:body];
[request setHTTPMethod:@"POST"];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];


NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

SBJsonParser *parser = [[[SBJsonParser alloc] init] autorelease];
NSDictionary *dic = (NSDictionary *)[parser objectWithString:returnString error:nil];
if (dic == nil)
    return NO;  // invalid JSON format

[dic retain];
*respData = dic;
return YES;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top