Asynchronous POST request parsing JSON
First, create a URL
Nsurl*url = [Nsurlurlwithstring:@ "Http://localhost:8080/MJServer/order"];
Second, create aRequest
nsmutableurlrequest*request = [nsmutableurlrequestRequestwithurl: URL];
Third, setRequest Method
Request.HttpMethod= @ "POST";
Five,Set the request body (Request parameters)
//Create a description of the order informationJSONDatansmutabledictionary*OrderInfo = [nsmutabledictionaryDictionary];OrderInfo[@ "shop_id"] = @ "123";OrderInfo[@"Shop_name"] = @ "123";OrderInfo[@"user_id"] = @ "123";//translate the dictionary into data
NSData*bodydata= [nsjsonserializationDatawithjsonobject: OrderInfoOptions:nsjsonwritingprettyprintedError:Nil];
Request.Httpbody= Bodydata;
Six,set the request header: The data for this request body is no longer a normal parameter, but aJSONData
[Request SetValue:@ "Application/json"Forhttpheaderfield:@ "Content-type"];
Seven,Send Request
[nsurlconnectionsendasynchronousrequest: RequestQueue:[NsoperationqueueMainqueue]Completionhandler:^(Nsurlresponse*response, NSData *data,Nserror *connectionerror) {
if(data = = Nil | | | connectionerror)return;
nsdictionary*dict = [nsjsonserializationJsonobjectwithdata:d ATAOptions:nsjsonreadingmutableleavesError:Nil];
NSString*error = dict[@ "Error"];
if(Error) {
[MbprogresshudShowError: ERROR];
} Else {
NSString*success = dict[@ "Success"];
[Mbprogresshudshowsuccess: Success];
}
}];
Note:[nsjsonserialization datawithjsonobject: orderInfo Options : nsjsonwritingprettyprinted Error:nil]
Convert JSON to binary data[nsjsonserializationjsonobjectwithdata:d ataoptions: NsjsonreadingmutableleavesError:nil];
turn binary data into JSONset the request header: The data for this request body is no longer a normal parameter, but a JSON Data
[Request SetValue : @ "Application/json" Forhttpheaderfield : @ "Content-type" ];
Asynchronous POST request parsing JSON