Я отправляю свои данные json в свой веб-сервис стека службы C#.Json Http Post Android
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); is
{"CustomerLine1":"gh","CustomerLine2":"dg","CustomerLine3":"eg","Status":0}
Который является действительным json. Однако ответ, который я получил от моего веб-сервиса, -
{"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 из моей андроид приложение к моему 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}
Что вы делаете с объектом «mn»? Этот код по-прежнему показывает Message = Json, который является строкой, а не JsonObject. –
Объект mn есть только в тот момент, когда я просматриваю код. Он должен заполнить класс Worksheets значениями, опубликованными. –