简体   繁体   English

在Android应用程序中导入Java项目?

[英]Import Java Project in Android application?

Can I use a Java Project in an Android Project even if some classes from the java project are not recognized in a normal android project? 即使在普通的android项目中无法识别java项目中的某些类,我是否可以在Android项目中使用Java项目? For example javax.xml package? 例如javax.xml包?

There are 2 possibilities as I see it: 我认为有两种可能性:

  1. Either create a jar with that java project and import it in android 用java项目创建一个jar并在android中导入它
  2. Or reference the project in android(as i understood it's possible). 或者在android中引用该项目(因为我知道它是可能的)。

But in either way, will those classes that are found in java but not in android be ok in my Android Application? 但无论哪种方式,在我的Android应用程序中,那些在java中但在android中找不到的类是否可以正常? Will this work? 这会有用吗? Thank you. 谢谢。 从我的Android应用程序的构建路径打印屏幕

从我的Android应用程序的构建路径打印屏幕

I made the JavaProject, and then imported it in my Android Project as a reference. 我创建了JavaProject,然后将其作为参考导入到我的Android项目中。 I get an error i did not expect, it does not recognize my .classes from my Java Project. 我得到了一个我没想到的错误,它无法识别我的Java项目中的.classes。

If you are using Eclipse (with the ADT plugin) you can reference the Java project in the build path of your Android project. 如果您正在使用Eclipse(使用ADT插件),则可以在Android项目的构建路径中引用Java项目。 This will package those classes in your .apk output and allow you to use them in your app. 这会将这些类打包在.apk输出中,并允许您在应用中使用它们。

As you pointed out in your comments; 正如你在评论中指出的那样; referencing a Java project as an Android library will not work in your case, since the presence of the javax.* packages will result in compile time errors in Eclipse. 将Java项目作为Android库引用将不适用于您的情况,因为javax。*包的存在将导致Eclipse中的编译时错误。 In this case, you will need to opt for option 1 and build a Jar file from your Java project and include it as explained here . 在这种情况下,您需要选择选项1并从Java项目构建一个Jar文件,并按照此处的说明包含它。 This way, the class files requiring javax.* should not produce any compile/runtime errors unless you try to use them . 这样, 除非您尝试使用它们,否则需要javax。*的类文件不应产生任何编译/运行时错误。 Either way, using the Java build path will not work as you expect - the class files are not bundled this way. 无论哪种方式,使用Java构建路径将无法正常工作 - 类文件不会以这种方式捆绑。

The Android platform provides some of javax.xml , but not the whole package (read this document for more detail on this). Android平台提供了一些javax.xml ,但不是整个包(请阅读本文档以获取更多详细信息)。 Often the easiest solution is to write an Android equivalent of your affected Java code that does not require the missing dependencies, and bridge the 2 implementations so the correct one is used for each project. 通常最简单的解决方案是编写一个Android等效的受影响的Java代码,不需要缺少依赖项,并桥接 2个实现,以便为每个项目使用正确的实现。

It finally worked, my biggest issue was the url i was passing to HttpPost and ksoap2 with SAP did not work for me at all. 它终于奏效了,我最大的问题是我传递给HttpPost的网址和SAP的ksoap2根本不适用于我。

private void testCallingService() {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.getCredentialsProvider().setCredentials(
            new AuthScope("ip here", port here),
            new UsernamePasswordCredentials(username, password));

    try {
        String buffer = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:urn='namespace'><soapenv:Header/><soapenv:Body><urn:methodname><USERNAME>test</USERNAME></urn:methodname></soapenv:Body></soapenv:Envelope>";
        HttpPost httppost = new HttpPost(url);

        StringEntity se = new StringEntity(buffer, HTTP.UTF_8);
        se.setContentType("text/xml");
        httppost.setHeader("Content-Type",
                "application/soap+xml;charset=UTF-8");
        httppost.setEntity(se);

        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope("ip", port),
                new UsernamePasswordCredentials(username, password));
        httpclient.setCredentialsProvider(credsProvider);

        BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient
                .execute(httppost);
        if (httpResponse.getStatusLine() != null) {
            System.out.println("Http status: "
                    + httpResponse.getStatusLine());
        }

        RequestLine requestLine = httppost.getRequestLine();

        String response = getResponseBody(httpResponse); // read server response. response.getEntity().getContent();...
        System.out.println("response: " + response);
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
    httpclient.getConnectionManager().shutdown();
}

So, I constructed myself the SOAP envelope, will try to stop doing that in the nearest future. 所以,我自己构建了SOAP信封,将在最近的将来尝试停止这样做。 Made an instance of CredentialsProvider and set my user/pass details. 制作CredentialsProvider的实例并设置我的用户/通行证详细信息。 The status from server is 200 and i receive information that i need. 服务器的状态是200,我收到了我需要的信息。 One problem remains that the response is apparently too large(and it's not going to stop here) so my response is truncated. 一个问题仍然是响应显然太大(并且它不会在此停止)所以我的响应被截断了。 Working to solve that now. 现在努力解决这个问题。 I really hope this helps someone. 我真的希望这有助于某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM