简体   繁体   中英

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? For example javax.xml package?

There are 2 possibilities as I see it:

  1. Either create a jar with that java project and import it in android
  2. Or reference the project in android(as i understood it's possible).

But in either way, will those classes that are found in java but not in android be ok in my Android Application? Will this work? Thank you. 从我的Android应用程序的构建路径打印屏幕

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

I made the JavaProject, and then imported it in my Android Project as a reference. I get an error i did not expect, it does not recognize my .classes from my Java Project.

If you are using Eclipse (with the ADT plugin) you can reference the Java project in the build path of your Android project. This will package those classes in your .apk output and allow you to use them in your app.

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. 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 . This way, the class files requiring javax.* should not produce any compile/runtime errors unless you try to use them . Either way, using the Java build path will not work as you expect - the class files are not bundled this way.

The Android platform provides some of javax.xml , but not the whole package (read this document for more detail on this). 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.

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.

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. Made an instance of CredentialsProvider and set my user/pass details. The status from server is 200 and i receive information that i need. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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