如何使用Post mehod的JSON模型将JSON字典作为参数传递?

时间:2015-09-03 05:12:19

标签: ios objective-c json web-services jsonmodel

我想通过使用uploadUrl将json字典作为参数传递。但是它给了我不支持的Url错误代码1002。 但是当我在邮递员上打这个网址时。它的工作非常完美。如何使用json模型实现这一点。我陷入了这个问题。请帮忙。

<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  <title>Google map demo</title>
  <style type="text/css">
    html,
    body,
    #map_canvas {
      width: 100%;
      height: 700px;
      margin: 0;
      padding: 0;
    }
    .infowindow * {
      font-size: 90%;
      margin: 0
    }
  </style>
  <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?callback=initialize" defer></script>
</head>

<body>
  <div id="map_canvas"></div>
</body>
<script type="text/javascript">
  function initialize() {
    alert("map api loaded");
    // initialize map 
    var myOptions = {
      zoom: 2,
      center: new google.maps.LatLng(37.422104808, -122.0838851)
    };

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  }
</script>

</html>

3 个答案:

答案 0 :(得分:1)

stringByAddingPercentEscapesUsingEncoding

此方法解决了这个问题。以前我分配了不受支持的网址。

  NSError * err;
    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&err];
    NSString *myString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
// This line is the answer..     
   myString = [myString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

NSString *uploadUrl = [NSString stringWithFormat:@"<MY host URL>"?data=%@",myString];

[JSONHTTPClient postJSONFromURLWithString:uploadUrl params:nil
                                   completion:^(NSDictionary *json, JSONModelError *err)
     {
         if(err == nil)
         {
             UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"success" message:@"uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
             [alert show];
             completionHanldler(json);
         }
         else
         {
             UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Failed" message:@"uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
             [alert show];

             NSMutableDictionary *errorDict=[[NSMutableDictionary alloc]init];
             if(err.code==-1009)
                 [errorDict setObject:@"The Internet connection appears to be offline."forKey:@"error"];
             else
                 [errorDict setObject:@"Error occurred. Please try again!"forKey:@"error"];

             completionHanldler(errorDict);
         }
     }];

答案 1 :(得分:0)

试试这个

NSString *uploadUrl =@"<Your host URL>";
    NSDictionary *val = @{<Add your params as key : value here>};
NSArray *innerParameter = @[val];
NSDictionary *parameter =@{@"Passports":innerParameter};


[JSONHTTPClient postJSONFromURLWithString:uploadUrl params:parameter
                               completion:^(NSDictionary *json, JSONModelError *err)
 {
     if(err == nil)
     {
         UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"success" message:@"uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
         [alert show];
 NSError *error;
NSDictionary *resultDict = [NSJSONSerialization JSONObjectWithData:objectData  options:NSJSONReadingMutableContainers error:&jsonError]; // this is a dictionary NOT the JSON
         completionHanldler(resultDict);
     }
     else
     {
         UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Failed" message:@"uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
         [alert show];

         NSMutableDictionary *errorDict=[[NSMutableDictionary alloc]init];
         if(err.code==-1009)
             [errorDict setObject:@"The Internet connection appears to be offline."forKey:@"error"];
         else
             [errorDict setObject:@"Error occurred. Please try again!"forKey:@"error"];

         completionHanldler(errorDict);
     }
 }];

[编辑] 使用AFNetworking进行相同的网络呼叫

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *val = @{<Params as key : value>};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"<your host URL>" parameters:val success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

[编辑:2]

首先,我认为后端是在POST中完成的,但它是通过URL编码进行的,经过数字试验和错误之后,这无法完成,即使在GET中尝试之后也是如此。据我所知,后端无法识别JSON格式的POST方法,因此您只能以URL编码方式发送它。

答案 2 :(得分:0)

审核您的问题和API。如果您想向后端发送一些信息,则需要使用json方法发布信息,并将数据设置为POSTMAN格式。

您可以使用任何在线API代理(例如hurlitGET)来测试请求。

我希望您明白,您不能使用POST请求发送超过256个字节,并且您的API是NSString *uploadUrl =@"<Your host URL>"; NSDictionary *dictParams = @{@"key" : @"value"}; [JSONHTTPClient postJSONFromURLWithString:uploadUrl params:dictParams completion:^(NSDictionary *json, JSONModelError *err) { }]; 请求。在您现有的方法中进行以下更改,您就完成了。

public static void ChangeSomething(ref int[] array)
{
     array = new int[2];
}