您好请帮我解决如何为api
准备NSMutableURLRequest网址:www.XXXXXXXX.com/api.php
登录: -
www.XXXXXXXXXXXX.com/api.php?task=login
POST数据: -
“email”=>用户的电子邮件
“pw”=>用户密码
json响应:成功登录时的会话ID
我是这样想的。
NSMutableURLRequest *request;
request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"www.XXXXXXXX.com/api.php?task=login"]];
[request setHTTPMethod:@"POST"];
NSString *postString =@"email=xxxxxxxx@gmail.com&pw=1234";
[request setValue:[NSString
stringWithFormat:@"%d", [postString length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[postString
dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
答案 0 :(得分:5)
一个原因可能是您忘记添加“http:”方案:
[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.XXXXXXXX.com/api.php?task=login]];
HERE --^
另请注意,设置正文数据的正确方法,尤其是长度
NSString *postString =@"email=xxxxxxxx@gmail.com&pw=1234";
NSData *postData = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request setValue:[NSString stringWithFormat:@"%d", [postData length]]
forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:postData];
因为UTF-8编码数据的长度可能与(Unicode)字符串长度不同。
答案 1 :(得分:-1)
试试这个:
NSError *error;
NSString *jsonString;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonData1
options:0 error:&error];
if (!jsonData) {
NSLog(@"Got an error: %@", error);
} else {
jsonString= [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
NSData *postData = [[[NSString alloc] initWithFormat:@"method=methodName&email=%@&password=%@", user_name, pass_word] dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%ld",[postData length]];
jsonData=[jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:URL]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"\"Accept\""];
[request setValue:@"application/json" forHTTPHeaderField:@"\"Content-Type\""];
[request setValue:postLength forHTTPHeaderField:@"\"Content-Length\""];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData];
NSError *requestError = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&requestError];
if ([response statusCode] >= 200 && [response statusCode] < 300) {
NSError *serializeError = nil;
NSString* newStr = [NSString stringWithUTF8String:[urlData bytes]];
NSDictionary *jsonData = [NSJSONSerialization
JSONObjectWithData:urlData
options:NSJSONReadingAllowFragments
error:&serializeError];
NSLog(@"recdata %@",jsonData);
}
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection)
{
NSLog(@"theConnection is succesful");
}
[connection start];
答案 2 :(得分:-1)
当我使用一个意外包含新行的变量指定我的基本URL时,我遇到了这个错误。