6

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. Printscreen from build path of my android app

Printscreen from build path of my android app

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.

11
  • It should work unless they are conflicting with android. Commented Feb 3, 2012 at 13:11
  • wiseandroid.com/post/2010/07/16/…. However it says some stuff android won't like. Commented Feb 3, 2012 at 13:11
  • that's what i am curious about, normally it will conflict with android, because the package javax.xml is not in default package of android and importing core library is not compilable. I was hoping that importing a java project would not be the same result, android to be complaining about those classes. Commented Feb 3, 2012 at 13:13
  • 1
    copy the java source of your java project into your android source folder and then check for the compilation errors. When you find these, post a question for those errors. This will save both our times as neither of the options you spoke of would work on an actual android platform Commented Feb 3, 2012 at 13:18
  • please tag as android and xml too Commented Feb 3, 2012 at 13:29

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.