我有一个Web脚本,它通过HTTP POST请求接受JSON字符串作为输入.我遇到了几个相同的AFNetworking 1.x示例,有人可以指点我或给AFNetworking 2.0示例对带有格式化JSON作为输入的Web脚本执行HTTP POST请求吗?
谢谢
我使用AFNetworking库使用POST方法在服务器上发布数据.
以下是我的代码
- (void) callLoginAPI:(NSDictionary *)dictProfile{
// 1
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:[dictProfile valueForKey:@"name"], @"username",
[dictProfile valueForKey:@"first_name"],@"first_name",
[dictProfile valueForKey:@"last_name"],@"last_name",
[dictProfile valueForKey:@"email"],@"email",
[dictProfile valueForKey:@"birthday"],@"dob",
[dictProfile valueForKey:@"gender"],@"gender",
[[dictProfile valueForKey:@"location"] valueForKey:@"name"],@"location",
[dictProfile valueForKey:@"timezone"],@"timezone",
@"",@"language",
[NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?type=large",[dictProfile valueForKey:@"id"]],@"profile_pic_url",
@"",@"cover_pic_url",nil];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:@"http://10.1.81.35:8000/api/login/" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
Run Code Online (Sandbox Code Playgroud)
但我得到了以下错误的回应
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text …Run Code Online (Sandbox Code Playgroud) 我尝试了AFNetworking的"创建上传任务",这是为了在.net服务器上传图像,但却没有.这是我试过的.
- (void)uploadJPEGImage:(NSString*)requestURL image:(UIImage*)image
{
NSURL *url = [[NSURL alloc] initWithString:requestURL];
NSMutableURLRequest *urequest = [NSMutableURLRequest requestWithURL:url];
[urequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[urequest setHTTPShouldHandleCookies:NO];
[urequest setTimeoutInterval:60];
[urequest setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[urequest setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
// add image data
NSData *imageData = UIImageJPEGRepresentation(qrCodeImage.image, 1.0);
if (imageData) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", pictureName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个与特定JIRA服务器交互的iPhone应用程序.我有以下代码登录:
NSURL *url = [[NSURL alloc] initWithString:@"https://mycompany.atlassian.net/rest/auth/latest/session/"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *postString = [NSString stringWithFormat:@"{\"username\":\"%@\",\"password\":\"%@\"}", username, password];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept" ];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:
^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"ERROR: %@", error);
}
];
[operation start];
Run Code Online (Sandbox Code Playgroud)
但它给了我以下与Content-Type有关的错误:
ERROR: Error Domain=AFNetworkingErrorDomain Code=-1011
"Request failed: unsupported media type (415)"
UserInfo=0x8cd6540
{
NSErrorFailingURLKey=https://mycompany.atlassian.net/rest/auth/latest/session/,
NSLocalizedDescription=Request failed: unsupported media type …Run Code Online (Sandbox Code Playgroud) objective-c ios afnetworking afjsonrequestoperation afnetworking-2
我正在使用AFNetworking尝试上传文件:
-(void)uploadFile{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"data.sqlite"];
NSURL *filePathURL = [NSURL fileURLWithPath:filePath];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://localhost:8888/myApp/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePathURL name:@"file" fileName:@"data.sqlite" mimeType:@"text/html" error:nil];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSProgress *progress = nil;
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"Success: %@ %@", response, responseObject); …Run Code Online (Sandbox Code Playgroud) 我正在用CocoaPods实现AFNetworking到Swift项目中。我习惯使用Ruby编程,并且对iOS开发非常陌生。Cocoapods很难在我的项目中正常工作,但现在我可以成功访问AFNetworking库。
我想做的是用POST命中一个表单,并得到一个可以解析的“ text / html”响应,以便我可以知道它是否保存。这本身不是API,而是InfusionSoft生成的一种形式。用户将输入一个电子邮件地址,我会将其发送到API进行存储。这是我正在使用的代码:
let manager = AFHTTPRequestOperationManager()
var parameters = ["inf_form_xid": "MY_ACCESS_ID",
"inf_form_name": "Webform in Content App",
"infusionsoft_version": "1.34.0.35",
"inf_field_email": self.emailTextField.text]
manager.POST( "https://ns166.infusionsoft.com/app/form/process/REALLYLONGUNIQUEID",
parameters: parameters,
success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
println("JSON: " + responseObject.description)
},
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println("Error: " + error.localizedDescription)
})
Run Code Online (Sandbox Code Playgroud)
我收到此错误是AFNetworking的回应:
Error: Request failed: unacceptable content-type: text/html
Run Code Online (Sandbox Code Playgroud)
归根结底,我希望在允许用户继续在应用程序中之前验证它是否已由服务器保存。
任何帮助是极大的赞赏。
我正在尝试下面的代码,但它给出了错误:
{
NSURL *url = [NSURL URLWithString:@"http://ielmo.xtreemhost.com/array.php"];
NSURLRequest *urlRequest =[[NSURLRequest alloc]initWithURL:url];
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response: %@", responseObject);
_imV.image = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Image error: %@", error);
}];
[requestOperation start];
}
Run Code Online (Sandbox Code Playgroud)
请帮我解决"Request failed: unacceptable content-type: text/html"错误.
ios ×7
afnetworking ×3
http-post ×2
json ×2
objective-c ×2
api ×1
asynchronous ×1
php ×1
swift ×1
web-services ×1