以下是代码
NSDictionary *post = [NSDictionary dictionaryWithObjectsAndKeys:
@"signup",@"methodName",_tfName.text,@"name",_tfNickName.text,@"nickname",
_dateOfBirth.text, @"birth_date",
@"1",@"gender",
_tfEmail.text,@"email",
_tfPassword.text,@"password",
_tfContact.text, @"mobile",
_tfCountry.text,@"country",
@"Normal",@"social_provider",
@"",@"social_accesstoken",@"3",@"sponsor_choice" ,_tfPinCode.text,@"pincode",@"",
@"fromLogin",nil];
这是实际发布数据的过程
NSData *postdata = [NSJSONSerialization dataWithJSONObject:post options:NSJSONWritingPrettyPrinted error:nil];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://abc@login.php"]]];
[request setHTTPMethod:@"POST"];
[request setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postdata];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(conn)
{
NSLog(@"Connection Successful");
}
else
{
NSLog(@"Connection could not be made");
}
}
URLConnection的DeleGates
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"Did Receive Response %@", response);
responseData = [[NSMutableData alloc]init];
}
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
//NSLog(@"Did Receive Data %@", data);
[responseData appendData:data];
}
我在这里获得响应数据的价值
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
NSLog(@"Did Fail");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Did Finish");
NSError *error = nil;
NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&error];
NSLog(@"Array: %@", jsonArray);
// Do something with responseData
}
实际上我认为jsonarray的价值为零。即使我从服务器获得响应数据的价值。
答案 0 :(得分:1)
如果我同步发布数据,那么我得到了响应,甚至我得到了JSON
NSData *responseData =[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
if (!err)
{
//Getting response from server
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];
NSLog(@"%@", jsonArray);
}