1

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

{
 "login":"name",
 "password":"password"
}

我该怎么做?

我写ios代码:

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

但是在服务器上我得到了:

Array
(
    [data] => {
    login = "a3bc5193-60b2-4f61-8f2d-a24b01423cb1";
    udid = "4D6A3ABF-29CF-4D79-9716-BBBD8160626F";
}
)

我如何发送 json 作为对象?

我接下来做:

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。

4

1 回答 1

3

我添加了一个rawPOSTData属性。

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

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

如果它不能满足您的需求,请告诉我。

于 2013-10-07T13:25:41.847 回答