gpt4 book ai didi

java - Android 应用程序和 Spring Rest 服务之间的 httpost 字符编码问题

转载 作者:行者123 更新时间:2023-12-01 22:57:14 25 4
gpt4 key购买 nike

我正在尝试让我的 Android 应用程序与我的 Spring REST Web 服务对话。我正在从 android 制作 httpost ,如下代码。但是我收到下面的异常。无论我为帖子请求的内容类型设置什么,它都不起作用。如果我的帖子对象中没有非英语字符,则效果很好。但是,如果存在 şŞüÜöÖgG 等,则会给出该错误。我需要你的建议。

public SowHttpUtil executeConnection(Object obj) throws Exception {
try{
HttpPost httppost = new HttpPost(serviceUrl);
if(obj != null){
StringEntity se = new StringEntity(objToString(obj));
se.setContentEncoding(HTTP.UTF_8);
se.setContentType("application/json");
httppost.setEntity(se);
}
HttpClient httpclient = new DefaultHttpClient();
//httppost.setHeader("Accept", "application/json");
//httppost.setHeader("Content-Type", "application/json;charset="+HTTP.UTF_8);
//httppost.addHeader("Accept", "application/json");
//httppost.addHeader("Content-Type", "application/json;charset="+HTTP.UTF_8);

HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if(response.getStatusLine().getStatusCode() == 200){
InputStream inStream = entity.getContent();
responseString = convertStreamToString(inStream);
}else{
Log.e("RESPONSE STATUS ERROR : " , "RSCode : "+response.getStatusLine().getStatusCode());
throw new SowException(R.string.error4);
}
} catch(HttpHostConnectException e){
throw new SowException(R.string.error4);
}
return this;
}

我在服务器端遇到的异常( Spring ):

Could not read JSON: Invalid UTF-8 start byte 0xfc
at [Source: org.apache.catalina.connector.CoyoteInputStream@79e0123b; line: 3, column: 29]; nested exception is org.codehaus.jackson.JsonParseException: Invalid UTF-8 start byte 0xfc
at [Source: org.apache.catalina.connector.CoyoteInputStream@79e0123b; line: 3, column: 29]
org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Invalid UTF-8 start byte 0xfc
at [Source: org.apache.catalina.connector.CoyoteInputStream@79e0123b; line: 3, column: 29]; nested exception is org.codehaus.jackson.JsonParseException: Invalid UTF-8 start byte 0xfc
at [Source: org.apache.catalina.connector.CoyoteInputStream@79e0123b; line: 3, column: 29]
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readJavaType(MappingJacksonHttpMessageConverter.java:187)
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.read(MappingJacksonHttpMessageConverter.java:179)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:138)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:183)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:98)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: org.codehaus.jackson.JsonParseException: Invalid UTF-8 start byte 0xfc
at [Source: org.apache.catalina.connector.CoyoteInputStream@79e0123b; line: 3, column: 29]
at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1433)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:521)
at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidInitial(Utf8StreamParser.java:2825)
at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidChar(Utf8StreamParser.java:2819)
at org.codehaus.jackson.impl.Utf8StreamParser._finishString2(Utf8StreamParser.java:1978)
at org.codehaus.jackson.impl.Utf8StreamParser._finishString(Utf8StreamParser.java:1905)
at org.codehaus.jackson.impl.Utf8StreamParser.getText(Utf8StreamParser.java:276)
at org.codehaus.jackson.map.deser.std.StringDeserializer.deserialize(StringDeserializer.java:26)
at org.codehaus.jackson.map.deser.std.StringDeserializer.deserialize(StringDeserializer.java:13)
at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299)
at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:414)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:697)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1923)
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readJavaType(MappingJacksonHttpMessageConverter.java:184)
... 38 more

最佳答案

你应该使用

new StringEntity(content, HTTP.UTF_8);

相反。

StringEntity 的构造函数使用字符集对提供的字符串调用 getBytes()(检查 source code )。如果未提供字符集,则使用默认字符集(并且不是 UTF-8)。

之后调用 setContentEncoding() 会更改 HTTP header ,但不会更改内容本身。因此不匹配(0xfc 不是有效的 UTF-8)

关于java - Android 应用程序和 Spring Rest 服务之间的 httpost 字符编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23849099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com