2013-03-30 37 views
1

這是我的發送請求到nodejs後端的代碼。從iPhone應用程序Expressjs/Nodejs後端發送JSON發佈數據

CLLocation* location = [locationManager location]; 
CLLocationCoordinate2D coord = [location coordinate]; 
NSMutableURLRequest *request = 
[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://50.63.172.74:8080/points"]]; 
//[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPMethod:@"POST"]; 
NSDictionary* jsonDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:coord.latitude], @"lat", [NSNumber numberWithFloat:coord.longitude], @"lng", nil];//dictionaryWithObjectsAndKeys:coord.latitude, nil] 
NSString *postString; 
NSError *error; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict 
                options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string 
                error:&error]; 

if (! jsonData) { 
    NSLog(@"Got an error: %@", error); 
} else { 
    postString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
} 
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]]; 
(void)[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

使用快遞,我發現了request.body回服務器上,但它看起來是這樣的:

{ '{\n "lat" : 0.0,\n "lng" : 0.0\n}': '' } 

而且由於它回來,因爲我不能隨便說request.body.lat訪問未定義。 我想身體看起來像:

{ "lat":0.0, "lng":0.0} 

如何,使用快遞做任何想法?

+0

當讓你的字典使用的setObject更換0.0:關鍵: http://stackoverflow.com/questions/9483872/iphone-json-message-not正確創建 對不起,我從我的手機提交 – badger0053

回答

4

願這幫助你。

請與您的實際coordiantes

NSArray *keys = [NSArray arrayWithObjects:@"lat", @"lng", nil]; 
     NSArray *objects = [NSArray arrayWithObjects:@"0.0",@"0.0", nil]; 

     NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 
     NSData *jsonData ; 
     NSString *jsonString; 
     if([NSJSONSerialization isValidJSONObject:jsonDictionary]) 
     { 
      jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:nil]; 
      jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]; 
     } 


     NSString *requestString = [NSString stringWithFormat: 
            @"http://50.63.172.74:8080/points"]; 

     NSURL *url = [NSURL URLWithString:requestString]; 
     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
     [request setHTTPMethod:@"POST"]; 
     [request setHTTPBody: jsonData]; 
     [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
     [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"]; 

     NSError *errorReturned = nil; 
     NSURLResponse *theResponse =[[NSURLResponse alloc]init]; 
     NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned]; 

     if (errorReturned) { 
      NSLog(@"Error %@",errorReturned.description); 
     } 
     else 
     { 
      NSError *jsonParsingError = nil; 
      NSMutableArray *arrDoctorInfo = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&jsonParsingError]; 

      NSLog(@"Dict %@",arrDoctorInfo); 
     } 
+0

同意...很好的解釋。 – Siten

0

問題是,在使用NSJSONSerialization獲取包含JSON數據的NSData對象後,您將從該數據創建postString。消除那不必要的步驟,只是:

[request setHTTPBody:jsonData]; 

而你應該在你的服務器端代碼中得到預期的JSON。