开发者

How do I use ASI Http in iOS to POST data to a web service?

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"];
}
开发者_StackOverflow中文版

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!


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.


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 :)


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


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

Check ASIHTTPRequest documentation for more details on how to use


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.


- (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];

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜