在android [重复]中JSON和SERVLET出错了

时间:2021-11-21 11:50:23

This question already has an answer here:

这个问题在这里已有答案:

I have a servlet which makes a connection to SQL server, gets resultset, and sends a string to the Android client. Untill here it is all fine.

我有一个servlet,它连接到SQL服务器,获取结果集,并将一个字符串发送到Android客户端。直到这里一切都很好。

Now I woull like to send objects to the client, so I understood I need to use JSON. I added the jar of JSON to the build path of the servlet and of the client. .Eclipse recognized it and I complied successfully.

现在我想将对象发送到客户端,所以我知道我需要使用JSON。我将jar的JSON添加到servlet和客户端的构建路径中。 .Eclipse认出了它,我顺利完成了。

This is in the servlet part:

这是在servlet部分:

List<String> myListOfStrings = new ArrayList<String>(); 
myListOfStrings.add("first word");
myListOfStrings.add("second word");
myListOfStrings.add("third word");

JSONArray arrOfJSON=new JSONArray();          
for (String s : myListOfStrings){
   arrOfJSON.put(s);
}
//Here I serialize the stream to a String.
final String output = arrOfJSON.toString();
response.setContentLength(output.length());
//And write the string to output.
response.getOutputStream().write(output.getBytes());
response.getOutputStream().flush();
response.getOutputStream().close();

The problem is on RUN-time:

问题出在运行时间:

java.lang.ClassNotFoundException: org.json.JSONArray
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    at Servlet2ForLNM_Pack.Servlet2ForLNM_Class.doPost(Servlet2ForLNM_Class.java:1374)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    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.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    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:504)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    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:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

May someone help me to understand where did I go wrong?

愿有人帮我理解我哪里出错了吗?

2 个解决方案

#1


  1. Check that your jar is placed in /WebContent/WEB-INF/lib
  2. 检查您的jar是否放在/ WebContent / WEB-INF / lib中

  3. Tell eclipse to include the jar in war file when building the project source
  4. 在构建项目源时,告诉eclipse将war包含在war文件中

I suggest that you move to maven build system. Maven allows you to configure whole project from single file(no eclipse configuration required).

我建议你转移到maven构建系统。 Maven允许您从单个文件配置整个项目(不需要eclipse配置)。

You run mvn clean install from project root directory to build war file. Your pom.xml would look like this

您从项目根目录运行mvn clean install来构建war文件。你的pom.xml看起来像这样

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>yourGroupId</groupId>
    <artifactId>yourArtifactId</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <!-- this is important, other libs can be excluded -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
            <version>3.0.1</version>
        </dependency>
        <!-- your json lib -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20141113</version>
        </dependency>
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.8.7</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>your app name</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

#2


This error arised because java is unable to get the classes used by you. Download java-json.jar file.

出现此错误是因为java无法获取您使用的类。下载java-json.jar文件。

You have to follow following steps

您必须按照以下步骤操作

1) You have to add the jar file of json in the java path bin and lib

1)你必须在java路径bin和lib中添加json的jar文件

2) you also need to add the jar file of json in server lib folder.

2)您还需要在服务器lib文件夹中添加json的jar文件。

Restart the server the problem will be solved

重启服务器问题将解决

#1


  1. Check that your jar is placed in /WebContent/WEB-INF/lib
  2. 检查您的jar是否放在/ WebContent / WEB-INF / lib中

  3. Tell eclipse to include the jar in war file when building the project source
  4. 在构建项目源时,告诉eclipse将war包含在war文件中

I suggest that you move to maven build system. Maven allows you to configure whole project from single file(no eclipse configuration required).

我建议你转移到maven构建系统。 Maven允许您从单个文件配置整个项目(不需要eclipse配置)。

You run mvn clean install from project root directory to build war file. Your pom.xml would look like this

您从项目根目录运行mvn clean install来构建war文件。你的pom.xml看起来像这样

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>yourGroupId</groupId>
    <artifactId>yourArtifactId</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <!-- this is important, other libs can be excluded -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
            <version>3.0.1</version>
        </dependency>
        <!-- your json lib -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20141113</version>
        </dependency>
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.8.7</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>your app name</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

#2


This error arised because java is unable to get the classes used by you. Download java-json.jar file.

出现此错误是因为java无法获取您使用的类。下载java-json.jar文件。

You have to follow following steps

您必须按照以下步骤操作

1) You have to add the jar file of json in the java path bin and lib

1)你必须在java路径bin和lib中添加json的jar文件

2) you also need to add the jar file of json in server lib folder.

2)您还需要在服务器lib文件夹中添加json的jar文件。

Restart the server the problem will be solved

重启服务器问题将解决