2011-07-25 12 views
2

我要發佈按鈕點擊動作代碼通過JSON webservicesmy數據是:後的數據通過JSON web服務在iphone

-(IBAction)login:(id)sender { 
    NSString *newurlString = [NSString StringWithFormat:@"{\"name\":\"asit\",\"email\":\"[email protected]\",\"businessType\":\"1\",\"score\":30}"]; 
    NSString * url = @"http://www.nieuwe-dag.nl/mobile_be/public/?action=saveScore"; 

    NSData *postData = [newurlString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; 

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
    NSLog(@"urlString::%@",newurlString); 
    NSLog(@"postLength::%@",postLength); 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:300]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody:postData]; 

    NSURLConnection *theConnection =[[NSURLConnection alloc] initWithRequest:request delegate:self]; 


    if(theConnection) { 
     webData = [[NSMutableData data] retain]; 
    } 
    else { 
     NSLog(@"theConnection is NULL"); 
    } 
} 

並在控制檯上的反應是:

[會話的開始2011-07-25 12:36:48 +0530。] 2011-07-25 12:36:48.860 DepartmentalStoreAdmin [2490:207]無法加載 「header.png」標識符 「com.easternenterpr ise.dag「2011-07-25 12:36:48.864 DepartmentalStoreAdmin [2490:207]無法加載標籤爲 」com.easternenterprise.dag「2011的叢中的筆尖引用的」leher.png「圖片 -07-25 12:36:50.708 DepartmentalStoreAdmin [2490:207] urlString :: {「name」:「asit」,「email」:「[email protected]」,「businessType」:「1」 ,「分數」:30} 2011-07-25 12:36:50.709 DepartmentalStoreAdmin [2490:207] postLength :: 88 2011-07-25 12:36:52.376 DepartmentalStoreAdmin [2490:207] DONE。接收字節:52 2011-07-25 12:36:52.377 DepartmentalStoreAdmin [2490:207]

THEXML { 「結果」: 「ERROR」, 「錯誤」:[ 「名稱不能爲空」]}

我不知道爲什麼它不是發佈。請任何人幫助我,非常緊迫。

在此先感謝:

回答

3

newurlString必須包含變量名。如果您也添加百分比編碼,這將是確定的。例如:

NSString *newUrlString = [[NSString stringWithFormat:@"&json={\"name\":\"asit\",\"email\":\"[email protected]\",\"businessType\":\"1\",\"score\":30}"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

如果你沒有看到任何變化,在這裏,他們是:

  1. THRE爲&json=在字符串的開頭。將其更改爲您的真實變量名稱。例如。如果您的服務器檢查$_POST['data']中的json,則應該使用&data=
  2. stringByAddingPercentEscapesUsingEncoding這增加百分之逃逸(例如space轉換爲%20

編輯:對不起,這裏是正確的代碼:

NSString *newUrlString = [NSString stringWithFormat:@"&json=%@",[[NSString stringWithFormat:@"{\"name\":\"asit\",\"email\":\"[email protected]\",\"businessType\":\"1\",\"score\":30}"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];