0

当托管在 i-Jetty 上时,有谁知道如何调试使用 Android API 的 java servlet?

对于常规网站(不在 Android / i-jetty 上),可以使用 Debug As -> Debug On Server 在本地服务器上使用 Eclipse EE 对其进行调试。

对于 Android 应用程序,我使用 Android Development Tools for Eclipse 并运行 Debug As -> Android Application。

但是,在这种情况下,我将 Android 代码打包在一个战争中。

我使用以下 POM.xml 文件和 maven 来打包 war 文件以与 i-Jetty 一起使用。它运行 dx 实用程序将 servlet 从 Java 转换为 Dalvik。我可以部署结果并在 Android 上的 i-jetty 上运行它。但是,我不知道如何使用除 LogCat 打印输出之外的任何其他方法来调试 servlet 调用。我考虑尝试在 Eclipse 中构建 i-jetty(它也是用 maven 构建的)并查看 ADT 是否可以将执行跟踪到 servlet,但不确定该路径是否会取得成果。

  <?xml version="1.0" encoding="UTF-8" ?> 
  <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0      http://maven.apache.org/maven-v4_0_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <parent>
  <groupId>org.mortbay.ijetty</groupId> 
  <artifactId>example-webapps-parent</artifactId> 
  <version>3.2-SNAPSHOT</version> 
  <relativePath>../pom.xml</relativePath> 
  </parent>
  <modelVersion>4.0.0</modelVersion> 
  <artifactId>blahblah</artifactId> 
  <name>I-Jetty :: blahblah</name> 
  <version>1.0-SNAPSHOT</version> 
  <packaging>war</packaging> 
  <url>http://maven.apache.org</url> 
  <build>
  <plugins>
  <plugin>
  <artifactId>maven-compiler-plugin</artifactId> 
  <configuration>
  <source>1.5</source> 
  <target>1.5</target> 
  <verbose>false</verbose> 
  </configuration>
  </plugin>
  <!--  Convert the compiled classes into a clases.dex. 
  --> 
  <plugin>
  <groupId>org.codehaus.mojo</groupId> 
  <artifactId>exec-maven-plugin</artifactId> 
  <version>1.1</version> 
  <executions>
  <execution>
  <id>generate-dex</id> 
  <phase>process-classes</phase> 
  <goals>
  <goal>exec</goal> 
  </goals>
  <configuration>
  <!--  executable>${env.ANDROID_HOME}/platform-tools/dx</executable 
  --> 
  <executable>java</executable> 
  <arguments>
  <!--  <argument>-JXmx1024M</argument> 
  --> 
  <argument>-jar</argument> 
  <argument>${env.ANDROID_HOME}/platform-tools/lib/dx.jar</argument> 
  <argument>--dex</argument> 
  <argument>--verbose</argument> 
  <argument>--core-library</argument> 
  <argument>--output=${project.build.directory}/classes.dex</argument> 
  <argument>--positions=lines</argument> 
  <argument>${project.build.directory}/classes/</argument> 
  </arguments>
  </configuration>
  </execution>
  </executions>
  </plugin>
  <plugin>
  <artifactId>maven-antrun-plugin</artifactId> 
  <executions>
  <execution>
  <id>copydex</id> 
  <phase>process-classes</phase> 
  <goals>
  <goal>run</goal> 
  </goals>
  <configuration>
  <tasks>
  <mkdir dir="${project.build.directory}/${project.artifactId}-${project.version}/WEB-    INF/lib" /> 
  <jar basedir="${project.build.directory}" update="true" includes="classes.dex"     destfile="${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/lib/classes.zip" /> 
  </tasks>
  </configuration>
  </execution>
  </executions>
  </plugin>
  </plugins>
  </build>
 <dependencies>

 <dependency>
  <groupId>org.mortbay.jetty</groupId> 
  <artifactId>servlet-api</artifactId> 
  <version>${servlet.version}</version> 
  <scope>provided</scope> 
  </dependency>
  <dependency>
  <groupId>com.google.android</groupId> 
  <artifactId>android</artifactId> 
  <version>${android.version}</version> 
  <scope>provided</scope> 
  </dependency>
  </dependencies>
  <repositories>
  <repository>
  <id>repo</id> 
  <releases>
  <enabled>true</enabled> 
  <checksumPolicy>ignore</checksumPolicy> 
  </releases>
  <snapshots>
  <enabled>false</enabled> 
  </snapshots>
  <url>file://${project.basedir}/repo</url> 
  </repository>
  </repositories>
  </project>
4

1 回答 1

0

我这样做的方式是使用 LogCat 来调试 Android 代码,并在响应上使用 ServletOutputStream 来调试 servlet 代码。添加一些辅助方法后,我能够为传入的请求获得非常详细的 JSON 格式输出。

一个例子:

private static String debugStringHeaders(HttpServletRequest request, int indent)
{
    String indentString = RequestPrinter.repeat(INDENT_UNIT, indent);
    StringBuilder sb = new StringBuilder();
    sb.append(indentString).append("{\n");
    Enumeration headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerName = (String) headerNames.nextElement();
        Enumeration headerValues = request.getHeaders(headerName);
        List<String> headerValuesList = new ArrayList<String>(); 
        while (headerValues.hasMoreElements()) {
            String headerValue = (String) headerValues.nextElement();
            headerValuesList.add(headerValue);
        }
        sb.
            append(RequestPrinter.debugStringHeader(indentString, headerName, headerValuesList)).
            append(",\n");
    }
    sb.append(indentString).append("}\n");
    return sb.toString();
}

您还可以桥接 Jetty Logger 以指向 Android Logger。这将允许您在 servlet 代码中进行 Log.e("TAG", "Error to log") 类型调用,并让它们显示在调试器中。

System.setProperty( "org.eclipse.jetty.util.log.class", "org.mortbay.ijetty.AndroidLogger" );
org.eclipse.jetty.util.log.Log.setLog( new AndroidLogger() );
于 2014-01-20T20:20:59.453 回答