我有一台服务器,上面有一个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)
到底是怎么回事?是托管公司还是我?
编辑:这是我正在使用的罐子的列表。
asm-3.1.jar
杰克逊核心-asl-1.8.3.jar
jackson-jaxrs-1.8.3.jar
杰克逊-映射器-asl-1.8.3.jar
杰克逊-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标头又回来了-应该是这样的:
Content-Type: application/json; charset=ISO-8859-1
但看起来好像又回来了:
Content-Type: application/json; charset=ISO-8859-1, application/json
或类似的东西。用类似Wireshark的标题
这看起来像问题:
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
我怀疑不应该是编码的一部分-它是内容类型的一部分。尝试将其更改为:
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));