0

I'm trying to send some JSON data to an asp.net web api controller like this (from here):

  JSONObject Parent = new JSONObject();

        Parent.put("enterpriseId", "55e8a2a3-466d-46dc-95ce-bc5f2d3e7828");
        List<Integer> lst = new ArrayList<Integer>();
        lst.add(1);
        lst.add(2);
        lst.add(3);
        lst.add(4);
        lst.add(5);
        Parent.put("itemids", lst);

        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPostRequest = new HttpPost(Financial.AccountReviewUrl+"/testtesttest");

        StringEntity se;
        se = new StringEntity(Parent.toString());
        //Set HTTP parameters
        httpPostRequest.setEntity(se);
        httpPostRequest.setHeader("Accept", "application/json");
        httpPostRequest.setHeader("Content-type", "application/json; charset=utf-8");
        httpPostRequest.setHeader("AuthenticationToken", Financial.UserEncrypt); 

        Log.d("json is", Parent.toString());
        Log.d("use encryp is", Financial.UserEncrypt);


        return httpclient.execute(httpPostRequest);

My web api action:

        [HttpPost]
        public Object TestTestTest(Guid enterpriseId, List<int> itemIds)
        {
            var count = 0;
            if (itemIds != null)
                count = itemIds.Count;

            return enterpriseId.ToString() + ":" + count.ToString();

        }

And I keep getting a 404 error. I know that this has been asked multiple times previously but none of the recommended answers seem to work for me. Any ideas?

i see this and this and ... answer but dont worked for me

Community
  • 1
  • 1
Shayan Pourvatan
  • 11,622
  • 4
  • 39
  • 62

1 Answers1

2

Your API expects a form post not JSON Object. For POSTing a JSON Object you will do something as follows

// Code snippet for posting JSONObject
HttpPost httpPost = new HttpPost(serviceUrl);
MultipartEntity multipartEntity = new MultipartEntity();    
multipartEntity.addPart("data", new StringBody(jsonObject.toString()));
httpPost.setEntity(multipartEntity);
try {
    HttpResponse response = httpClient.execute(httpPost);
} catch (Exception ex) {
    // Log the error
}

Since your code expects the URL encoded form data, you will do that as follows

// Code snippet for posting form data
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serviceUrl);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
for (int i = 0; i < params.length; i++) {
    nameValuePairs.add(new BasicNameValuePair(params[i], values[i]));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairs);
httpPost.setEntity(formEntity);
try {
    HttpResponse response = httpClient.execute(httpPost);
} catch (Exception ex) {
    // Log the error
}

In the second code snippet, you will build two NameValuePair objects, one for enterpriseId and other for itemids.

PS: These are code snippets, you will have to modify them as per your use.

Hope this helps.

jagmohan
  • 2,012
  • 1
  • 26
  • 38