Json Http Post Android

本文关键字:Android Post Http Json | 更新日期: 2023-09-27 17:50:28

我正在将json数据发布到我的c#服务堆栈web服务。

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();

我从Log得到的响应。d (dasd, json);

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

这是有效的json。然而,我从webservice得到的响应是

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

这是无效的,我不知道为什么。

Webservice代码片段在

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

感谢

** Update **

我正在传递一个Json字符串从我的android应用程序到我的API和反序列化它。

        public object Any(String Json)
    {
        var mn = JsonSerializer.DeserializeFromString<Worksheets>(Json);
        var rs = new ResponseStatus()
        {
            Message = Json
        };
        return new WorksheetsResponse { ResponseStatus = rs };
    }

但是CustomerLine1, CustomerLine2和CustomerLine3没有从Worksheets类中填充。

似乎当应用程序的帖子到达API时,它被接收为

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

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

Json Http Post Android

return语句告诉服务以这种方式返回json。如果您希望嵌套对象返回JSON而不是字符串,您可以这样做:

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

如果你只想要记录的原始json字符串,那么不要在ResponseStatus对象中返回它。

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