English 中文(简体)
• 询问如何与SBJson站起来?
原标题:asihttprequest how to post json with 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;
}

the method is simple,just post the value and parse the return value,when I run it ,I get the console output:

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) 

and it crash!but if I comment the line[request appendPostData:[newJSON dataUsingEncoding:NSUTF8StringEncoding]];I will receive the data,but it also crash. thank you in advance...

最佳回答

这是因为你从服务器获得的不是有效的初等人物,而是平原的html。

The problem is that your server does not support POST , so it returns error page.

问题回答

暂无回答




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签