2

I am developing one iOS application that Post data to web service and Get data from the Web service. I am using Afnetworking for Post the data to web service.Web service accept the data in the form of array of dictionaries like

[{"name":"stephen","age":25},{"name":"john","age":35},{"name":"david","age":45},{"name":"roger","age":15}]

which need to send in the body of the request and set Accept application/json in the header of the request.

I checked some examples already avilable in the stack overflow but all are explain about Post dictionary .My question is possible to Post array to web service using Afnetworking. If it is possible help me to develop the code which completely reach my requirements.

user3762713
  • 101
  • 3
  • 13
  • Yes it is ,if you accept the array against a key – amar Aug 21 '14 at 05:26
  • @amar array against a key ? would you please explain about that. – user3762713 Aug 21 '14 at 05:32
  • If you are sending it in post then it has to be against a key value.you will send a dictionary which will have a key : – amar Aug 21 '14 at 05:34
  • @amar {key: [{"name":"stephen","age":25},{"name":"john","age":35},{"name":"david","age":45},{"name":"roger","age":15}]} like this type? – user3762713 Aug 21 '14 at 05:36
  • yes but you dont ave to do it jsonrequestserializer will do it for you – amar Aug 21 '14 at 05:49
  • @amar I am new in objective c would you please Put your comment as answer with code.It will help me to solve this issue.I already spend 2 days to solve this issue. If it will work for me i will mark as answer. – user3762713 Aug 21 '14 at 06:01
  • it depends on what the PHP guy wants from you in post – amar Aug 21 '14 at 06:05
  • @amar Server is not in PHP it is in java. they want the object in [{"name":"stephen","age":25},{"name":"john","age":35},{"name":"david","age":45},‌​{"name":"roger","age":15}] format and object need to send in body and header set as Accept application/json – user3762713 Aug 21 '14 at 06:08
  • can you post the code , what you have tried & where its failing ? here is similar answer for posting at java server. http://stackoverflow.com/questions/7404559/sending-a-json-via-post-in-nsurlrequest – Pawan Rai Aug 21 '14 at 06:28

2 Answers2

0

If your are sending all dictionaries in single array. Very first you have to JSONserialize every dictionary and then put it in array and then again JSON serialise the whole array. Then only it will be called as a correct JSON format. Its the only way you can do it.

Chetan
  • 2,004
  • 1
  • 13
  • 21
0

My suggestion for you is to use AFJSONRequestSerializer.

Swift solution:

    let manager = AFHTTPSessionManager(baseURL: URL(string: yourBaseURL))
    manager.requestSerializer = AFJSONRequestSerializer()
    let parameters: [[String: Any]] = [["name": "stephen", "age": 25], ["name": "john", "age": 35],["name": "david", "age": 45]]
    manager.post(requestURL,
         parameters: parameters,
         progress: nil,
         success: { (task, data) in
            // process server response

    }, failure: {  (task, error) in
           // process error server  response
    })

AFJSONRequestSerializer sets Content-type to application/json in request header.

Stacy Smith
  • 490
  • 5
  • 11