2012-11-14 110 views
0

我試圖調用這些參數的Web服務:IOS HTTP請求發送JSON格式

NSString *user = [NSString stringWithFormat:@"username=%@",_username.text]; 
NSString *pass = [NSString stringWithFormat:@"password=%@",_password.text]; 
NSString *Class = @"Authorization"; 
NSString *method = @"login"; 

//NSString *post = [NSString stringWithFormat:@"%@&%@&%@&%@", user, pass, Class, method ]; 

NSString *jsonPostBody = [NSString stringWithFormat:@"'json' = '{\"Class\":" 
           "\"%@\"" 
           ",\"method\":" 
           "\"%@\"" 
           ",\"pass\":" 
           "\"%@\"" 
           ",\"user\":" 
           "\"%@\"" 
           "}'", 
           [Class stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], 
           [method stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], 
           [pass stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], 
           [user stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
    NSData *postData = [jsonPostBody dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; 

    NSURL *url = [NSURL URLWithString:@"http://212.119.87.45:8080/IktissabServices/index.php/service"]; 
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url 
                  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
                 timeoutInterval:180.0]; 

    [request setHTTPMethod:@"POST"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody:[[jsonPostBody stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding] 
          dataUsingEncoding:NSUTF8StringEncoding 
          allowLossyConversion:YES]]; 

    [request setHTTPBody:postData]; 

    NSString* postDataLengthString = [[NSString alloc] initWithFormat:@"%d", [postData length]]; 
    [request setValue:postDataLengthString forHTTPHeaderField:@"Content-Length"]; 

    NSURLResponse *response; 
    NSError *err; 
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; 
    NSError *error; 
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];// 
    if (! error) { 
     NSLog(@"%@",jsonDict); 
    }else{ 
     NSLog(@"%@",error.localizedDescription); 
    } 

每次

{"Class":"Authorization","method":"login","user":"","pass":""} 

如下結果:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'

網絡服務有:

input-content-type: text/json

output-content-type: text/json

任何幫助或例子將不勝感激。

+0

錯誤是什麼? – woz

+0

當通過登錄測試並調用web服務時,語句會出現終止應用程序,由於未捕獲的異常'NSInvalidArgumentException',原因:'數據參數爲零' –

+0

它返回給你結果?如何和它從我身邊終止親切送我結果 –

回答

0

我沒有收到與下面的代碼的任何異常(不過顯然我沒有訪問您的服務器,所以我不能測試它最終到終端):

NSDictionary *dictionary = @{ 
    @"Class" : @"Authorization", 
    @"method" : @"login", 
    @"pass" : _password.text, 
    @"user" : _username.text 
}; 

NSError *error; 
NSData *postData = [NSJSONSerialization dataWithJSONObject:dictionary 
                options:0 
                error:&error]; 
if (error) 
    NSLog(@"Failure to serialize JSON object %@", error); 

NSURL *url = [NSURL URLWithString:@"http://212.119.87.45:8080/IktissabServices/index.php/service"]; 
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url 
                 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
                timeoutInterval:180.0]; 

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

NSURLResponse *response; 

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
if (error) 
    NSLog(@"%s sendSynchronousRequest error %@", __FUNCTION__, error); 

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];// 
if (!error) { 
    NSLog(@"%@",jsonDict); 
} else { 
    NSLog(@"%s JSONObjectWithData error %@", __FUNCTION__, error); 
} 

或者如果你沒有使用最新的Xcode和沒有進入詞典的現代Objective C的定義,你可以明顯地定義你的字典的傳統方式:

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
          @"Authorization", @"Class", 
          @"login",   @"method", 
          _password.text, @"pass", 
          _username.text, @"user", 
          nil]; 

艾迪T:

jsonPostBody推斷,此字典條目可能需要:

NSDictionary *dictionary = @{ 
    @"json" : @{ 
     @"Class" : @"Authorization", 
     @"method" : @"login", 
     @"pass" : _password.text, 
     @"user" : _username.text 
    } 
}; 

或可能(雖然可能性更小),從你的userpass定義推斷:

NSDictionary *dictionary = @{ 
    @"json" : @{ 
     @"Class" : @"Authorization", 
     @"method" : @"login", 
     @"pass" : [NSString stringWithFormat:@"password=%@",_password.text], 
     @"user" : [NSString stringWithFormat:@"username=%@",_username.text] 
    } 
}; 

這是一個關於如何爲您的服務器格式化JSON的問題。

+0

我試過所有的建議給我錯誤時調用webservices 2012-11-14 20:13:46.852 Iktissab Demo1 [483:11303 ] - [登錄getlogin:] JSONObjectWithData錯誤錯誤域= NSCocoaErrorDomain代碼= 3840「該操作無法完成。(可可錯誤3840.)」(JSON文本沒有開始與數組或對象和選項,以允許片段沒有設置。 )UserInfo = 0x7496d40 {NSDebugDescription = JSON文本沒有開始數組或對象和選項,以允許片段沒有設置。 –

+0

@MohammedAbdelrasoul我無法回答,因爲我不知道如何形成JSON請求您的網絡服務。希望你的服務器團隊可以對此有所瞭解。我已經根據您的代碼示例推斷出了三種不同的請求排列,但我完全不知道您的web服務需要什麼。當我測試時,我也做了一個NSLog(@「responseData =%@」,[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);'所以我可以看到服務器響應的全文,所以也許類似調試可能會幫助你。 – Rob

+0

感謝羅布,但[從NSString * jsonresult = [NSJSONSerialization JSONObjectWithData:responseData選項:NSJSONReadingMutableContainers錯誤:&錯誤]出現的問題; –