いくつかの簡単なJSONをRESTfulサービスにPOSTしようとしています。IOS - SBJsonが作成するJSON NSDataを調べるにはどうすればよいですか?
{username:"someuser",password:"somepassword"}
私のPOSTが存在しない値を掲示し、サーバーの訴えに失敗し続けて:私は、次の形式を取りJSON文字列を形成するようにしたいです。しかし、JSテストの実装を作成すると、同じサービスがうまく動作します。だから私はかなり私のSBJsonクライアントの実装が問題だと確信しています。
NSDictionary *userDict = [[NSDictionary alloc] initWithObjectsAndKeys:
userName,@"username",
password, @"password",
nil];
SBJsonWriter *writer = [[SBJsonWriter alloc]init];
NSData *jsonData = [writer dataWithObject:userDict];
jsonDataがサイズを持っているように見えますが、SBJsonでシリアル化しようとしましたが、失敗してしまい、ゼロになります。
NSString *test = [writer stringWithObject:jsonData]; // returns nil
私はNSMutableURLRequest上記を投稿しようとしています。コンテキストを提供するのに役立つ場合は、私の実装です:
-(void)registerUserName:(NSString *)userName andPassword:(NSString *)password{
responseData = nil;
responseData = [NSMutableData data];
NSDictionary *userDict = [[NSDictionary alloc] initWithObjectsAndKeys:
userName,@"username",
password, @"password",
nil];
SBJsonWriter *writer = [[SBJsonWriter alloc]init];
NSData *jsonData = [writer dataWithObject:userDict];
NSString *test = [writer stringWithObject:jsonData]; //always nil
NSURL *url = [[NSURL alloc] initWithString:@"http://localhost/~user/app_web/index.php/user/create"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request addValue:@"form-data" forHTTPHeaderField:@"Content-Disposition"];
[request setHTTPBody:jsonData];
[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES ];
currentRequestType = REGISTER_USER;
NSURLConnection *connection =[[NSURLConnection alloc]initWithRequest:request delegate:self];
ありがとう!
NSDictionary *userDict = [[NSDictionary alloc] initWithObjectsAndKeys:
userName,@"username",
password, @"password",
nil];
NSString *jsonString = [userDict JSONRepresentation];
NSData *jsonData = [jsonString dataUsingEncoding];
[request setHTTPBody:jsonData];
[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES ];
currentRequestType = REGISTER_USER;
NSURLConnection *connection =[[NSURLConnection alloc]initWithRequest:request delegate:self];
残念ながら私の投稿の値がまだサーバーで発見されていない:私は今、これをやっているの下に提案を使用して
更新
。私は何かが私のNSMutableURLRequestで間違っていると思う。
私はバックエンドでCodeIgniterを使用しています。アクションは次のようになります。
public function create(){
if($this->input->post('username') && $this->input->post('password')){
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->load->model('User_model','users');
$status = $this->users->add_user($username,$password);
if($status){
echo json_encode(array('success'=>true));
} else{
echo json_encode(array('success'=>false));
}
}
echo json_encode(array('success'=>false)); // when posting I arrive here
}
感謝を解放することを忘れないでくださいは。これ実際に私が後になっているjson文字列を返します。しかし、setHTTPBody:はNSDataをパラメータとして取ります。私は文字列を取得し、それをNSDataに変換しました。問題は残っている。それは私の中の何か他のものによって要求されなければならない。 – Nick
これをNSDataに変換する方法を示す追加ステップが追加されました* – DHamrick