如何在 iOS 中使用 ASI Http 将数据 POST 到 Web 服务?

发布于 2024-12-02 06:39:46 字数 1246 浏览 1 评论 0原文

我正在使用 ASI 页面此处中提供的说明。我正在尝试将一些数据发送到网络服务,但没有看到任何结果。

这是我的 sendRequest 方法,它在 viewDidLoad 中被调用

-(void)sendRequest {
    NSURL *url = [NSURL URLWithString:@"http://153.60.6.75:8080/BarcodePayment/transactions"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request addRequestHeader:@"Accept" value:@"application/json"];
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    NSString *dataContent = @"{\"id\":7,\"amount\":7.0,\"paid\":true}";
    NSLog(@"dataContent: %@", dataContent);
    [request appendPostData:[dataContent dataUsingEncoding:NSUTF8StringEncoding]];
    [request setRequestMethod:@"POST"];
}

,我检查了 dataContent 字符串,输出是

{"id":7,"amount":7.0,"paid":true}

如果我从终端使用curl,我检查并这个命令有效。

curl -X POST -H 'Accept:application/json' -H 'Content-Type: application/json' http://153.60.6.75:8080/BarcodePayment/transactions/ --data '{"id":7,"amount":7.0,"paid":true}'

我的理解是,在使用curl时,我将其设置为json,指定地址,指定相当于代码中的dataContent的数据。什么也没发生。怎么了?

感谢您的帮助!

I am using the instructions provided in the ASI page here. I am trying to send some data to a web service and not seeing any results.

This is my sendRequest method which gets called in viewDidLoad

-(void)sendRequest {
    NSURL *url = [NSURL URLWithString:@"http://153.60.6.75:8080/BarcodePayment/transactions"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request addRequestHeader:@"Accept" value:@"application/json"];
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    NSString *dataContent = @"{\"id\":7,\"amount\":7.0,\"paid\":true}";
    NSLog(@"dataContent: %@", dataContent);
    [request appendPostData:[dataContent dataUsingEncoding:NSUTF8StringEncoding]];
    [request setRequestMethod:@"POST"];
}

I check the dataContent string and the output is

{"id":7,"amount":7.0,"paid":true}

If I use curl from Terminal, I checked and this command works.

curl -X POST -H 'Accept:application/json' -H 'Content-Type: application/json' http://153.60.6.75:8080/BarcodePayment/transactions/ --data '{"id":7,"amount":7.0,"paid":true}'

My understanding is that in using curl, I set it to json, specify the address, specify the data which is equivalent to dataContent in my code. Nothing happens. What's wrong?

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

甜味超标? 2024-12-09 06:39:46

除了最关键的组件(启动请求)之外,您几乎拥有了所有内容。

您需要为同步请求添加 [request startSynchronous]; 或为 [requester startAsynchronous]; 添加异步请求(并且您可能需要委托方法来处理您返回的任何响应)

这一切都在 ASIHTTPRequest 使用指南。与此最相关的部分是“创建同步请求”和“创建异步请求”。还有一些值得思考的内容摘自该页面:

一般来说,您应该优先使用异步请求而不是同步请求。当您从主线程同步使用 ASIHTTPRequest 时,应用程序的用户界面将锁定并在请求期间变得不可用。

You have pretty much everything except the most crucial component which is to start the request

You need to add [request startSynchronous]; for a Synchronous Request or [requester startAsynchronous]; for a Asynchronous request (and you possibly need the Delegate Methods to handle any response you have back)

This is all covered pretty nicely in the ASIHTTPRequest How to Use Guide. The Sections most relevant to this would be 'Creating a synchronous request' and 'Creating an asynchronous request'. Also something to think about taken from that page:

In general, you should use asynchronous requests in preference to synchronous requests. When you use ASIHTTPRequest synchronously from the main thread, your application’s user interface will lock up and become unusable for the duration of the request.

掌心的温暖 2024-12-09 06:39:46

您忘记致电:

[request setDelegate: self];
[request startAsynchronous];

或者:

[request startSynchronous];

如果您不致电其中任何一个,则永远不会提出请求:)

You forgot to call:

[request setDelegate: self];
[request startAsynchronous];

Or:

[request startSynchronous];

If you don't call any of these, the request will never be made :)

寒冷纷飞旳雪 2024-12-09 06:39:46

我在您的代码中没有看到对 [request startSynchronous] (或 [request startAsynchronous])的调用...您是否在任何地方发起请求?

I don't see a call to [request startSynchronous] (or [request startAsynchronous]) in your code... Are you even initiating the request anywhere?

转角预定愛 2024-12-09 06:39:46

您似乎没有调用 [request startAsynchronous];

检查 ASIHTTPRequest 文档了解如何使用的更多详细信息

It looks like you didn't call [request startAsynchronous];

Check ASIHTTPRequest documentation for more details on how to use

书信已泛黄 2024-12-09 06:39:46

您实际上并未发送请求。

1) 您需要调用 [request startSynchronous] 或 [request startAsynchronous]

2) 如果您使用异步(您可能应该这样做),您需要设置委托并实现 - (void)requestFinished:(ASIHTTPRequest * )请求方法。

You're not actually sending the request.

1) You'll need to call [request startSynchronous] or [request startAsynchronous]

2) If you use asynchronous (which you should probably do) you'll need to set the delegate and implement a - (void)requestFinished:(ASIHTTPRequest *)request method.

〃安静 2024-12-09 06:39:46
- (void)postThresholdDetails:(NSDictionary *)info

{
NSString *urlString = [NSString stringWithFormat:@"%@%@/",BaseUrl,PostThresholdDetails];
NSURL *url = [NSURL URLWithString:urlString];

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request setTimeOutSeconds:120];
[request addRequestHeader:@"Accept" value:@"application/json"];
[request addRequestHeader:@"content-type" value:@"application/x-www-form-urlencoded"];
request.allowCompressedResponse = NO;
request.useCookiePersistence = NO;
request.shouldCompressRequestBody = NO;
[request setPostBody:[NSMutableData dataWithData: [info objectForKey:@"jsondata"] ]];
[request startAsynchronous];

}

- (void)postThresholdDetails:(NSDictionary *)info

{
NSString *urlString = [NSString stringWithFormat:@"%@%@/",BaseUrl,PostThresholdDetails];
NSURL *url = [NSURL URLWithString:urlString];

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request setTimeOutSeconds:120];
[request addRequestHeader:@"Accept" value:@"application/json"];
[request addRequestHeader:@"content-type" value:@"application/x-www-form-urlencoded"];
request.allowCompressedResponse = NO;
request.useCookiePersistence = NO;
request.shouldCompressRequestBody = NO;
[request setPostBody:[NSMutableData dataWithData: [info objectForKey:@"jsondata"] ]];
[request startAsynchronous];

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文