2017-08-28 80 views
0

目前,我正在開發一個項目。我正在使用網絡上傳帶有參數的圖像。它在wifi上工作正常,但是如果我使用蜂窩網絡。它沒有正確傳遞參數。就像後臺服務器接收參數的次數一樣。有時會丟失。我使用的代碼是在WiFi的工作參數上傳圖片使用AFnetwork 3多部分獲取零參數

NSString *URLString = [NSString stringWithFormat:@"%@%@",BASEURL,urlID]; // some URL 
NSLog(@"URL : %@ Parameters %@",URLString,postVars); 

AFHTTPSessionManager *uploadManager = [AFHTTPSessionManager manager]; 

[uploadManager POST:URLString parameters:postVars constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { 

    NSLog(@"Parameters %@",postVars); 

    if (profileImageData) { 
     [formData appendPartWithFileData:profileImageData name:@"photo" fileName:@"myimage.jpg" mimeType:@"image/jpeg"]; 
    } 
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 
    NSLog(@"Success %@", responseObject); 
    if ([[responseObject valueForKey:@"status"]isEqualToString:@"success"]) { 
     block(responseObject,nil); 
    } 
    else{ 

     block(responseObject,nil); 
    } 


} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 
    if([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:AFNETWORK_OFFLINE_MESSAGE]) 
    { 
     [alertHelper showAlertwithTitle:NOINTERNET_TITLE details:NOINTERNET_DETAILS]; 
    } 
    block(nil,error); 
}]; 

上傳圖像,但是蜂窩網絡上有時paramenters失蹤AFNetwork。我的後端開發人員通知我,在上傳圖像時不會發送參數。我現在應該做什麼。

回答

0

試試這個

如果你的參數是零的,然後PAAS零代替_params。

NSMutableDictionary *_params=[[ NSMutableDictionary alloc]init]; 
[_params setObject:postsomthingtext.text forKey:@"body"]; 
[_params setObject:userid forKey:@"user_id"]; 


NSData *imageData = UIImageJPEGRepresentation(choosedimg,.6); 
documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
// Here is the file path for the image 
filepath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"imagename.png"]]; 
[imageData writeToFile:filepath options:NSDataWritingAtomic error:nil]; 

AFHTTPRequestOperationManager*operationManager = [AFHTTPRequestOperationManager manager]; 
operationManager.responseSerializer = [AFHTTPResponseSerializer serializer]; 

AFHTTPRequestOperation *operation = [operationManager POST:@"http://ekisansansar.com/application/webservices/Webservicefiles.php" parameters: _params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
    NSError *error; 
    if (![formData appendPartWithFileURL:[NSURL fileURLWithPath:filepath] name:@"upload_file" fileName:[filepath lastPathComponent] mimeType:@"image/png" error:&error]) { 
     NSLog(@"error appending part: %@", error); 
    } 
} success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; 
    NSLog(@"responseObject = %@", result); 




} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"error = %@", error); 
}]; 

if (!operation) { 
    NSLog(@"Creation of operation failed."); 
} 
+0

我總是發送無線參數,它完全到達服務器。但是,當使用移動數據時,我也要求參數,並在服務器端,我得到空參數。這裏我發送的參數沒有達到3g數據的服務器。你明白了嗎? –

+0

在增加請求時間後嘗試或在後端側進行調試後檢查,爲什麼您的參數無法通過移動數據訪問服務器 –