2

我想通过使用将 JSON 字典作为参数传递uploadUrl,但它给了我一个Unsupported Urlcode-1002 错误。

当我在 Postman 上点击此 URL 时,它运行良好。如何使用 JSON 模型实现这一点?

NSString *uploadUrl =@"<Your host URL>";

[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);
    }
}];
4

3 回答 3

1

stringByAddingPercentEscapesUsingEncoding

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

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);
    }
}];
于 2015-09-03T09:14:48.797 回答
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 编码方式发送它。

于 2015-09-03T05:25:28.390 回答
0

查看您的问题和 API。您想向后端发送一些信息,您需要使用POST方法和您的数据作为json格式发布信息。

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

我希望你清楚,你不能使用请求发送超过 256 个字节GET,因为你的 API 是POST请求。在您现有的方法中进行以下更改,您就完成了。

NSString *uploadUrl =@"<Your host URL>";
NSDictionary *dictParams = @{@"key" : @"value"};
[JSONHTTPClient postJSONFromURLWithString:uploadUrl params:dictParams
                               completion:^(NSDictionary *json, JSONModelError *err)
 {
 }];
于 2015-09-03T07:10:55.297 回答