2014-01-13 19 views
-1

我試圖通過json發佈數據到服務器。假設我只有一個名爲username的字段到我的xib中。現在我將這些數據發佈到服務器上。我寫了這段代碼 NSString * uname = txt_name.text;如何在iOS中通過json發佈數據

NSMutableURLRequest *request =[[NSMutableURLRequest alloc] initWithURL: 
[NSURL URLWithString:@"http://mypath/index.php?params=123"]]; 

[request setHTTPMethod:@"POST"]; 

// NSString *postString = @"[email protected]"; 

[request setValue:[NSString 
        stringWithFormat:@"%d", [uname length]] 
forHTTPHeaderField:@"Content-length"]; 

[request setHTTPBody:[uname 
         dataUsingEncoding:NSUTF8StringEncoding]]; 

[[NSURLConnection alloc] 
initWithRequest:request delegate:self]; 

NSLog(@"text",uname); 

但我不知道數據是否張貼或不張貼。我想將我的輸入數據發佈到Xcode的控制檯中,但沒有任何顯示。什麼原因..?發生什麼錯誤..?

回答

0

您需要以下要求:

NSDictionary *dataDict = @{@"uname": <YOUR_UNAME>}; 

NSData *postData = [NSJSONSerialization dataWithJSONObject:dataDict options:0 error:nil]; 

NSMutableURLRequest *request =[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://mypath/index.php?params=123"]]; 

[request setHTTPMethod:@"POST"]; 
[request setHTTPBody:postData]; 
[request setValue:[NSString stringWithFormat:@"%d",postData.length] forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 

爲了保證服務器有你的數據使用

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 
    NSInteger statusCode = httpResponse.statusCode; 
    ........... 
} 

在成功的情況下,你會得到的StatusCode = 200

+0

[request setHTTPBody:postData]; [請求setValue:[NSString stringWithFormat:@「%d」,postData.length] forHTTPHeaderField:@「Content-Length」]; [請求setValue:@「application/json; charset = utf-8」forHTTPHeaderField:@「Content-Type」]; –

+0

錯誤正在進入這三行 –

+0

錯誤是什麼?檢查postData是否不爲零。 – malex

0
NSString *string= [NSString stringWithFormat:@"your Url.php?&Username=%@",username]; 
     NSLog(@"%@",string); 
     NSURL *url = [NSURL URLWithString:string]; 
     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
     [request setHTTPMethod:@"POST"]; 

     NSURLResponse *response; 
     NSError *err; 
     NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; 
     NSLog(@"responseData: %@", responseData); 
     NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
     NSLog(@"responseData: %@", str); 
     NSString *str1 = @"1"; 
     if ([str isEqualToString:str1 ]) 
     { 

      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Successfully" message:@"" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
      [alert show]; 
     } 
     else 
     { 
      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Try Again" message:@"" delegate:self cancelButtonTitle:@"Try Later" otherButtonTitles:@"Call", nil]; 
      alert.tag = 1; 
      [alert show]; 
     } 

不需要使用JSON,你可以在沒有JSON的情況下做到這一點一種愉快的方式!

+0

感謝move ..我正在嘗試這個...讓我們看看它的工作與否 –

+0

我想但代碼不工作。數據未正確提交..再次嘗試顯示在我的Xcode控制檯中。我該怎麼辦。? –

+0

我輸入的代碼相同 –