在ios中向服务器发送数据时出现问题

时间:2013-09-09 12:51:33

标签: iphone ios asihttprequest

我正在创建包含聊天机制的ios和android应用程序。

当我向服务器发送消息时,我在ios应用程序中遇到了问题。有时由ios设备发送的消息会在服务器数据库中重复输入。它发生在后面。

我正在使用ASIFormDataRequest类与ios中的服务器进行通信。

我的Android应用程序也使用相同的API与服务器通信。 Android应用正常运行

在ios中发送消息的代码如下。

 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:urlString];

 [request setRequestMethod:@"POST"];

 for (NSString* key in dictionaryWithValue) {

 [request setPostValue:[dictionaryWithValue objectForKey:key] forKey:key];

 }

 [request setTimeOutSeconds:60];

 [request setDelegate:self];

 [request setShouldContinueWhenAppEntersBackground:YES];

  request.shouldAttemptPersistentConnection   = NO;

  [request startSynchronous];

这里,dictionaryWithValue是NSDictonary,它包含键值对中的数据。

代码中出现了什么问题吗?

同样在ios应用程序中,图像不会上传到服务器。它会让连接超时。

为了上传图片,我使用了以下代码

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request setRequestMethod:@"POST"];
[request setTimeOutSeconds:120];
[request setShouldContinueWhenAppEntersBackground:YES];
[request setUploadProgressDelegate:self];
[request setShouldAttemptPersistentConnection:YES];


 [request setData:[parametersPOST objectAtIndex:2] withFileName:@"UserAvatarFile.png" andContentType:@"image/png" forKey:KEYAvatar];

[request startSynchronous];


  NSString *fileName = [parametersPOST objectAtIndex:1];

 [request setData:[parametersPOST objectAtIndex:2] withFileName:fileName   andContentType:@"image/jpg" forKey:KEYAvatar];


 [request setPostValue:[parametersPOST objectAtIndex:2] forKey:@"avatar"];


[request addPostValue:[parametersPOST objectAtIndex:2] forKey:@"avatar"];

[request setPostValue:[parametersPOST objectAtIndex:0] forKey:@"userid"];
[request setPostValue:fileName forKey:@"name"];
[request setPostValue:[parametersPOST objectAtIndex:3] forKey:@"token"];


[request startSynchronous]; 

这里的parametersPOST是NSArray,它包含我必须发送给服务器的数据。

1 个答案:

答案 0 :(得分:0)

+(NSDictionary *)getParsedDictonaryforUrlString:(NSString *)urlString withData:(NSData *)postData
{    
    NSString *newURL = urlString;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:newURL]                                                          cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    [request setHTTPBody: postData];

    // get response
    NSHTTPURLResponse *urlResponse = nil;
    NSError *error = [[NSError alloc] init];

    NSData *responseData = [NSURLConnection sendSynchronousRequest:request
                                                 returningResponse:&urlResponse
                                                             error:&error];
    if (responseData == nil) 
    {
        // Check for problems
        if (responseData != nil) 
        {

        }
    }
    else 
    {
        // Data was received.. continue processing
        NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];  
        NSLog(@"Response code: %d", [urlResponse statusCode]);

        if ([urlResponse statusCode] >=200 && [urlResponse statusCode] <300)
        {
            NSLog(@"Response ==> %@", result);
            NSError *parseError = nil;
            NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:result error:parseError];     
            return xmlDictionary;
        }
    }

    return nil;
}

此方法可能对您有所帮助