Java Json Http Post Android
我正在将json数据发布到我的c#服务堆栈web服务中Java Json Http Post Android,java,c#,android,json,web-services,Java,C#,Android,Json,Web Services,我正在将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 = "";
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)得到的响应;是
这是有效的json。然而,我从我的Web服务得到的回应是
{"responseStatus":{"message":"{CustomerLine1:gh,CustomerLine2:dg,CustomerLine3:eg,Status:0}"}
这是无效的,我不知道为什么
下面是Webservice snippit
[EnableCors(allowedMethods: "POST")]
public class WorksheetService : Service
{
public object Any(String Json)
{
var rs = new ResponseStatus()
{
Message = Json
};
return new WorksheetsResponse { ResponseStatus = rs };
}
}
谢谢
**更新**
我正在将一个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":"gh","CustomerLine2":"dg","CustomerLine3":"eg","Status":0}
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;
}
}
你在用“mn”对象做什么?这段代码仍然显示Message=Json,它是一个字符串而不是一个JsonObject。它应该用发布的值填充Worksheets类。
public class WorksheetService : Service
{
public object Any(JsonObject Json)
{
var rs = new ResponseStatus()
{
Message = Json
};
return new WorksheetsResponse { ResponseStatus = rs };
}
}
public class WorksheetService : Service
{
public object Any(JsonObject Json)
{
return Json;
}
}