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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

thanks for your answer, i did second way and its worked, but i need send Json. i try first way and will tell you the result
what is MultipartEntity class?
Ok. For sending JSON, I believe you will have to modify your API as well.
org.apache.http.entity.mime.MultipartEntity
could you please tell me how can I put one list to BasicNameValuePair?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.