我想向服务器发送值,我在插入值时收到消息但是在获取值时为null, 代码示例。
-(void)requestReturnedData:(NSData *)data{ //activated when data is returned
NSDictionary *dictionary = [NSDictionary dictionaryWithJSONData:data];
output.text = dictionary.JSONString;
value1TextField.text =[dictionary objectForKey:@"productDescription"] ;
NSLog(@"val1",value1TextField.text);
value2TextField.text = [dictionary objectForKey:@"productId"];
value3TextField.text = [dictionary objectForKey:@"productImageUrl"];
value4TextField.text = [dictionary objectForKey:@"productName"];
value5TextField.text = [dictionary objectForKey:@"productPrice"];
value6TextField.text = [dictionary objectForKey:@"productRating"];
value7TextField.text = [dictionary objectForKey:@"productReviews"];
NSLog(@"%@",dictionary);
}
-(void)getTest{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@" http://www.mobdevapp.com/iphapi/getProductDetail.php"]];
[request setHTTPMethod:@"GET"];
[request addValue:@"getValues" forHTTPHeaderField:@"METHOD"];
//selects what task the server will perform
//initialize an NSURLConnection with the request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(!connection){
NSLog(@"Connection Failed");
}
}
-(void)postTest{
//build up the request that is to be sent to the server
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.mobdevapp.com/iphapi/ProductDetail.php"]];
[request setHTTPMethod:@"POST"];
[request addValue:@"postValues" forHTTPHeaderField:@"METHOD"];
//create data that will be sent in the post
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setValue: @"" forKey:@"productDescription"];
[dictionary setValue:@"" forKey:@"productId"];
[dictionary setValue: @"" forKey:@"productImageUrl"];
[dictionary setValue:@"" forKey:@"productName"];
[dictionary setValue: @"" forKey:@"productPrice"];
[dictionary setValue:@"" forKey:@"productRating"];
[dictionary setValue:@"" forKey:@"productReviews"];
//serialize the dictionary data as json
NSData *data = [[dictionary copy] JSONValue];
[request setHTTPBody:data]; //set the data as the post body
[request addValue:[NSString stringWithFormat:@"%d",data.length] forHTTPHeaderField:@"Content-Length"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(!connection){
NSLog(@"Connection Failed");
}
}`
以便 nslog值为null ,指导我 我如何发送价值?????
答案 0 :(得分:0)
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:requestDataDict options:NSJSONWritingPrettyPrinted error:&error];
NSLog(@"serverURL : %@", [serverURL absoluteString]);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:serverURL];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:jsonData];
// create Asynchronous connection
NSURLConnection * urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (urlConnection)
self.responseData = [[NSMutableData alloc] init];