【问题标题】:POST request not receiving all jsonPOST 请求未收到所有 json
【发布时间】:2015-09-18 23:49:48
【问题描述】:

一直困扰着我的小问题。我一直在向我的 AWS RDB 发出 POST 请求。该请求应返回 json 输出。我遇到的问题是我会收到返回的字节,但有时它包含不完整的 json,因此将其转换为字典是行不通的。有时我也会收到接收到的 nsdata 的空值,但我可以打印出数据的长度。有任何想法吗?这是我的 iOS 请求代码:

#import "ServiceConnector.h"

@implementation ServiceConnector{
    NSMutableData *receivedData;
}

-(void)getTest{

    //Send to server
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"MY_WEBSITE"]];

   [request setHTTPMethod:@"GET"];

    //initialize an NSURLConnection  with the request
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if(!connection){
        NSLog(@"Connection Failed");
    }

} 

-(void)postTest:(NSMutableArray *)carSearches{

    //build up the request that is to be sent to the server
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"MY_WEBSITE"]];

    [request setHTTPMethod:@"POST"];

    NSError *writeError = nil; 
    NSData *data = [NSJSONSerialization dataWithJSONObject:carSearches options:NSJSONWritingPrettyPrinted error:&writeError];
    NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
    NSLog(@"JSON Output: %@", jsonString);

    [request setHTTPBody:data]; //set the data as the post body
    [request addValue:[NSString stringWithFormat:@"%lu",(unsigned long)data.length] forHTTPHeaderField:@"Content-Length"];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if(!connection){
        NSLog(@"Connection Failed");
    }
}

#pragma mark - Data connection delegate -
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ // executed when the connection receives data
    if(!receivedData){
        receivedData = [[NSMutableData alloc]init];
        [receivedData appendData:data];
    }
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ //executed when the connection fails

    NSLog(@"Connection failed with error: %@",error);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{

    NSLog(@"Request Complete,recieved %lu bytes of data",(unsigned long)receivedData.length);

    NSString *tmp = [NSString stringWithUTF8String:[receivedData bytes]];
    NSLog(@"%@",tmp);

    NSError *error; 
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[NSData dataWithBytes:[receivedData bytes] length:[receivedData length]] options:NSJSONReadingAllowFragments error:&error];

    [self.delegate requestReturnedData:dictionary];
}

【问题讨论】:

    标签: ios json nsurlconnection nsurl nsurlconnectiondelegate


    【解决方案1】:

    在本节中:

    if(!receivedData){
        receivedData = [[NSMutableData alloc]init];
        [receivedData appendData:data];
    }
    

    仅当对象尚未创建时才附加数据。你想每次都追加。 if 语句应该是这样的:

    if(!receivedData){
        receivedData = [[NSMutableData alloc]init];
    }
    [receivedData appendData:data];
    

    【讨论】:

      猜你喜欢
      • 2021-12-25
      • 1970-01-01
      • 2016-09-02
      • 2018-07-23
      • 2014-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-14
      相关资源
      最近更新 更多