从西班牙应用程序以西班牙语字符发送数据但从邮递员发送到宁静的Web服务的问题

时间:2018-09-28 18:36:11

标签: android postman

我有一个RESTful Web服务,它可以保存到数据库中。我使用JSON传递数据。 如果我从POSTMAN传递JSON,一切正常。如果我从Android应用程序传递数据(JSON),则如果存在西班牙字符,例如“ó,ñ等”,服务器将无法保存到数据库。

为什么是邮递员提供的,为什么不是来自android应用程序的??

我尝试添加ISO-8859-1,但是它不起作用。错误在哪里? SQL Server?,Android App?,IIS?如果数据不包含西班牙字符,那么一切都很好,如果使用西班牙字符,则无法正常工作。

android应用中的代码如下:

HttpClient httpClient = new DefaultHttpClient();

HttpPost httpPost;

httpPost = new HttpPost("http://"...

httpPost.setHeader("content-type", "application/json" );

httpPost.setHeader("charset", "ISO-8859-1");

JSONObject jSONObject = new JSONObject();

try
{
    jSONObject.put("Mensaje", params[0].mensaje);
    jSONObject.put("IdUsuarioOrigen", params[0].idUsuarioOrigen);
    jSONObject.put("IdUsuarioDestino", params[0].idUsuarioDestino);

    StringEntity stringEntity = new StringEntity(jSONObject.toString());

    stringEntity.setContentType("application/json");
    stringEntity.setContentEncoding("ISO-8859-1");
    httpPost.setEntity(stringEntity);

    HttpResponse httpResponse = httpClient.execute(httpPost);
    strHttpResponse = EntityUtils.toString(httpResponse.getEntity());

EDIT ::::::::::::::::::::::::::::::::::::

我要调试几天。重点是:

android应用中的代码为:

...     httpPost.setHeader(“ content-type”,“ application / json”);     httpPost.setHeader(“ charset”,“ ISO-8859-9”);

JSONObject jSONObject = new JSONObject();

        try
        {
            jSONObject.put("Mensaje", params[0].mensaje);
            jSONObject.put("IdUsuarioOrigen", params[0].idUsuarioOrigen);
            jSONObject.put("IdUsuarioDestino", 
params[0].idUsuarioDestino);

            StringEntity stringEntity = new 
StringEntity(jSONObject.toString());

            stringEntity.setContentType("application/json");
            stringEntity.setContentEncoding("ISO-8859-9");
            httpPost.setEntity(stringEntity);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            strHttpResponse = 
EntityUtils.toString(httpResponse.getEntity());

如果我在“ httpClient.execute(httpPost);”之前调试httpPost, httpPost可以,里面有数据(ó,ñ等),但是到达c#后端时,“ mensaje”为空:

public HttpResponseMessage PostMensaje(Mensajeria mensaje)
{
        if (mensaje == null)
            return 
Request.CreateResponse(HttpStatusCode.InternalServerError, "El mensaje no 
puede llegar al servidor");

但是,如果android应用中的httpPost没有任何西班牙语字符,则一切正常,并且后端“ Mensajeria mensaje”的“ mensaje”不为null。 为什么只为携带西班牙语字符才为null?。

有了邮递员,一切都很好,不管有没有西班牙字符。

我必须要说,我的最后一次尝试是在Web服务.net中添加web.config:

  <globalization
      fileEncoding="ISO-8859-9" 
      requestEncoding="ISO-8859-9" 
     responseEncoding="ISO-8859-9"


  />

但是当json到达Web服务时,“ mensaje”为空(但它在android代码中表现得很好)。如果.net中的JSON带有“ó,ñ等。” mensaje“为空,则一切都完美。

  public HttpResponseMessage PostMensaje(Mensajeria mensaje)
    {

1 个答案:

答案 0 :(得分:0)

尝试一下:

httpPost.setHeader("content-type", "application/json;charset=utf-8" );

strHttpResponse = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
相关问题