objective c - how to handle space in sending json parameters to server in ios? -
i sending data server going successful response coming %20 @ space in data have sent server here code using
nsstring *str = [nsstring stringwithformat:@"http://www.me911.com/new/miphone3/android_edithealth.php?profile_id=%@&health_condition=%@&health_insurance_provider=%@&primary_physician_name=%@&primary_physician_phone=%@&last_physical=%ld&blood_type=%@&organ_donor=%@",profileid,txthospital.text,textinsurence.text,txtprimary.text,txtphone.text,dateinmillis,questionno,textorgan.text]; str = [str stringbyreplacingoccurrencesofstring:@" " withstring:@"%20"]; nslog(@"healthinfo url: %@",str); nsmutableurlrequest *datarqst = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:str] cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:30.0]; [datarqst sethttpmethod:@"post"]; nsstring *stringboundary = @"0xkhtmlboundary---this_is_the_boundaryy---pqo"; nsstring *headerboundary = [nsstring stringwithformat:@"multipart/form-data; boundary=%@",stringboundary]; [datarqst addvalue:headerboundary forhttpheaderfield:@"content-type"]; nsmutabledata *postbody = [nsmutabledata data]; [datarqst sethttpbody:postbody]; nshttpurlresponse *dataresponse =[[nshttpurlresponse alloc] init]; nserror* error = [[nserror alloc] init] ; //synchronous filling of data http post response nsdata *responsedata = [nsurlconnection sendsynchronousrequest:datarqst returningresponse:&dataresponse error:&error]; //convert data string nsstring *responsestring = [[nsstring alloc] initwithbytes:[responsedata bytes] length:[responsedata length] encoding:nsutf8stringencoding]; nslog(@"responsestring %@ ",responsestring); if (responsestring == null) { nsdictionary *infodic = [[nsdictionary alloc] initwithobjectsandkeys:@"there small problem", @"title", @"the network doesn't seem responding, please try again.", @"message", @"ok", @"cancel", @"1", @"tag",nil, @"delegate", nil]; [commonfunctions performselectoronmainthread:@selector(showalertwithinfo:) withobject:infodic waituntildone:no]; } else { nsdictionary *jsonresponse = [responsestring jsonvalue]; if ([jsonresponse objectforkey:@"error"]){ nslog(@"response %@",jsonresponse); } else{ } nsmutablearray *dataresponse=[jsonresponse valueforkey:@"success"]; if ([jsonresponse objectforkey:@"success"]) { nslog(@"array response %@",dataresponse); } }
and web service
here sending data in textfield in guru prasad response getting guru%20prasad.
so please correct me going wrong .
thanks in advance
your code has number of issues.
in order create url query params, recommend use utility class nsurlcomponents
(see apple documentation: nsurlcomponents).
composing post request content type "multipart/formdata" quite error prone. if absolutely have compose such request strongly recommend use network library, example afnetworking.
on other hand, using post request content-type application/json
easy setup, nsurlsession , friends.
you can find specific solutions of suggested approaches mentioned above on so, too.
Comments
Post a Comment