【问题标题】:json PUT requestjson PUT 请求
【发布时间】:2011-05-02 11:19:21
【问题描述】:

您好,我使用 JSONKit。我需要通过json请求向服务器发送状态更新来更新linkedin站点中的状态。这是我发送的代码。我刚开400 eroor。请告诉我出了什么问题。

谢谢。

NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"linkedin-html",@"contentType",@"My Fancy Update",@"body",nil];

NSString *str =[jsonDict JSONString];

NSMutableData *requestData = [NSMutableData dataWithBytes:[str UTF8String] length:[str length]]; 

[self setHTTPBody:requestData];

[self setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];  

[self setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

【问题讨论】:

  • 实际响应说什么?是返回http还是json?
  • 我遇到 400 错误。它说不支持的数据类型。

标签: iphone objective-c json httprequest linkedin


【解决方案1】:

不知道这是否完全符合您的使用需求,我正在使用 iphone 并连接到 jayrock .net 网络服务。

这是我用来处理所有呼叫的命令。

另外,我正在使用 json 框架

- (NSDictionary*) sendJSONRPCRequestTo:(NSString*) url 
                        forCommand:(NSString*)command 
                    withParamaters:(NSMutableArray*) parameters 
                       synchronous:(BOOL) sendSynchronous
{

    if (parameters != nil)
    {
        [parameters setValue:[HSAppData appVersion] forKey:@"AppVersion"];
        [parameters setValue:[NSNumber numberWithDouble:[HSAppData currentLocation].longitude] forKey:@"Longitude"];
        [parameters setValue:[NSNumber numberWithDouble:[HSAppData currentLocation].latitude] forKey:@"Latitude"];
    }

    if (self.commandId == nil)
    {
        self.commandId = @"1";
    }

    NSMutableURLRequest *request = [self.baseTransaction makeNewRequestFor:url];

    NSMutableDictionary *mainPackage = [NSMutableDictionary dictionary];
    [mainPackage setValue:self.commandId forKey:@"id"];
    [mainPackage setValue:command forKey:@"method"];
    [mainPackage setValue:parameters forKey:@"params"];

    NSString *jsonData = [mainPackage JSONRepresentation];

    [request setValue:command forHTTPHeaderField:@"X-JSON-RPC"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    if (jsonData != nil && [jsonData isEqual:@""] == NO)
    {
        [request setHTTPMethod:@"POST"];
        [request setValue:[[NSNumber numberWithInt:[jsonData length]] stringValue] forHTTPHeaderField:@"Content-Length"];
    }

    [request setHTTPBody:[jsonData dataUsingEncoding:NSUTF8StringEncoding]];
    if (sendSynchronous)
    {
        NSHTTPURLResponse   * response = nil;
        NSError             * error = nil;

        //self.baseTransaction.lastConnection = nil;
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

        NSString *jsonResult = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];

        NSDictionary *jsonDict = nil;

        @try {
            jsonDict = [jsonResult JSONValue];
        }
        @catch (NSException * e) {
            NSLog(@"Error: %@",jsonResult);
            jsonDict = [NSMutableDictionary dictionary];
            [jsonDict setValue:self.commandId forKey:@"id"];
            [jsonDict setValue:@"Unable to call function on server" forKey:@"error"];
            [jsonDict setValue:[NSNull null] forKey:@"result"];
        }
        @finally {
            return jsonDict;
        }
    }
    // TODO: Add ASynchronous
//  else 
//  {
//  }

}

【讨论】:

  • 我正在使用 OAuth 库。所以我只需要创建请求并仅使用库方法发送。
  • 看起来你的“自我”对象是我的“请求”对象。而你的“requestData”我正在用“[jsonData dataUsingEncoding:NSUTF8StringEncoding]”做同样的事情。也许您只是缺少这一行“[request setHTTPMethod:@"POST"];"您将在“self”上执行的操作
  • 我也不知道你的 JSON 格式是什么样的。我使用了 json-framework,所以我不必担心手动格式化它。
【解决方案2】:

我会首先将您的 json 有效负载和连接响应数据写入一个简单的文本文件并查看(或发布,如果您希望我们看一下)。我发现这是在将数据发布到服务时发现问题的最简单方法。你正在使用一个库,所以我猜有效载荷应该没问题,但你永远不知道。响应数据可能包含更多关于真正问题的提示,尽管我不能说我曾经使用过 LinkedIn 的 api。

另外,我没有看到您在哪里指定请求是“PUT”。你包括了

 [req setHTTPMethod:@"PUT"];

使用此代码将有效负载写入文件系统(抱歉格式化,它不能很好地与移动 safari 配合使用):

NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *documentPath = [NSString stringWithFormat:@"%@/payloadData.txt", documentsDirectoryPath];  
[requestData writeToFile:documentPath atomically:YES];

【讨论】:

  • 我已将 PUT 包含在我的请求中。 sry 没有在这里发布。 json有效载荷是什么意思?对不起,我是 json 新手,你能解释一下吗?谢谢
  • 有效负载是指您提供给服务的数据。我将使用代码编辑答案,将文件写入本地目录供您浏览(在您的 iPhone/模拟器文件系统中本地)
【解决方案3】:

可能是this可以帮助你。

【讨论】:

    猜你喜欢
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-25
    • 2015-01-19
    • 2011-06-13
    • 2013-06-12
    • 1970-01-01
    相关资源
    最近更新 更多