Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.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 使用NSURLSession的服务器没有响应_Ios_Objective C_Nsurlsession - Fatal编程技术网

Ios 使用NSURLSession的服务器没有响应

Ios 使用NSURLSession的服务器没有响应,ios,objective-c,nsurlsession,Ios,Objective C,Nsurlsession,我已经使用NSURLSession将我的应用程序与服务集成,我的问题是,当我使用NSURLConnection向服务器发送请求时,它会工作,但当我使用NSURLSession向服务器发送请求时,我会得到null响应。请告知 我的字典: NSURL连接: NSURL会议: 我也面临同样的问题,当我更改代码中的HTTPHeaderField值时,我得到了响应 以下是我的示例代码: NSString *strReqParameters = [NSString stringWithFormat:@"us

我已经使用
NSURLSession
将我的应用程序与服务集成,我的问题是,当我使用
NSURLConnection
向服务器发送请求时,它会工作,但当我使用
NSURLSession
向服务器发送请求时,我会得到
null
响应。请告知

我的字典: NSURL连接: NSURL会议:
我也面临同样的问题,当我更改代码中的
HTTPHeaderField
值时,我得到了响应

以下是我的示例代码:

NSString *strReqParameters = [NSString stringWithFormat:@"user_email=your_email_id&method=your_methodname"];

NSURL *url = [NSURL URLWithString:@"Your_URL"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];

[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];

NSData *postData = [strReqParameters dataUsingEncoding:4];

[request setHTTPBody:postData];

NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

    //response handler code here
}];

[task resume];

您是否尝试像上面那样设置请求参数?我正在使用一个字符串作为请求参数..这里的服务器需要NSDictionary数据,那么我如何像上面所说的那样发送响应?当我使用NSurlSession时,响应会出现,但当我使用NSurlSession时,此空响应来自服务器,我不知道您的服务器端代码。。您可以检查它,或者您可能需要在服务器端添加更多代码来处理此类请求。您是否使用POSTMAN插件尝试了此操作以查看您的url是否正常工作?
NSString *jsonString = [mainDict JSONRepresentation];

NSError *theError = nil;

NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

NSURL *serviceUrl=[NSURL URLWithString:[NSString stringWithFormat:@"my url"]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:serviceUrl];
[request setValue:jsonString forHTTPHeaderField:@"json"];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData];

NSURLResponse *theResponse =[[NSURLResponse alloc]init];

// [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//  [self performSelector:@selector(activityIndicater) withObject:self afterDelay:3];

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];

NSMutableString *string = [[NSMutableString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"jsonRequest is %@", string);

NSDictionary *jsonDictionaryResponse = [string JSONValue];
NSLog(@"jsonRequest is %@", jsonDictionaryResponse);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"mu url"]
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:15.0];

                                                          cachePolicy:NSURLRequestUseProtocolCachePolicy

                                                       timeoutInterval:60.0];


//[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];

NSData* jsonData = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:nil];

[request setHTTPBody:jsonData];

//[request setHTTPBody:[self encodeDictionary:params]];

//You now can initiate the request with NSURLSession or NSURLConnection, however you prefer. For example, with NSURLSession, you might do:

NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    if (error) {
        NSLog(@"dataTaskWithRequest error: %@", error);
    } else if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
        NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];

        if (statusCode != 200) {
            NSError *parseError;
            id responseObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
            NSLog(@"responseobject is %@",responseObject);

        } else {
            NSError *parseError;
            id responseObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];

            NSLog(@"else condtion");
            if (!responseObject) {
                NSLog(@"JSON parse error: %@", parseError);
            } else {
                NSLog(@"responseobject is %@",responseObject);
                //[self MainService:responseObject];
            }

            //if response was text/html, you might convert it to a string like so:  
            NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"final responseString = %@", responseString);
        }
    }
}];
[task resume];
NSString *strReqParameters = [NSString stringWithFormat:@"user_email=your_email_id&method=your_methodname"];

NSURL *url = [NSURL URLWithString:@"Your_URL"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];

[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];

NSData *postData = [strReqParameters dataUsingEncoding:4];

[request setHTTPBody:postData];

NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

    //response handler code here
}];

[task resume];