Import a Java project into an Android application?

Is it possible to use the Java Project in an Android project, even if some classes from the java project are not recognized in a regular Android project? For example, javax.xml package?

There are two possibilities that I see:

  • Create a jar with this java project and import it into android
  • Or a link to a project in android (as I understand it, this is possible).

But in any case, will those classes that are in java, but not in android, be fine in my Android application? Will this work? Thanks. Printscreen from build path of my android app

Printscreen from build path of my android app

I created a JavaProject and then imported it into my Android project as a reference. I get an error that I did not expect, it does not recognize my .classes from my Java project.

+6
source share
2 answers

If you use Eclipse (with the ADT plugin), you can reference the Java project in the build path of your Android project. This will pack these classes into your .apk output and allow you to use them in your application.

As you noted in your comments; a link to a Java project as an Android library will not work in your case, since there are javax packages. * will lead to compilation errors in Eclipse. In this case, you will need to select option 1 and create a Jar file from your Java project and enable it as described here . Thus, class files requiring javax. *, Should not lead to compilation / runtime errors if you do not try to use them . In any case, using the Java build path will not work as you expect - the class files are not related in this way.

The Android platform provides part of javax.xml , but not the entire package (read this document for more details on this). Often the easiest solution is to write the Android equivalent of your affected Java code that does not require missing dependencies, and the bridge 2 implementation is therefore the correct one for each project.

+1
source

In the end, my biggest problem was that the URL I was passing to HttpPost and ksoap2 with SAP didn't 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 created a SOAP envelope, I will try to stop doing this in the near future. Created an instance of CredentialsProvider and set my user / password information. The status from server 200, and I get the information that I need. One problem remains that the answer is apparently too large (and it won't stop here), so my answer is truncated. Working to solve it now. I really hope this helps someone.

0
source

Source: https://habr.com/ru/post/907568/


All Articles