简体   繁体   中英

Json Http Post Android

I am posting my json data to my c# service stack web service.

try {

        // 1. create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // 2. make POST request to the given URL
        HttpPost httpPost = new HttpPost(url);

        String json = "";


        // 3. build jsonObject
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("CustomerLine1", worksheet.getCustomerLine1());
        jsonObject.accumulate("CustomerLine2", worksheet.getCustomerLine2());
        jsonObject.accumulate("CustomerLine3", worksheet.getCustomerLine3());
        jsonObject.accumulate("Status", worksheet.getStatus());

        // 4. convert JSONObject to JSON to String
        json = jsonObject.toString();

        Log.d("dasd", json);

        // ** Alternative way to convert Person object to JSON string usin Jackson Lib 
        // ObjectMapper mapper = new ObjectMapper();
        // json = mapper.writeValueAsString(person); 

        // 5. set json to StringEntity
        StringEntity se = new StringEntity(json);

        // 6. set httpPost Entity
        httpPost.setEntity(se);



        // 7. Set some headers to inform server about the type of the content
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        // 8. Execute POST request to the given URL
        HttpResponse httpResponse = httpclient.execute(httpPost);

        // 9. receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

The response I get from Log.d("dasd", json); is

{"CustomerLine1":"gh","CustomerLine2":"dg","CustomerLine3":"eg","Status":0}

Which is valid json. However the response I get back from my webservice is

{"responseStatus":{"message":"{CustomerLine1:gh,CustomerLine2:dg,CustomerLine3:eg,Status:0}"}

Which is not valid and I have no idea why.

Webservice snippit is below

    [EnableCors(allowedMethods: "POST")]
public class WorksheetService : Service
{
    public object Any(String Json)
    {
        var rs = new ResponseStatus()
        {
            Message = Json
        };

        return new WorksheetsResponse { ResponseStatus = rs };
    }
}

Thanks

** Update **

I'm passing a Json string from my android app to my API and the de serializing it.

        public object Any(String Json)
    {
        var mn = JsonSerializer.DeserializeFromString<Worksheets>(Json);

        var rs = new ResponseStatus()
        {
            Message = Json
        };

        return new WorksheetsResponse { ResponseStatus = rs };
    }

But CustomerLine1, CustomerLine2 and CustomerLine3 are not being populated from the Worksheets class.

It seems that when the post from the app is reaching the API its being received as

{CustomerLine1:gh,CustomerLine2:dg,CustomerLine3:eg,Status:0}

And not

{"CustomerLine1":"gh","CustomerLine2":"dg","CustomerLine3":"eg","Status":0}

The return statement is what is telling the service to return the json that way. If you want the nested object to return JSON instead of a string, you can do this:

public class WorksheetService : Service
    {
        public object Any(JsonObject Json) 
        {
            var rs = new ResponseStatus()
            {
                Message = Json
            };

            return new WorksheetsResponse { ResponseStatus = rs }; 
        }
    }

If you just want the raw json string that you're logging, then don't return it wrapped in a ResponseStatus object.

public class WorksheetService : Service
        {
            public object Any(JsonObject Json) 
            {
                return Json; 
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM