ios - asihttprequest 如何使用 SBJson 发布 json?

标签 ios asihttprequest sbjson

我写了一个方法:

-(id)postAndParseJson:(NSMutableDictionary *)theDictionary action:(NSString *)action type:(NSString *)myType {

    NSString *newJSON = [theDictionary JSONRepresentation];
    NSLog(@"what to send: %@", newJSON);

    NSURL *url = [NSURL URLWithString:@"http://api.uat.ushi.cn/user/getProfile?user_id=3"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    NSLog(@"to url = %@",url);
    [request appendPostData:[newJSON dataUsingEncoding:NSUTF8StringEncoding]];

    [request startSynchronous];
    NSString *responseString = [request responseString];    


    NSMutableDictionary* responseDict = [responseString JSONValue];
    NSLog(@"responseString = %@",responseString);



    return responseDict;
}

方法很简单,只需要post值和解析返回值,当我运行它的时候,我得到了控制台输出:

2012-05-02 12:06:29.414 Ushi.com[775:f803] what to send: {"phone":"123456","password":"123456","user_name":"tan"}
2012-05-02 12:06:29.416 Ushi.com[775:f803] to url = http://api.uat.ushi.cn/user/getProfile?user_id=3
2012-05-02 12:06:29.449 Ushi.com[775:f803] -JSONValue failed. Error is: Illegal start of token [<]
2012-05-02 12:06:29.449 Ushi.com[775:f803] responseString = <html><head><title>Apache Tomcat/6.0.20 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 405 - Request method 'POST' not supported</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Request method 'POST' not supported</u></p><p><b>description</b> <u>The specified HTTP method is not allowed for the requested resource (Request method 'POST' not supported).</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0.20</h3></body></html>
(lldb) 

它崩溃了!但是如果我注释行[request appendPostData:[newJSON dataUsingEncoding:NSUTF8StringEncoding]];我会收到数据,但它也会崩溃。 提前谢谢你...

最佳答案

那是因为你从服务器得到的不是有效的 JSON,而是纯 html。

问题是你的服务器不支持POST,所以返回错误页面。

关于ios - asihttprequest 如何使用 SBJson 发布 json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10407596/

相关文章:

iphone - 从 ASIHTTPRequest 转换为 AFNetworking

ios - NSMutableURLRequest 转换为 ASIFormDataRequest

iphone - 为什么SBJson JSON解析只得到最后一个感兴趣的key?

ios - didReceiveMemoryWarning 和释放全局资源

ios - Objective C 半只读属性

ios - 将视频播放器添加到 UITableView Cell *Swift

iphone - 如何创建 session ?

iphone - ASIHTTPRequest 和僵尸...

objective-c - iOS Objective-C - JSON 字符串到 NSDictionary 异常

ios - iOS Facebook SDK V 3.0 上的重复符号 SBJSON