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.

Gabi Radu
  • 1,067
  • 2
  • 16
  • 34
  • It should work unless they are conflicting with android. – kosa Feb 03 '12 at 13:11
  • http://www.wiseandroid.com/post/2010/07/16/Porting-existing-Java-code-to-Android.aspx. However it says some stuff android won't like. – sealz Feb 03 '12 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. – Gabi Radu Feb 03 '12 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 – MozenRath Feb 03 '12 at 13:18
  • please tag as android and xml too – MozenRath Feb 03 '12 at 13:29
  • Can you give more detail on the .class error you mentioned? In particular, paste any error messages or stacktraces you are getting... – seanhodges Feb 03 '12 at 14:25
  • I have edited my question, and this is the first error I run into: Could not find class 'com.test.webservices.RunConnector', referenced from method com.android.login.LoginActivity.getInfo – Gabi Radu Feb 03 '12 at 14:40
  • From your screenshots it looks like you are referencing the other project in the "Java" way. Take a look at this section of the Android documentation: http://developer.android.com/guide/developing/projects/projects-eclipse.html#SettingUpLibraryProject – seanhodges Feb 03 '12 at 14:43
  • yes, I am referencing a pure Java Project because I make use of javax.xml.naming, etc. and that is why I don't want to make an Android Project, because it will fail at compile time. – Gabi Radu Feb 03 '12 at 14:47
  • I was trying to use my code in android without having the problem of packages that are not in it but are in android. But I get this error at the begining: Dx bad class file magic (cafebabe) or version (0033.0000) ...while parsing com/test/webservices/XResponse.class – Gabi Radu Feb 03 '12 at 14:50
  • I see your predicament. I'll update my answer to take this into consideration... – seanhodges Feb 03 '12 at 15:25

2 Answers2

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.

Community
  • 1
  • 1
seanhodges
  • 16,593
  • 14
  • 67
  • 89
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.

Gabriela Radu
  • 717
  • 2
  • 12
  • 32