簡體   English   中英

如何使用AFNetworking將json數據作為參數傳遞到http中

[英]how to pass json data as parameter in the http using AFNetworking

嗨,我正在嘗試將json請求傳遞給server.I已經為此使用api,並且必須在api中傳遞json參數,但是我不知道如何在api中傳遞json參數,知道我正在使用AFNetworking傳遞json參數下面的代碼,我已經使用。 請幫助我解決此問題,謝謝。

參數格式就像

 {
  "email" : "abc@gmail.com",
  "phone" : "1234567890",
  "password" : "aaa",
  "medium" : "Email",
  "name" : "abc",
  "uos" : "i"
}

代碼:

services *srv=[[services alloc]init];

NSString *str=@"http://emailsending.in/setting_saver_api/";                    NSString *method=@"registration.php";
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];

[dict setObject:self.txtName.text forKey:@"name"];
[dict setObject:self.txtEmail.text forKey:@"email"];
[dict setObject:self.txtphone.text forKey:@"phone"];
[dict setObject:self.txtPassword.text forKey:@"password"];
[dict setObject:@"Email" forKey:@"medium"];
[dict setObject:@"i" forKey:@"uos"];
NSString *jsonString;
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                   options:NSJSONWritingPrettyPrinted
                                                     error:&error];
if (! jsonData) {
    NSLog(@"Got an error: %@", error);
} else {
    jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"json string :%@",jsonString);
}

[srv postToURL:str withMethod:method andParams:jsonString completion:^(BOOL success, NSDictionary *responseObj)

 {
     NSLog(@"res :%@",responseObj);
     NSLog(@"%d",success);
     NSLog(@"Successfully..................");
}];

這是您遇到的錯誤。 請與您的后端團隊聯系。

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /setting_saver_api/ was not found on this server.</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>

您可以這樣使用:

NSString* jsonString = [NSString stringWithFormat:@"email=%@&phone=%@&password=%@&name=%@&uos=%@",email.text,phone.text,password.text,name.text,uos.text];//enter your own values

NSURL *urlPath = [NSURL URLWithString:@"Your URL String"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urlPath cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];

NSData *requestData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

 [request setHTTPMethod:@"POST"];
 [request setHTTPBody:requestData];
 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {            
                if(data != nil){
                  //your code
                }
                else {
                  //error
                }
}];

希望這可以幫助。

以下代碼可能有幫助:

NSString *comment_id = @"comment"
NSString *string = [NSString stringWithFormat:@"apiURL];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"apikey":apikey,@"text":text, @"comment_id":comment_id}; // this is an example 

[manager POST:string parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success %@", responseObject);
    NSDictionary *data = (NSDictionary *)responseObject;

    if ([[[data objectForKey:@"response"] objectForKey:@"status"] integerValue]){

        //success 

    }else{
        //else part goes here 
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

試試這個

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];


    NSDictionary *params = @ {@"user" :txtUserName, @"pwd" :txtPwd };


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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM