我是RESTKIT的初学者,最近刚刚在ray&#39教程http://www.raywenderlich.com/13097/intro-to-restkit-tutorial的foursquare public api上测试过它。
虽然我得到了它的要点,但有一些我不理解的部分,并希望指针,以便我可以使用我自己的网络服务。
- (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!"]
编辑 - 我目前为viewDidLoad
和sendRequest
更改的内容,以测试我自己的服务
- (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 :(得分: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
}
}