gpt4 book ai didi

java - 尝试使用 JSON 将 http 发布到服务器,获取 : java. io.UnsupportedEncodingException

转载 作者:行者123 更新时间:2023-11-29 06:02:10 24 4
gpt4 key购买 nike

我有一台服务器,上面有一个 servlet,用作我的移动应用程序的后端。当我在 Eclipse 中测试它时,它在我的计算机上运行良好,但是当我尝试从服务器获取数据时,它出现在日志中。

Mar 15, 2012 4:38:37 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Hello] in context with path [] threw exception
java.io.UnsupportedEncodingException: The character encoding [ISO-8859-1, application/json] is not supported
at org.apache.tomcat.util.buf.B2CConverter.getCharset(B2CConverter.java:74)
at org.apache.tomcat.util.buf.B2CConverter.reset(B2CConverter.java:159)
at org.apache.tomcat.util.buf.B2CConverter.<init>(B2CConverter.java:97)
at org.apache.catalina.connector.InputBuffer.setConverter(InputBuffer.java:556)
at org.apache.catalina.connector.InputBuffer.checkConverter(InputBuffer.java:518)
at org.apache.catalina.connector.Request.getReader(Request.java:1238)
at org.apache.catalina.connector.RequestFacade.getReader(RequestFacade.java:505)
at com.myserver.server.Hello.decodeRequest(Hello.java:319)
at com.myserver.server.Hello.doPost(Hello.java:93)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$SocketWithOptionsProcessor.run(AprEndpoint.java:1763)
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)

这是怎么回事?是托管公司还是我?

编辑:这是我正在使用的 jar 的列表。

asm-3.1.jar

jackson 核心-asl-1.8.3.jar

jackson -jaxrs-1.8.3.jar

jackson-mapper-asl-1.8.3.jar

jackson -xc-1.8.3.jar

jersey-client-1.10.jar

jersey-core-1.10.jar

Jersey -json-1.10.jar

jersey-server-1.10.jar

Jersey -servlet-1.10.jar

jettison-1.1.jar

jsr311-api-1.1.1.jar

sqlite-jdbc-3.7.2.jar

发布 JSON 的代码:

public class SendJSON{
JSONArray outr = new JSONArray();

GetSet gs = new GetSet();
URI u;
String out;

public JSONArray sendJ(final JSONArray json) throws JSONException{

ATask task = new ATask(json);
AsyncTask<URL, Integer, JSONArray> t = task.execute();
try {
outr = t.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return outr;
}

public JSONArray sendJ2(final JSONArray json) throws JSONException {

try {
u = new URI("http://mycensoredurl/Hello");
} catch (URISyntaxException e1) {
e1.printStackTrace();
}

HttpClient client = new DefaultHttpClient();

HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit HttpResponse response;
HttpPost post = new HttpPost(u);

StringEntity se = null;
try {

se = new StringEntity( json.toString());
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

post.setEntity(se);

StringBuilder builder = new StringBuilder();
try {
HttpResponse response = client.execute(post);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
Log.d("DEBUG", "status code = 200");
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e(SendJSON.class.toString(), "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d("JSON RESPONSE SHOULD", "BE NEXT..");

Log.d("JSON RESPONSE LENGTH", ""+ builder.length());
out = builder.toString();
gs.setOut(out);

Log.d("SendJSON... JSON RESPONSE String", ""+ out);
return new JSONArray(out);
}

private class ATask extends AsyncTask<URL, Integer, JSONArray>{
private JSONArray jary;

private ATask(JSONArray json) {
this.jary = json;
}

@Override
protected JSONArray doInBackground(URL... params) {
try {
outr = sendJ2(jary);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return outr;
}

}

我认为那里的异步任务是 b.s。并且不需要在那里。作为作业的一部分,一位教授让我把它放在那里。 (顺便说一句,这不是作业帮助,那节课已经结束了)

最佳答案

看起来您的 Content-Type header 又坏了 - 它应该是这样的:

Content-Type: application/json; charset=ISO-8859-1

但看起来它可能会回来:

Content-Type: application/json; charset=ISO-8859-1, application/json

或类似的东西。查看带有 Wireshark 之类的 header

这看起来像问题:

se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

我怀疑 application/json 不应该是编码的一部分——它是内容类型的一部分。尝试将其更改为:

se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

关于java - 尝试使用 JSON 将 http 发布到服务器,获取 : java. io.UnsupportedEncodingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9715902/

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