48 votes

Comment envoyer des requêtes POST et GET ?

Je veux envoyer mon JSON vers une URL ( POST et GET ).

 NSMutableDictionary *JSONDict = [[NSMutableDictionary alloc] init];
[JSONDict setValue:"myValue" forKey:"myKey"];

NSData *JSONData = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:nil];

Mon code de demande actuel ne fonctionne pas.

 NSMutableURLRequest *requestData = [[NSMutableURLRequest alloc] init];

[requestData setURL:[NSURL URLWithString:@"http://fake.url/"];];

[requestData setHTTPMethod:@"POST"];
[requestData setValue:postLength forHTTPHeaderField:@"Content-Length"];
[requestData setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestData setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[requestData setHTTPBody:postData];

L'utilisation de ASIHTTPRequest n'est pas une réponse responsable.

1voto

Maulik Salvi Points 269
 -(void)postmethod
    {

        NSString * post =[NSString stringWithFormat:@"Email=%@&Password=%@",_txt_uname.text,_txt_pwd.text];

        NSData *postdata= [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
        NSString *postLength=[NSString stringWithFormat:@"%lu",(unsigned long)[postdata length]];
        NSMutableURLRequest *request= [[NSMutableURLRequest alloc]init];

        NSLog(@"%@",app.mainurl);

       // NSString *str=[NSString stringWithFormat:@"%@Auth/Login",app.mainurl];
        NSString *str=YOUR URL;
        [request setURL:[NSURL URLWithString:str]];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postdata];
        NSError *error;
        NSURLResponse *response;

        NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        NSString *returnstring=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
        NSMutableDictionary *dict=[returnstring JSONValue];

        NSLog(@"%@",dict);

        }

-(void)GETMethod
{
NSString *appurl;
    NSString *temp =@"YOUR URL";
    appurl = [NSString stringWithFormat:@"%@uid=%@&cid=%ld",temp,user_id,(long)clubeid];
    appurl = [appurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:appurl]];
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse: nil error: nil ];
    NSString  *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
    NSMutableDictionary *dict_eventalldata=[returnString JSONValue];
    NSString *success=[dict_eventalldata objectForKey:@"success"];
}

-2voto

sasidharan.M Points 27

voir control.h

 @interface ViewController     UIViewController<UITableViewDataSource,UITableViewDelegate>

  @property (weak, nonatomic) IBOutlet UITableView *tableView;
  @property (strong,nonatomic)NSArray *array;
  @property NSInteger select;
  @end

vue.m

   - (void)viewDidLoad {
 [super viewDidLoad];
 NSString *urlString = [NSString stringWithFormat:    @"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=11.021459,76.916332&radius=2000&types=atm&sensor=false&key=AIzaS yD7c1IID7zDCdcfpC69fC7CUqLjz50mcls"];
 NSURL *url = [NSURL URLWithString: urlString];
 NSData *data = [NSData dataWithContentsOfURL:url];
 NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:      
 data options: 0 error: nil];
 _array = [[NSMutableArray alloc]init];
 _array = [[jsonData objectForKey:@"results"] mutableCopy];
[_tableView reloadData];}
// Do any additional setup after loading the view, typically from a         



 - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
  }
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;
  }

  - (NSInteger)tableView:(UITableView *)tableView
  numberOfRowsInSection:(NSInteger)section {

 return _array.count;
  }

 - (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath {

 static NSString *cellid = @"cell";
 UITableViewCell *cell = [tableView
                         dequeueReusableCellWithIdentifier:cellid];
 cell = [[UITableViewCell
         alloc]initWithStyle:UITableViewCellStyleSubtitle
        reuseIdentifier:cellid];

 cell.textLabel.text = [[_array
 valueForKeyPath:@"name"]objectAtIndex:indexPath.row]; 
 cell.detailTextLabel.text = [[_array 
 valueForKeyPath:@"vicinity"]objectAtIndex:indexPath.row];
 NSURL *imgUrl = [NSURL URLWithString:[[_array
 valueForKey:@"icon"]objectAtIndex:indexPath.row]];  
 NSData *imgData = [NSData dataWithContentsOfURL:imgUrl];
 cell.imageView.layer.cornerRadius =        
 cell.imageView.frame.size.width/2;
 cell.imageView.layer.masksToBounds = YES;
 cell.imageView.image = [UIImage imageWithData:imgData];

 return cell;
 }

 @end

tablecell.h

  @interface TableViewCell : UITableViewCell
 @property (weak, nonatomic) IBOutlet UIImageView *imgView;
 @property (weak, nonatomic) IBOutlet UILabel *lblName;
 @property (weak, nonatomic) IBOutlet UILabel *lblAddress;

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X