2012-11-13 43 views
1

我是RESTKIT的初学者,最近刚刚从ray的教程​​3210的foursquare public api上进行了测试。使用RESTKIT消费Web服务

虽然我得到它的要点,但有一部分我不明白,并且希望为它指针,以便我可以使用我自己的Web服务。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    RKURL *baseURL = [RKURL URLWithBaseURLString:@"https://api.Foursquare.com/v2"]; 
    RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:baseURL]; 
    objectManager.client.baseURL = baseURL; 

    RKObjectMapping *venueMapping = [RKObjectMapping mappingForClass:[Venue class]]; 
    [venueMapping mapKeyPathsToAttributes:@"name", @"name", nil]; 
    [objectManager.mappingProvider setMapping:venueMapping forKeyPath:@"response.venues"]; 

    [self sendRequest]; 
} 

我如何更改

[venueMapping mapKeyPathsToAttributes:@"name", @"name", nil]; 
[objectManager.mappingProvider setMapping:venueMapping forKeyPath:@"response.venues"]; 

,以适应自己的WebMethod? (我的webMethod如下所示)

目前,我上传我的文件到IIS进行测试,并使用IP进行Web服务。 (我在不断变化的工作区,所以我将它指定为myIPAddress更容易沟通)

- 我的业务代码(改变编辑:现在回到JSON)

[WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public void testTextJSON() 
    { 
     string text = "Testing for Json!"; 
     List<string> arrayList = new List<string>(); 
     arrayList.Add(text); 
     JavaScriptSerializer js = new JavaScriptSerializer(); 
     string name = js.Serialize(arrayList); 
     Context.Response.Write(name); 
    } 

回报 - ["Testing for Json!"]

编辑 - 我现在改变了viewDidLoadsendRequest来测试我自己的服务

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    RKURL *baseURL = [RKURL URLWithBaseURLString:@"http://192.168.1.12"]; 
    RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:baseURL]; 
    objectManager.client.baseURL = baseURL; 

    RKObjectMapping *venueMapping = [RKObjectMapping mappingForClass:[venue class]]; 
    [venueMapping mapKeyPathsToAttributes:@"name", @"name", nil]; 


    [self sendRequest]; 
} 

- (void)sendRequest 
{ 

    RKObjectManager *objectManager = [RKObjectManager sharedManager]; 

    RKURL *URL = [RKURL URLWithBaseURL:[objectManager baseURL] resourcePath:@"/webService/webService1.asmx/testTextJSON"]; 
    objectManager.acceptMIMEType = RKMIMETypeJSON; 
    objectManager.serializationMIMEType = RKMIMETypeJSON; 
    [[RKParserRegistry sharedRegistry] setParserClass:[RKJSONParserJSONKit class] forMIMEType:@"text/plain"]; 

    [objectManager loadObjectsAtResourcePath:[NSString stringWithFormat:@"%@", [URL resourcePath]] delegate:self]; 
} 

编辑n + 1 - 这里是我的一些错误信息,也许有人可以告诉我哪里出了问题?

2012-11-25 06:49:20.925 fourSquareAPI[352:12e03] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x8354430> valueForUndefinedKey:]: this class is not key value coding-compliant for the key venues.' 

如果我删除[objectManager.mappingProvider setMapping:venueMapping forKeyPath:@"venues"]; 我会得到

2012-11-25 06:52:47.495 fourSquareAPI[368:11603] response code: 200 
2012-11-25 06:52:47.499 fourSquareAPI[368:12e03] W restkit.object_mapping:RKObjectMapper.m:87 Adding mapping error: Could not find an object mapping for keyPath: '' 
2012-11-25 06:52:47.499 fourSquareAPI[368:12e03] E restkit.network:RKObjectLoader.m:231 Encountered errors during mapping: Could not find an object mapping for keyPath: '' 
2012-11-25 06:52:47.502 fourSquareAPI[368:11603] Error: Could not find an object mapping for keyPath: '' 

是否有人可以教教我该怎么办?任何帮助将不胜感激,我真的想学习如何使用RESTKIT消费Web服务。

回答

0

关键路径response.venues是相对于JSON或XML格式的服务器响应的属性路径。在这种情况下,服务器返回具有关键“场地”的“响应”。这很可能是您将应用该映射的场地列表。您必须根据您的服务器响应来调整它。

如果你想映射前处理特定的值时,使用此功能:

- (void)objectLoader:(RKObjectLoader *)loader willMapData:(inout __autoreleasing id *)mappableData; { 
    if([*mappableData valueForKey:@"result"] != nil){ 
    [*mappableData removeObjectForKey:@"result"]; //This key won't be mapped now 
    } 
} 
+0

因此,假如我的服务返回一个简单的字符串,我仍然需要的keyPath? –

+1

映射基于键值对完成。如果服务器只返回一个值,它将不会被映射。 – danielM

+0

我明白了,非常感谢!教程解释倾向于跳过这些细节和信息,即使在阅读和完成之后,也无法理解大部分组件。 –