简体   繁体   English

iPhone-Web服务客户端应用

[英]iPhone - Web service client app

I am writing an app that's a very simple web service client. 我正在编写一个非常简单的Web服务客户端的应用程序。 I have the following questions: 1. Is there a standard common way to write the web service consumption part so that it won't interfere with the main GUI thread? 我有以下问题:1.是否有编写Web服务使用部分的标准通用方法,以使其不会干扰主GUI线程? Is it common practice to use the main thread considering the web request should take a very short time to complete (but never know)? 考虑到Web请求应该花费很短的时间来完成(但永远不知道),因此使用主线程是否是一种常见的做法? Any links to tutorials? 有教程链接吗? 2. All examples I saw call the web service via GET. 2.我看到的所有示例都通过GET调用了Web服务。 I need to POST the data to the web service. 我需要将数据发布到Web服务。 Any known samples/tutorials that use POST? 是否有任何使用POST的已知示例/教程?

If you're using the iPhone's built-in web request API, NSURLConnection, you run the request on the main thread but you run it asynchronously, with callbacks to a delegate as data is returned. 如果您使用的是iPhone的内置Web请求API NSURLConnection,则可以在主线程上运行请求,但可以异步运行它,并在返回数据时回调到委托。 This leaves the application responsive to user events. 这使应用程序响应用户事件。 If the parsing or processing of the returned data takes too long to run on the main thread then you should run the NSURLConnection on the main thread, then hand off response data either incrementally or after the download is complete to a secondary thread for pure compute processing. 如果对返回的数据进行解析或处理花费的时间太长而无法在主线程上运行,则应在主线程上运行NSURLConnection,然后以增量方式或在下载完成后将响应数据移交给辅助线程以进行纯计算处理。

It is possible to start NSURLConnections on a non-main thread but you'll need to create a runloop on the non-main thread, and there are reports of random thread-safety bugs in the Apple libraries. 可以在非主线程上启动NSURLConnections,但是您需要在非主线程上创建运行循环,并且Apple库中有关于随机线程安全性错误的报告。 If you really need to run the request itself on a non-main thread you can use the third party library ASIHTTPRequest , but you shouldn't need to do this except in very specialized circumstances. 如果您确实需要在非主线程上运行请求本身,则可以使用第三方库ASIHTTPRequest ,但是除了非常特殊的情况外,您不需要这样做。

Generating a POST request is straightforward with NSURLConnection: 使用NSURLConnection可以直接生成POST请求:

    NSString * requestBody = @"format up your body here, which is often form-urlencoded";
NSURL * nsurl = [NSURL URLWithString: @"http://example.com/post_request_receiver"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:nsurl];
// set appropriate content type here, usually application/x-www-form-urlencoded
    [theRequest addValue: @"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: [NSString stringWithFormat:@"%d", [requestBody length]] forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [requestBody dataUsingEncoding:NSUTF8StringEncoding]];

conn = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM