2017-06-03 45 views
1

我想將字符串數據發佈到API,我嘗試使用以下代碼將其發送到服務器。我使用郵遞員檢查了那裏的api,它沒有將字符串數據傳入服務器。我不知道這是什麼問題,需要幫助。如何將字符串數據發佈到API

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:reqURLStr]]; 
    [request setHTTPMethod:@"POST"]; 

**//Pass The String to server** 
NSString *userUpdate =[NSString stringWithFormat:@"service_type=%@&ParcelSize=%@&ReceiverName=%@&MobileNumber=%@&Email=%@&DropOffHub=%@&PickupHub=%@" ,serviceType,pSize,rName,rMobile,rEmail,dropHubID,pickHubID]; 

NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding]; 

[request setHTTPBody:data1]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setValue:[data makeRestAPICall:reqURLStr] forHTTPHeaderField:@"Authorization"]; 


NSError *err; 
NSURLResponse *response; 

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; 

NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding]; 


NSLog(@"got response==%@", resSrt); 
if(resSrt) 
{ 
    NSLog(@"got response"); 
} 
else 
{ 
    NSLog(@"fail to connect"); 
} 
return resSrt; 
+0

你從響應得到了什麼錯誤? –

+0

{狀態碼:400,標題「Cache-Control」=「no-cache」; 「Content-Length」= 0; 日期=「星期六,2017年6月3日06:37:33 GMT」; Expires =「-1」; Pragma =「no-cache」; Server =「Microsoft-IIS/7.5」; 「X-AspNet-Version」=「4.0.30319」; 「X-Powered-By」=「ASP.NET」; } – Nurburgring

+0

我「po迴應」,它告訴我這 – Nurburgring

回答

0

答案很簡單

-(void)postJsonDataToServer{ 
NSDictionary *parameters = @{ 
           @"service_type": serviceType, 
           @"ParcelSize": pSize, 
           @"ReceiverName": rName, 
           @"MobileNumber": rMobile, 
           @"Email" : rEmail, 
           @"DropOffHub" : dropHubID, 
           @"PickupHub" : pickHubID 
           }; 

    NSData *data = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http:/api/order/add"]]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"]; 
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 
    NSURLSessionUploadTask *dataTask = [session uploadTaskWithRequest: request 
                  fromData:data completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
                   if(data != nil) 
                   { 
                    NSError *parseError = nil; 
                    //If the response is in dictionary format 
                    NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError]; 

                    //OR 
                    //If the response is in array format 
                    NSArray *res = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError]; 

                    NSLog(@"The res is - %@",res); 

                   } 
                   else 
                    NSLog(@"Data returned the parameter is nil here"); 
                  }]; 
    [dataTask resume]; 


} 
+0

它現在工作,感謝你的幫助兄弟。 – Nurburgring

+0

歡迎大哥 – user3182143

相關問題