Posting a JSON array to webservice in Android

Posted by Sam on Stack Overflow See other posts from Stack Overflow or by Sam
Published on 2010-04-08T06:06:23Z Indexed on 2010/04/08 6:13 UTC
Read the original article Hit count: 739

Filed under:
|
|
|

I am having some problems with what should be a rather simple task. I simply need a JSON array with a single JSON object within it to be posted to my webservice. The entire URL request needs to be formatted like this:

http://www.myserver.com/myservice.php?location_data=[{"key1":"val1","key2":"val2"....}]

I cannot for the life of me figure out how to append the 'location_data' bit using HttpPost. Here is a code snippet to demonstrate the HTTP connection method I am using:

    HttpClient hClient = new DefaultHttpClient();
    HttpPost hPost = new HttpPost(url);

    try {
        hPost.setEntity(new StringEntity(string));
        hPost.setHeader("Accept", "application/json");
        hPost.setHeader("Content-type", "application/json");

        //execute request
        HttpResponse response = (HttpResponse) hClient.execute(hPost);
        HttpEntity entity = response.getEntity();

I don't have any syntax errors, and my code is accessing the server fine, just not in the exact format the server needs. Any help on how to format my request to look like how I need it would be greatly appreciated!

© Stack Overflow or respective owner

Related posts about android

Related posts about java