ios - when request post jsondata requested URL show blank field only dictionary key are viewd -
nsdictionary *dictionary =[nsjsonserialization jsonobjectwithdata: [@"{\"id\":\"7\",\"user\":\"alok7@gmail.com\",\"name\":\"ashok\",\"gender\":\"male\",\"price\":\"898989\",\"place\":\"disa\",\"time\":\"2014-10-22\",\"given_taken\":\"given by\"}" datausingencoding:nsutf8stringencoding]options: nsjsonreadingmutablecontainers error: nil]; nsstring *jsonrequest = [nsstring stringwithformat:@"%@",dictionary];// jsondict]; nslog(@"jsonrequest %@", jsonrequest); nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url1]; [request sethttpmethod:@"post"]; [request sethttpbody:[jsonrequest datausingencoding:nsutf8stringencoding]]; [request setvalue:@"application/json" forhttpheaderfield:@"accept"]; [request setvalue:@"application/json" forhttpheaderfield:@"content-type"]; [request setvalue:[nsstring stringwithformat:@"%ld",(long)[jsonrequest length]] forhttpheaderfield:@"content-length"];
output on requested link:
{"id":"261","user":"","given_taken":"","name":"","gender":"","price":"0","place":"","time":""},{"id":"262","user":"","given_taken":"","name":"","gender":"","price":"0","place":"","time":""},{"id":"263","user":"","given_taken":"","name":"","gender":"","price":"0","place":"","time":""}],"success":1}
in original question created nsmutableurlrequest
, called [[nsurlconnection alloc] initwithrequest]
, proceeded try finish configuring request.
but, initwithrequest
start request, make sure finish configuring request before instantiating nsurlconnection
.
also, don't call start
because connection automatically started you. have start
if created nsurlconnection
startimmediately
of no
, not appropriate here.
in revised question, included code says:
nsdictionary *dictionary =[nsjsonserialization jsonobjectwithdata: [@"{\"id\":\"7\",\"user\":\"alok7@gmail.com\",\"name\":\"ashok\",\"gender\":\"male\",\"price\":\"898989\",\"place\":\"disa\",\"time\":\"2014-10-22\",\"given_taken\":\"given by\"}" datausingencoding:nsutf8stringencoding]options: nsjsonreadingmutablecontainers error: nil]; nsstring *jsonrequest = [nsstring stringwithformat:@"%@",dictionary];
in you're manually constructing json string, converting nsdata
, converting nsdictionary
(using nsjsonreadingmutablecontainers
option reason). you're using stringwithformat
, take dictionary , convert string (but not valid json) , sending that.
i'd suggest (a) don't create json strings manually, that's fraught simple typographical mistakes; (b) create nsdictionary
, create nsdata
directly that. example:
nsdictionary *jsondictionary = @{ @"id" : @"7", @"user" : @"alok7@gmail.com", @"name" : @"ashok", @"gender" : @"male", @"price" : @"898989", @"place" : @"disa", @"time" : @"2014-10-22", @"given_taken": @"given by" }; nserror *error; nsdata *data = [nsjsonserialization datawithjsonobject:jsondictionary options:0 error:&error]; nsassert(data, @"datawithjsonobject failed: %@", error); request.httpbody = data;
by way, unrelated question, don't have set content-length
because nsurlconnection
you.
Comments
Post a Comment