在Xcode中使用NSURLConnection发送JSON数据

时间:2012-06-14 12:53:19

标签: objective-c xcode json

我正在尝试通过以下代码将JSON数据发送到Web服务器。由于某种原因,请求似乎没有出去。我错过了什么? NSURLConnection(retStr)的结果也总是空的?

NSDictionary *data = [NSDictionary dictionaryWithObject:@"test sending ios" forKey:@"value1"];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:kNilOptions error:&error];

    NSURL *url = [NSURL URLWithString:@"http://webserveraddress"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url cachePolicy:nil timeoutInterval:60];
[req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[req setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:jsonData];
NSString *retStr = [[NSString alloc] initWithData:[NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil] encoding:NSUTF8StringEncoding];

3 个答案:

答案 0 :(得分:3)

要将后期变量中的简单数据发送到运行php的网络服务器,您只需在

中执行此操作

示例

NSString * key = [NSString stringWithFormat:@"var1=%@&var2=%@&var3=%@",@"var1String" ,@"var2string" ,[NSnumber numberWithBool:YES]];

NSURL * url = [NSURL URLWithString:@"http://webserver.com/yourScriptPHP.php"];

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];
[request setHTTPBody:[key dataUsingEncoding:NSUTF8StringEncoding]];

[[NSURLConnection alloc] initWithRequest:request delegate:self];
// this is for you to be able to get your server answer. 
// you will need to make your class a delegate of NSURLConnectionDelegate and NSURLConnectionDataDelegate
myClassPointerData = [[NSMutableData data] retain];

<强>实施

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [myClassPointerData appendData:data]
}

-(void)connection:(NSURLConnection *)connection DidFinishLoading {
    // do what you want with myClassPointerData the data that your server did send you back here
    // for info on your server php script you just need to do: echo json_encode(array('var1'=> $var1, 'var2'=>$var2...));
    // to get your server sending an answer
}

答案 1 :(得分:0)

一次检查

ASIFormDataRequest * request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@“ur url”]];

[request setPostValue:appdelegate.userid forKey:@"userid"];
[request setPostValue:self.nameLbl.text forKey:@"username"];
[request setPostValue:self.website.text forKey:@"website"];

NSLog(@"~~~~~~ request~~~~~ %@",request);

[request setTimeOutSeconds:5000];
[request startSynchronous];

答案 2 :(得分:0)

不是像JSON一样发送,而是通过普通的方式发送它 - 通过POST方法设置请求的HTTPBody。

如果您需要拨打电话,则需要进行区分 'http://abc.example.com/user_profile.json?user [FIRST_NAME] = fdffdf&安培;用户[姓氏] = dffdf'

是,你需要让你的字典键有一个前缀。 也就是说,'first_name = fdffdf'需要更改为'user [first_name] = fdffdf'。

尝试使用这段代码将参数字典更改为JSON所需的格式..

for (id key in [self allKeys]) {
NSString *newKey = [NSString stringWithFormat:@"%@[%@]", parent, key];
[result setObject:[self objectForKey:key] forKey:newKey];
}