2012-05-30 46 views
-1

我正在使用POST方法向我們的服務器發送JSON字符串。應該發生的是它應該返回一個顯示「Array(JSON String)Array(JSON String)」的響應。響應包含兩個數組:第一個數組填充如果我使用POST方法,而第二個數組填充如果我使用GET方法。我嘗試通過GET方法發送JSON,實際上,第二個數組已填充。但是,當我嘗試通過POST方法發送JSON時,兩個數組都返回爲空。通過POST發送JSON不起作用 - 目標C

這裏是我使用的代碼:

NSData *requestData = [NSJSONSerialization dataWithJSONObject:sqlArray options:NSJSONWritingPrettyPrinted error:&error];

NSURL *url = [NSURL URLWithString:@"http://myurl/xmlrpc/imwebsrvcjson.php"]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60]; 

[request setHTTPMethod:@"POST"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody:requestData]; 

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

NSString *returnString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding]; 

if (error == nil){ 
    NSLog(@"The Result string: %@", returnString); 
} 

你能告訴我什麼是錯我的代碼?

+0

另一端看到什麼? –

+0

@熱舔我真的不知道,因爲可以訪問服務器的人不在身邊。他只是告訴我,爲了讓我知道POST是否成功,我會收到上面的迴應。 :/ – user1412469

+0

這類似於 http://stackoverflow.com/questions/4456966/how-to-send-json-data-in-the-http-request-using-nsurlrequest –

回答

-2

這就是我所做的(請注意,去我的服務器的JSON需要是一個包含一個值(另一個字典)的字典,用於key = question..ie {:question => {dictionary}}):

NSArray *objects = [NSArray arrayWithObjects:[[NSUserDefaults standardUserDefaults]valueForKey:@"StoreNickName"], 
    [[UIDevice currentDevice] uniqueIdentifier], [dict objectForKey:@"user_question"],  nil]; 
NSArray *keys = [NSArray arrayWithObjects:@"nick_name", @"UDID", @"user_question", nil]; 
NSDictionary *questionDict = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 

NSDictionary *jsonDict = [NSDictionary dictionaryWithObject:questionDict forKey:@"question"]; 

NSString *jsonRequest = [jsonDict JSONRepresentation]; 

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

NSURL *url = [NSURL URLWithString:@"https://xxxxxxx.com/questions"]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
      cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 


NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]]; 

[request setHTTPMethod:@"POST"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody: requestData]; 

NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 
if (connection) { 
receivedData = [[NSMutableData data] retain]; 
} 
The receivedData is then handled by: 

NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
NSDictionary *jsonDict = [jsonString JSONValue]; 
NSDictionary *question = [jsonDict objectForKey:@"question"]; 

這是不是100%清楚,並會採取一些重新閱讀,但一切都應該在這裏讓你開始。據我所知,這是異步的。進行這些調用時,我的用戶界面沒有被鎖定。希望有所幫助。

+0

請編輯您的答案並對代碼進行格式化以使其可讀。 – kleopatra

+0

謝謝..當然我會.. –