我有一個服務器上有一個servlet,它充當我的移動應用程序的後端。當我在Eclipse中測試它時,它在我的計算機上工作正常,但是當我嘗試從服務器獲取數據時,它會在日誌中出現。嘗試使用JSON將http發佈到服務器,得到:java.io.UnsupportedEncodingException
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
傑克遜 - JAXRS-1.8.3.jar
傑克遜映射器-ASL-1.8.3的.jar
傑克遜-XC-1.8.3.jar
球衣,客戶1.10.jar
球衣-COR E-1.10.jar
球衣,JSON-1.10.jar
球衣 - 服務器1.10.jar
球衣,servlet的1.10.jar
拋放-1.1.jar
JSR311-API-1.1.1.jar
源碼-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;
}
}
}
在那裏的異步任務,我認爲,是學士學位並不需要在那裏。一位教授讓我把它放在那裏作爲任務的一部分。 (這不是順便說一下家庭作業幫助,那下課)
我們可以看到您的代碼形成JSON併發布它嗎?它似乎不喜歡ISO8859-1 - 你可以試試UTF-8嗎? – JTeagle 2012-03-15 08:11:19
我不知道如何告訴它發送UTF-8,因爲所有有碰巧是se.setContentEncoding(新的BasicHeader(HTTP.CONTENT_TYPE,「application/json」)); – tricknology 2012-03-15 08:17:53