Note that there are some explanatory texts on larger screens.

plurals
  1. POASIHttpRequest POST JSON well, but AFNetworking is not
    text
    copied!<p>I want to "POST" a JSON value to server and response a json databack.</p> <p>The URL: <a href="http://solok.com:8080/soloo/phone/execute?content=" rel="nofollow">http://solok.com:8080/soloo/phone/execute?content=</a>{"method":"tet_123","version","1"}, can get the right value(JSON) in browser.</p> <p>ASIHTTPRequest way:</p> <pre><code>NSDictionary *postDic = [NSDictionary dictionaryWithObjectsAndKeys:@"tet_123",@"method",@"1",@"version",nil]; NSString *postString; //Then convert the "postDic" to NSString, the value is:{"method":"tet_123","version","1"} assign to postString; psotString = ...; ASIFormDataRequest *req=[ASIFormDataRequest requestWithURL:url]; [req setRequestMethod:@"POST"]; [req setPostValue:posStr forKey:@"content"]; [req startAsynchronous]; [req setDelegate:self]; [req setCompletionBlock:^{ NSData *d = [req responseData]; NSLog(@"respond is %@".d); } </code></pre> <hr> <p>It works smoothly! But AFNetworkding is not, here is the code;</p> <pre><code>NSURL *url = [NSURL URLWithString:@"http://localhost:8080"]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; [httpClient setParameterEncoding:AFJSONParameterEncoding]; NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"tet_123",@"method",@"1",@"version",nil]; NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:dic,@"content", nil]; [httpClient postPath:@"/soloo/phone/execute" parameters:dic1 success:^(AFHTTPRequestOperation *operation, id responseObject) { NSDictionary *d = (NSDictionary *)responseObject; NSLog(@"success is %@",d); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"fail"); }]; </code></pre> <p>The output is: success is &lt;>.</p> <p>or i use another way of AFNetworking:</p> <pre><code>NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"path:@"/soloo/phone/execute" parameters:dic1]; AFJSONRequestOperation *ope = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSLog(@"response %d",response.statusCode); } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog(@"fail%d JSON %@",response.statusCode,JSON); }]; </code></pre> <p>The respond code is 200, which means connection is correct, but still no the correct result. Not sure why. Any help, thank in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload