Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在参数下发送post请求?_Ios_Objective C_Post - Fatal编程技术网

Ios 如何在参数下发送post请求?

Ios 如何在参数下发送post请求?,ios,objective-c,post,Ios,Objective C,Post,我有一些参数可以向服务器发送post请求: `[{"LoginID":151,"UserID":0,"SubUserID":0,"WorkGroupID":92,"WorksFor":"Doctor","UserWorkGroup":0},{"SearchingFilters":{"GroupingOperator":"And","Filters":[{"SearchingValue":"04-13-2016","SearchingName":"AppointmentDate","Searchi

我有一些参数可以向服务器发送post请求:

`[{"LoginID":151,"UserID":0,"SubUserID":0,"WorkGroupID":92,"WorksFor":"Doctor","UserWorkGroup":0},{"SearchingFilters":{"GroupingOperator":"And","Filters":[{"SearchingValue":"04-13-2016","SearchingName":"AppointmentDate","SearchingOperator":"Ge"},{"SearchingValue":"04-27-2016","SearchingName":"AppointmentDate","SearchingOperator":"Le"}],"Groups":[{"Groups":[],"GroupingOperator":"And","Filters":[]}]},"Searching":true,"SortingOrder":"Desc","RecordsCount":10,"PageIndex":0}]`
如何以这种格式发送like

[getProfileServices sendSynchronousPostRequestWithStringForAction:getProfileURL andParameters:[[NSDictionary alloc] initWithObjectsAndKeys:[[NSUserDefaults standardUserDefaults] objectForKey:@"USER_ID"],@"LoginID",@"0",@"UserID",@"0",@"SubUserID",[[NSUserDefaults standardUserDefaults] objectForKey:@"WORK_ID"],@"WorkGroupID",@"Doctor",@"WorksFor",@"0",@"UserWorkGroup",nil] andRequestType:@"POST"];

首先,您需要使用json格式而不是直接作为对象发送请求

第二

我认为有必要改变服务

服务应接受此格式的请求

{
  "LoginID": 151,
  "UserID": 0,
  "SubUserID": 0,
  "WorkGroupID": 92,
  "WorksFor": "Doctor",
  "UserWorkGroup": 0,
  "SearchingFilters": {
    "GroupingOperator": "And",
    "Filters": [
      {
        "SearchingValue": "04-13-2016",
        "SearchingName": "AppointmentDate",
        "SearchingOperator": "Ge"
      },
      {
        "SearchingValue": "04-27-2016",
        "SearchingName": "AppointmentDate",
        "SearchingOperator": "Le"
      }
    ],
    "Groups": [
      {
        "Groups": [

        ],
        "GroupingOperator": "And",
        "Filters": [

        ]
      }
    ]
  },
  "Searching": true,
  "SortingOrder": "Desc",
  "RecordsCount": 10,
  "PageIndex": 0
}

如果您需要将其作为单个对象传递,否则在请求中发送多个对象时将其添加到数组中

@Raghvendra首先在字典中设置用于传递参数的参数,然后设置url
在这里输入代码

-(void)call_WebService
{
    NSMutableDictionary *dicParameter=[[NSMutableDictionary alloc] init];
    [dicParameter setObject:@"" forKey:@"Parameter1"];// setObject - String/Dictionary/Array
    [dicParameter setObject:@"" forKey:@"parameter2"];  // setObject - String/Dictionary/Array
    [dicParameter setObject:@"" forKey:@"Parameter3"];// setObject - String/Dictionary/Array

    NSError *error = nil;
    NSData *data = [NSJSONSerialization dataWithJSONObject: dicParameter options:0 error:&error];
    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    if (error)
        NSLog(@"%s: JSON encode error: %@", __FUNCTION__, error);

    NSURL *url = [NSURL URLWithString:@"web Service url url"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *params = [NSString stringWithFormat:@"json=%@",
                        [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSData *paramsData = [params dataUsingEncoding:NSUTF8StringEncoding];
    [request addValue:@"8bit" forHTTPHeaderField:@"Content-Transfer-Encoding"];
    [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:paramsData];
    NSURLResponse *response = nil;
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if (error)
        NSLog(@"%s: NSURLConnection error: %@", __FUNCTION__, error);
    // examine the response
    NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(@"responseString: %@",responseString);
    if(![responseString isEqualToString:@""])
    {
        NSDictionary *dicFinalGetFacility = [NSJSONSerialization JSONObjectWithData:returnData
                                                                            options:kNilOptions error:&error];
        NSLog(@"dicFinalGetFacility: %@",dicFinalGetFacility);
    }
}

[{“LoginID”:151,“UserID”:0,“SubUserID”:0,“WorkGroupID”:92,“WorksFor”:“Doctor”,“UserWorkGroup”:0},{“SearchingFilters”:{“GroupingOperator”:“And”,“Filters”:[{“SearchingValue”:“04-13-2016”,“SearchingName”:“任命日期”,“SearchingOperator”:“Ge”},{“SearchingValue”:“04-27-2016”,“SearchingName”:“任命日期”,“SearchingOperator”:“Le”}],“Groups”:[{“Groups”:[],“GroupingOperator”:“And”,“Filters”:[]}],“Searching”:true,“SortingOrder”:“Desc”,“RecordsCount”:10,“PageIndex”:0}]如下[getProfileServices sendSynchronousPostRequestWithStringForAction:getProfileURL和参数:[[NSDictionary alloc]InitWithObjects和Keys:[[NSSerDefaults standardUserDefaults]objectForKey:@“用户ID”]、@“登录ID”、@“0”、@“子用户ID”、[[NSUserDefaults standardUserDefaults]objectForKey:@“工作ID”]、@“工作组ID”、@“医生”、@“工作对象”、@“0”、@“用户工作组”、@]nil]和RequestType:@“发布”]@Raghavenderredy plz仔细阅读代码,并告诉我,如果您有任何疑问,-在这段代码中,您的整个数组设置在dicParameter first对象中,然后给出它的参数名,只需输入您的url,还要注意您将分配给整个数组的参数,它将提供给后端进行解析。。