1

我正在创建包含聊天机制的 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 是 NSDictionary,它包含键值对中的数据。

代码有什么问题吗?

同样在 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 ,其中包含我必须发送到服务器的数据。

4

1 回答 1

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;
}

这个方法可能对你有帮助

于 2013-09-10T05:10:40.137 回答