首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >STHTTPRequest如何不postData键值?

STHTTPRequest如何不postData键值?
EN

Stack Overflow用户
提问于 2013-10-04 08:06:59
回答 1查看 1K关注 0票数 1

我尝试使用STHTTPRequest发布JSON对象:

代码语言:javascript
复制
{
 "login":"name",
 "password":"password"
}

我怎么能做到呢?

我编写ios代码:

代码语言:javascript
复制
STHTTPRequest * request=[STHTTPRequest requestWithURLString:@"http://moscow2013.hol.es/test.php"];
request.completionBlock=^(NSDictionary *headers, NSString *body) {
    NSLog(@"Data:%@",body);
};
request.errorBlock=^(NSError *error) {
    NSLog(@"Error:%@",error);

};

NSMutableDictionary *postData=[[NSMutableDictionary alloc] init];
NSMutableDictionary *data=[[NSMutableDictionary alloc] init];
[data setValue:@"a3bc5193-60b2-4f61-8f2d-a24b01423cb1" forKey:@"login"];
[data setValue:udid forKey:@"udid"];
[postData setValue:data forKey:@"data"];
request.encodePOSTDictionary=NO;
request.POSTDictionary=postData;
request.postDataEncoding=NSUTF8StringEncoding;
[request startAsynchronous];

但在服务器上:

代码语言:javascript
复制
Array
(
    [data] => {
    login = "a3bc5193-60b2-4f61-8f2d-a24b01423cb1";
    udid = "4D6A3ABF-29CF-4D79-9716-BBBD8160626F";
}
)

我是怎么把json当作对象的?

我接下来做的是:

代码语言:javascript
复制
NSString *jsonString =nil;
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data
                                                   options:NSJSONWritingPrettyPrinted
                                                     error:&error];
if (! jsonData) {
    NSLog(@"Got an error: %@", error);
} else {
    jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

NSMutableURLRequest *request2 = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://moscow2013.hol.es/horeca.php"]];
[request2 setHTTPMethod:@"POST"];
[request2 setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"content-type"];
//this is hard coded based on your suggested values, obviously you'd probably need to make this more dynamic based on your application's specific data to send
NSString *postString = jsonString;
NSData *data2 = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request2 setHTTPBody:data2];
[request2 setValue:[NSString stringWithFormat:@"%u", [data2 length]] forHTTPHeaderField:@"Content-Length"];
[NSURLConnection connectionWithRequest:request2 delegate:self];
NSData *retData=    [NSURLConnection sendSynchronousRequest:request2 returningResponse:nil error:nil];

NSString *retDataStr=[[NSString alloc] initWithData:retData encoding:NSUTF8StringEncoding];
NSLog(@"%@",retDataStr);

然后在服务器端,我得到了正确的json。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-07 13:25:41

我添加了一个rawPOSTData属性。

您可以提取最新版本并尝试如下:

代码语言:javascript
复制
NSDictionary *d = @{ @"login":@"myLogin", @"udid":@"myUDID" };

NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:d
                                                   options:NSJSONWritingPrettyPrinted
                                                     error:&error];

STHTTPRequest *request = [STHTTPRequest requestWithURLString:@"http://moscow2013.hol.es/test.php"];

[request setHeaderWithName:@"content-type" value:@"application/json; charset=utf-8"];

request.rawPOSTData = jsonData;

request.completionBlock=^(NSDictionary *headers, NSString *body) {
    NSLog(@"Body: %@", body);
};

request.errorBlock=^(NSError *error) {
    NSLog(@"Error: %@", [error localizedDescription]);
};

[request startAsynchronous];

如果不符合你的需要请告诉我。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19176289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档