如何使用iOS中的AFNetworking将数据发送到php服务器

时间:2015-03-19 09:07:37

标签: ios objective-c json afnetworking-2

我有一些数据,如(Image& Text)。我想通过我的目标c编码使用AFNetworking将这些数据发送到PHP服务器。

我的回复是这样的:

enter image description here

我必须发送指定&关于PHP服务器的字段文本。

我的代码如下:

   - (void) upload 
   {
      AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
      manager.requestSerializer = [AFJSONRequestSerializer serializer];

      NSDictionary *params = @ {@"about" :self.aboutField.text, @"designation" :self.desinationField.text };

      [manager POST:@UPDATE_PROFILE_URL parameters:params
      success:^(AFHTTPRequestOperation *operation, id responseObject)
      {
         NSLog(@"JSON: %@", responseObject);
      }
         failure:
      ^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"Error: %@", error);
      }];
   }   

我正在添加" text / html"在acceptableContentTypes课程中设置为AFURLResponseSerialization

当我运行我的应用程序时,我得到以下错误:

 Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x7d405870 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

请帮帮我。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

最后我可以上传图片&使用AFNetworking的服务器上的文本。

添加这两个类 AFNetworking &的的UIKit + AFNetworking

导入:

#import "AFHTTPRequestOperation.h"
#import "AFHTTPRequestOperationManager.h"

这是我的代码:

 - (void)uploadProfile_Details
{
   AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@UPDATE_PROFILE_URL]];

   // Retrieve Image from Document directory------

   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"profileImg.png"];
   UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];

   NSData *imageData = UIImageJPEGRepresentation(img, 0.5);

   // Create dictionary for sending text to server ------

   NSDictionary *parameters  = [NSDictionary dictionaryWithObjectsAndKeys:self.desinationField.text,@"designation",self.teamField.text,@"team",self.aboutField.text,@"about",self.skillLbl.text,@"skills",self.interestField.text,@"interests", nil];
   AFHTTPRequestOperation *operation = [manager POST:@UPDATE_PROFILE_URL parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
   {
       //do not put image inside parameters dictionary as I did, but append it!
       [formData appendPartWithFileData:imageData name:@"image" fileName:@"myimage.jpg" mimeType:@"image/jpeg"];
   } success:^(AFHTTPRequestOperation *operation, id responseObject)     {
     NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);

 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@ ***** %@", operation.responseString, error);
 }];
 [operation start];
}