Objective c post with json data request to server -
i trying post data server , send data json this
data: {"userid":"aaaaa","token":"12345","type":"bbb","version":"45"}
nsdictionary *requestdictionary = @{@"data" : @{ @"{userid" : @"aaa", @"token ": @"12345",@"type":@"ios",@"version":@"1}"}}; nsurl *urls =[nsurl urlwithstring:[nsstring stringwithformat:@"http://url/send_code"]]; self.request = [[nsmutableurlrequest alloc]init]; [self.request seturl:urls]; nsstring *contenttype = [nsstring stringwithformat: @"application/json"]; [request addvalue:contenttype forhttpheaderfield: @"content-type"]; [request sethttpmethod:@"post"]; [request addvalue:@"ios" forhttpheaderfield: @"x-application-platform"]; [request setvalue:@"application/x-www-form-urlencoded" forhttpheaderfield:@"content-type"]; [request sethttpbody:[nsjsonserialization datawithjsonobject:requestdictionary options:0 error:nil]]; nsdata *postdata = [nsjsonserialization datawithjsonobject:requestdictionary options:0 error:nil]; urlconnection = [[nsurlconnection alloc]initwithrequest:request delegate:self startimmediately:true];
i ve tried several times , invalid data response
please me.thanks in advance
try send using afnetworking:
first try convert json dictionary.
nserror *error; nsdata *objectdata = [@"{your dictionary}" datausingencoding:nsutf8stringencoding]; nsdictionary *json = [nsjsonserialization jsonobjectwithdata:objectdata options:nsjsonreadingmutablecontainers error:&error];
then can send dictionary:
afhttpsessionmanager *manager = [afhttpsessionmanager manager]; manager.responseserializer.acceptablecontenttypes = [nsset setwithobject:@"text/html"]; [manager post:@"your_url" parameters:json progress:nil success:^(nsurlsessiondatatask * _nonnull task, id _nullable responseobject) { nslog(@"complete"); } failure:^(nsurlsessiondatatask * _nullable task, nserror * _nonnull error) { nslog(@"fail"); }];
Comments
Post a Comment