Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用不同字符集编码的Android http post发送_Android_Rest_Restlet_Restlet 2.0 - Fatal编程技术网

使用不同字符集编码的Android http post发送

使用不同字符集编码的Android http post发送,android,rest,restlet,restlet-2.0,Android,Rest,Restlet,Restlet 2.0,我在android上用德语“umlaute”äöü发http帖子时遇到了一个问题。我将一个json对象传递给下面的方法,并在返回的客户机响应中执行返回的ClientResource with post和请求实体。当我想发布类似{“foo”:“bär”}的内容时,HttpClient会发送类似{“foo”:“b”的内容√§r“} 不知道为什么。我做错了什么 public static ClientResource newPostRequest(Context context, String urn

我在android上用德语“umlaute”äöü发http帖子时遇到了一个问题。我将一个json对象传递给下面的方法,并在返回的客户机响应中执行返回的ClientResource with post和请求实体。当我想发布类似{“foo”:“bär”}的内容时,HttpClient会发送类似{“foo”:“b”的内容√§r“}

不知道为什么。我做错了什么

public static ClientResource newPostRequest(Context context, String urn,
        JSONObject form) throws MissingAccessTokenException {

    ClientResource resource = new ClientResource(uri + urn);

    StringRepresentation sr = new StringRepresentation(form.toString());
    sr.setMediaType(MediaType.APPLICATION_JSON);

    resource.getRequest().setEntity(sr);

    return resource;
}
更新

我使用了默认的android http客户端(我相信这是一个apache http客户端),并得到了相同的错误。所以问题可能就在这里。我尝试实现另一个json解析器(目前是gson)和(如果可能的话)另一个http客户端。稍后回来

更新

格森不是问题所在。我在StringRepresentation中添加了一个json字符串,没有任何更改

回答

好吧,这很奇怪。也许有人能帮我弄清楚。我总是问自己,为什么√§在哪里使用,我发现翻译utf-8ä会导致√§. 显然,我的android手机没有使用macroman,但我的mac使用了。因此,我在eclipse中更改了文本文件编码,重新启动了eclipse和tomcat服务器,并且工作正常。eclipse中的TCP/IP监视器仍然使用mac roman,这看起来仍然是错误的。因此,这是我的服务器的问题,而不是android上的restlet客户端的问题。我就是看不到,因为TCP/IP监视器用macroman对所有内容进行编码

您是否尝试调用您的
StringRepresentation
?e、 g

StringRepresentation sr = new StringRepresentation(form.toString());
sr.setCharacterSet(CharacterSet.UTF_8);

首先,您提到了HttpClient,但您使用的是Restlet。你能解释一下吗?第二,您是如何确定数据被错误发送的,而不是在服务器上被错误解码的?Restlet不是在Android上使用DefaultHttpClient吗?我开始监视传入请求的服务器端口,这就是我确定客户端发送了什么的方式。restlet可以使用HttpClient,但它不一定使用它(在您的案例中,它似乎正在使用它)。无论如何,由于您使用的是restlet接口,因此需要在restlet上下文中解决这个问题。