How to receive nearby print information with Cups4j

55 Views Asked by At

I am using Cups4j by importing it into an android (JAVA) project. I am writing the code by looking at the link belo

https://github.com/harwey/cups4j#importing

private class PrintTask extends AsyncTask<Void, Void, PrintRequestResult> {
        @Override
        protected PrintRequestResult doInBackground(Void... params) {
            try {
                CupsClient cupsClient = new CupsClient("myIP", 631);
                List<CupsPrinter> printers = cupsClient.getPrinters();
                for (CupsPrinter printer : printers) {
                    System.out.println("Printer Name: " + printer.getName());
                }
            } catch (IOException e) {
                Log.e("PrintError", "Error while reading file or communicating with the printer : ", e);
                e.printStackTrace();
            } catch (Exception e) {
                Log.e("PrintError", "Error : ", e);
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(PrintRequestResult result) {
            if (result != null && result.isSuccessfulResult()) {
                Log.d("PrintSuccess", "Print job submitted successfully!");
            } else {
                Log.e("PrintError", "Print job failed: " + (result != null ? result.getResultMessage() : "Unknown error"));
            }
        }
    }

In the code above, we create a Client object with host and ip through CupsClient() and then try to receive printer information through getPrinters().

In the code above, we create a Client object with host and ip through CupsClient() and then try to receive printer information through getPrinters().

The log appears as below.

W/System.err: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
W/System.err: SLF4J: Defaulting to no-operation (NOP) logger implementation
W/System.err: SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process: com.example.wifidir, PID: 31309
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$4.done(AsyncTask.java:415)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
        at java.util.concurrent.FutureTask.run(FutureTask.java:271)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:923)
     Caused by: java.lang.NoSuchFieldError: No static field INSTANCE of type Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; in class Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; or its superclasses (declaration of 'org.apache.http.conn.ssl.AllowAllHostnameVerifier' appears in /system/framework/framework.jar!classes5.dex)
        at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:151)
        at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:977)
        at org.cups4j.operations.IppHttp.<clinit>(IppHttp.java:33)
        at org.cups4j.operations.IppHttp.createHttpClient(IppHttp.java:39)
        at org.cups4j.operations.IppOperation.sendRequest(IppOperation.java:157)
        at org.cups4j.operations.IppOperation.sendRequest(IppOperation.java:130)
        at org.cups4j.operations.IppOperation.request(IppOperation.java:64)
        at org.cups4j.operations.cups.CupsGetPrintersOperation.getPrinters(CupsGetPrintersOperation.java:59)
        at org.cups4j.CupsClient.getPrinters(CupsClient.java:138)
        at com.example.wifidir.MainActivity$PrintTask.doInBackground(MainActivity.java:55)
        at com.example.wifidir.MainActivity$PrintTask.doInBackground(MainActivity.java:50)
        at android.os.AsyncTask$3.call(AsyncTask.java:394)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:923) 

The problem cannot be determined. Is there a problem with my code?

0

There are 0 best solutions below