4

我正在尝试通过下面的代码将 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];
4

3 回答 3

3

要将 post vars 中的简单数据发送到运行 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
}
于 2012-06-15T08:20:22.010 回答
0

您可以简单地以正常方式发送它,而不是作为 JSON 发送 - 通过使用 POST 方法设置请求的 HTTPBody。

如果您需要拨打电话,例如 'http://abc.example.com/user_profile.json?user[first_name]=fdffdf&user[last_name]=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];
} 
于 2013-04-02T12:58:01.447 回答
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];
于 2012-06-14T13:15:34.017 回答