Ios 不支持的URL错误代码-1002

Ios 不支持的URL错误代码-1002,ios,nsmutableurlrequest,Ios,Nsmutableurlrequest,您好,请帮助我如何为下面的api准备NSMutableURLRequest 网址:www.xxxxxxx.com/api.php 登录:- www.xxxxxxxxxx.com/api.php?task=login 发布数据:- “电子邮件”=>用户的电子邮件 “pw”=>用户密码 json响应:成功登录时的会话id 我试着这样做 NSMutableURLRequest *request; request=[NSMutableURLRequest requestWithURL:[NSURL U

您好,请帮助我如何为下面的api准备NSMutableURLRequest

网址:www.xxxxxxx.com/api.php

登录:-

www.xxxxxxxxxx.com/api.php?task=login

发布数据:-

“电子邮件”=>用户的电子邮件

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

一个原因可能是您忘记添加“http:”方案:

还要注意,设置主体数据,特别是长度的正确方法是

因为UTF-8编码数据的长度可能不同于(Unicode)字符串长度。

请尝试以下操作:

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];
NSError*错误;
NSString*jsonString;
NSData*jsonData=[NSJSONSerialization dataWithJSONObject:jsonData1
选项:0错误:&错误];
if(!jsonData){
NSLog(@“出现错误:%@”,错误);
}否则{
jsonString=[[NSString alloc]initWithData:jsonData编码:NSUTF8StringEncoding];
}
NSData*postData=[[NSString alloc]initWithFormat:@“方法=方法名和电子邮件=%@&密码=%@”,用户名,密码]数据使用编码:NSUTF8StringEncoding];
NSString*postLength=[NSString stringWithFormat:@“%ld”,[postData length]];
jsonData=[jsonString数据使用编码:NSUTF8StringEncoding];
NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]init];
[请求设置URL:[NSURL URLWithString:URL]];
[请求设置HttpMethod:@“POST”];
[请求设置值:@“应用程序/json”用于HttpHeaderField:@“接受”];
[请求设置值:@“应用程序/json”用于HttpHeaderField:@“\”内容类型\];
[请求设置值:HttpHeaderField的postLength:@“\”内容长度\”“;
[request setValue:@“application/x-www-form-urlencoded;charset=utf-8”用于HttpHeaderField:@“Content Type”];
[请求setHTTPBody:jsonData];
NSError*requestError=[[NSError alloc]init];
NSHTTPURLResponse*响应=nil;
NSData*urlData=[NSURLConnection sendSynchronousRequest:请求
returningResponse:&响应
错误:&requestError];
如果([response statusCode]>=200&&[response statusCode]<300){
n错误*序列化错误=nil;
NSString*newStr=[NSString stringWithUTF8String:[urlData字节]];
NSDictionary*jsonData=[NSJSONSerialization
JSONObjectWithData:urlData
选项:NSJSONReadingAllowFragments
错误:&序列化错误];
NSLog(@“recdata%@”,jsonData);
}
NSURLConnection*connection=[[NSURLConnection alloc]initWithRequest:request委托:self];
如果(连接)
{
NSLog(@“连接成功”);
}
[连接启动];

当我用一个意外包含新行的变量指定我的基本URL时,我遇到了这个错误。

我也尝试了。但它现在可以正常工作了。没有改变任何东西。@Enmud:如果一切都正常,没有任何改变,那么你最好删除这个问题。
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];
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];