leksyde
leksyde

Reputation: 65

Gluon Android POST request

hi please i have a login button that sends a post request to an api before switching views with javafx gluon.

this works well on desktop but does not work on the android packaged apk

package com.nphcda.views;

import com.gluonhq.attach.settings.SettingsService;
import com.gluonhq.attach.util.Services;
import com.gluonhq.charm.glisten.animation.BounceInRightTransition;
import com.gluonhq.charm.glisten.application.AppManager;
import com.gluonhq.charm.glisten.application.MobileApplication;
import com.gluonhq.charm.glisten.control.Alert;
import com.gluonhq.charm.glisten.control.AppBar;
import com.gluonhq.charm.glisten.mvc.View;
import com.gluonhq.charm.glisten.visual.MaterialDesignIcon;
import com.google.gson.Gson;
import com.nphcda.User;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class SecondaryPresenter implements Serializable {

    @FXML
    private View secondary;

    @FXML
    private TextField usernametf;

    @FXML
    private TextField passwordtf;

    @FXML
    private Button login;

    @FXML
    private Button login2;

    String token;
    String BACKENDURL = "nphcdainternal.herokuapp.com";
    boolean isValid = false;

    public void initialize() {

        secondary.setShowTransitionFactory(BounceInRightTransition::new);
        secondary.showingProperty().addListener((obs, oldValue, newValue) -> {
            if (newValue) {

            }
        });
        AppManager.getInstance().switchView("Primary View");


    }

    @FXML
    void sendData() {

        Platform.runLater(() -> {
            try {
                boolean isValid = authenticate(usernametf.getText(), passwordtf.getText());

                if (isValid) {

                    Services.get(SettingsService.class).ifPresent(settings -> {
                        settings.store("username", usernametf.getText());
                        settings.store("token", token);

                    });

                    AppManager.getInstance().switchView("Primary View");

                } else {
                    Alert notifyme = new Alert(AlertType.ERROR, "INCORRECT USERNAME/PASSWORD");
                    notifyme.showAndWait();
                }
            } catch (Exception ex) {
                System.out.println("cant load");
                Logger.getLogger(SecondaryPresenter.class.getName()).log(Level.SEVERE, null, ex);

                ex.printStackTrace();
            }
        });

    }

    @FXML
    void sendData2() {
        AppManager.getInstance().switchView("Primary View");

    }

    public boolean authenticate(String username, String password) {

        // The token request URL
        final String tokenUrl = "https://" + BACKENDURL + "/authenticate";


        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(tokenUrl);
        post.setHeader("Content-type", "application/json");

        HttpResponse response = null;

        System.out.println(new Gson().toJson(new AuthDTO(username, password)));
        try {
            post.setEntity(new StringEntity(new Gson().toJson(new AuthDTO(username, password))));
            response = client.execute(post);
            // System.out.println("bodyy" + EntityUtils.toString(response.getEntity()));
            token = EntityUtils.toString(response.getEntity());
            System.out.println("code" + response.getStatusLine().getStatusCode());

            if (response.getStatusLine().getStatusCode() != 200) {
                return false;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            Alert notifyme = new Alert(AlertType.ERROR, "NETWORK ERROR. contact administrator");
            notifyme.showAndWait();
            return false;
        }

        return true;

    }

    public void loadProperties() throws IOException {
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream("app.properties");
        Properties appProps = new Properties();
        appProps.load(inputStream);
        token = appProps.getProperty("token");
        BACKENDURL = appProps.getProperty("backend");

    }

    public boolean getTokenForCode(String code) {

        // The token request URL
        final String tokenUrl = "https://" + BACKENDURL + "/hello";

        // Using HttpClient to make the POST to exchange the auth code for the token
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(tokenUrl);

        post.addHeader("Bearer ", code);

        // Execute the request
        HttpResponse response = null;
        try {
            response = client.execute(post);
        } catch (IOException ex) {
            Logger.getLogger(SecondaryPresenter.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Print the status code
        System.out.println("Response Code : " + response.getStatusLine().getStatusCode());

        if (response.getStatusLine().getStatusCode() != 200) {
            return false;
        }
        return true;
    }

}

class AuthDTO {

    String username;
    String password;

    public AuthDTO(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "{\"username\": " + username + "\" password\":" + password + '}';
    }

}

the switching of views works cant seem to figure out why the button click is not working, also the alert does not pop up. tried both javafx alertand gluon alert.

update it works after running gluon:runagent but currently getting an error on logcat

android not able to call the endpoint

5-31 08:30:35.728 D/GraalCompiled(29525): Exception in thread "JavaFX Application Thread" java.lang.NoClassDefFoundError: Could not initialize class java.net.Inet6Address
05-31 08:30:35.728 D/GraalCompiled(29525):  at java.lang.Class.ensureInitialized(DynamicHub.java:518)
05-31 08:30:35.728 D/GraalCompiled(29525):  at com.oracle.svm.jni.functions.JNIFunctions.FindClass(JNIFunctions.java:351)
05-31 08:30:35.728 D/GraalCompiled(29525):  at java.net.Inet4AddressImpl.lookupAllHostAddr(Inet4AddressImpl.java)
05-31 08:30:35.728 D/GraalCompiled(29525):  at java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:929)
05-31 08:30:35.728 D/GraalCompiled(29525):  at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1519)
05-31 08:30:35.729 D/GraalCompiled(29525):  at java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:848)
05-31 08:30:35.729 D/GraalCompiled(29525):  at java.net.InetAddress.getAllByName0(InetAddress.java:1509)
05-31 08:30:35.729 D/GraalCompiled(29525):  at java.net.InetAddress.getAllByName(InetAddress.java:1368)
05-31 08:30:35.729 D/GraalCompiled(29525):  at java.net.InetAddress.getAllByName(InetAddress.java:1302)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:112)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:376)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
05-31 08:30:35.729 D/GraalCompiled(29525):  at com.nphcda.views.SecondaryPresenter.authenticate(SecondaryPresenter.java:118)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.nphcda.views.SecondaryPresenter.lambda$sendData$2(SecondaryPresenter.java:71)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
05-31 08:30:35.730 D/GraalCompiled(29525):  at java.security.AccessController.executePrivileged(AccessController.java:169)
05-31 08:30:35.730 D/GraalCompiled(29525):  at java.security.AccessController.doPrivileged(AccessController.java:91)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.sun.glass.ui.monocle.RunnableProcessor.runLoop(RunnableProcessor.java:92)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.sun.glass.ui.monocle.RunnableProcessor.run(RunnableProcessor.java:51)
05-31 08:30:35.730 D/GraalCompiled(29525):  at java.lang.Thread.run(Thread.java:829)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:704)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.oracle.svm.core.posix.thread.PosixPlatformThreads.pthreadStartRoutine(PosixPlatformThreads.java:202)
05-31 08:30:35.727 D/GraalCompiled(29525): traceEvent: Pushing TouchState[0] to TouchPipeline[SmallMove]
05-31 08:30:35.727 D/GraalCompiled(29525): traceEvent: Pushing TouchState[0] to TouchPipeline[SmallMove]
05-31 08:30:35.727 D/GraalCompiled(29525): traceEvent: Applying SmallMove to TouchState[0]
05-31 08:30:35.727 D/GraalCompiled(29525): traceEvent: Set TouchState[0]
05-31 08:30:35.727 D/GraalCompiled(29525): traceEvent: Set MouseState[x=77,y=367,wheel=0,buttonsPressed=IntSet[]]
05-31 08:30:35.728 D/GraalCompiled(29525): {"username":"leksyde","password":"1234"}
05-31 08:30:35.728 D/GraalCompiled(29525): Exception in thread "JavaFX Application Thread" java.lang.NoClassDefFoundError: Could not initialize class java.net.Inet6Address
05-31 08:30:35.728 D/GraalCompiled(29525):  at java.lang.Class.ensureInitialized(DynamicHub.java:518)
05-31 08:30:35.728 D/GraalCompiled(29525):  at com.oracle.svm.jni.functions.JNIFunctions.FindClass(JNIFunctions.java:351)
05-31 08:30:35.728 D/GraalCompiled(29525):  at java.net.Inet4AddressImpl.lookupAllHostAddr(Inet4AddressImpl.java)
05-31 08:30:35.728 D/GraalCompiled(29525):  at java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:929)
05-31 08:30:35.728 D/GraalCompiled(29525):  at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1519)
05-31 08:30:35.729 D/GraalCompiled(29525):  at java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:848)
05-31 08:30:35.729 D/GraalCompiled(29525):  at java.net.InetAddress.getAllByName0(InetAddress.java:1509)
05-31 08:30:35.729 D/GraalCompiled(29525):  at java.net.InetAddress.getAllByName(InetAddress.java:1368)
05-31 08:30:35.729 D/GraalCompiled(29525):  at java.net.InetAddress.getAllByName(InetAddress.java:1302)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:112)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:376)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
05-31 08:30:35.729 D/GraalCompiled(29525):  at com.nphcda.views.SecondaryPresenter.authenticate(SecondaryPresenter.java:118)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.nphcda.views.SecondaryPresenter.lambda$sendData$2(SecondaryPresenter.java:71)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
05-31 08:30:35.730 D/GraalCompiled(29525):  at java.security.AccessController.executePrivileged(AccessController.java:169)
05-31 08:30:35.730 D/GraalCompiled(29525):  at java.security.AccessController.doPrivileged(AccessController.java:91)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.sun.glass.ui.monocle.RunnableProcessor.runLoop(RunnableProcessor.java:92)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.sun.glass.ui.monocle.RunnableProcessor.run(RunnableProcessor.java:51)
05-31 08:30:35.730 D/GraalCompiled(29525):  at java.lang.Thread.run(Thread.java:829)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:704)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.oracle.svm.core.posix.thread.PosixPlatformThreads.pthreadStartRoutine(PosixPlatformThreads.java:202)
05-31 08:30:35.727 D/GraalCompiled(29525): traceEvent: Applying SmallMove to TouchState[0]
05-31 08:30:35.727 D/GraalCompiled(29525): traceEvent: Set TouchState[0]
05-31 08:30:35.727 D/GraalCompiled(29525): traceEvent: Set MouseState[x=77,y=367,wheel=0,buttonsPressed=IntSet[]]
05-31 08:30:35.728 D/GraalCompiled(29525): {"username":"leksyde","password":"1234"}
05-31 08:30:35.728 D/GraalCompiled(29525): Exception in thread "JavaFX Application Thread" java.lang.NoClassDefFoundError: Could not initialize class java.net.Inet6Address
05-31 08:30:35.728 D/GraalCompiled(29525):  at java.lang.Class.ensureInitialized(DynamicHub.java:518)
05-31 08:30:35.728 D/GraalCompiled(29525):  at com.oracle.svm.jni.functions.JNIFunctions.FindClass(JNIFunctions.java:351)
05-31 08:30:35.728 D/GraalCompiled(29525):  at java.net.Inet4AddressImpl.lookupAllHostAddr(Inet4AddressImpl.java)
05-31 08:30:35.728 D/GraalCompiled(29525):  at java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:929)
05-31 08:30:35.728 D/GraalCompiled(29525):  at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1519)
05-31 08:30:35.729 D/GraalCompiled(29525):  at java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:848)
05-31 08:30:35.729 D/GraalCompiled(29525):  at java.net.InetAddress.getAllByName0(InetAddress.java:1509)
05-31 08:30:35.729 D/GraalCompiled(29525):  at java.net.InetAddress.getAllByName(InetAddress.java:1368)
05-31 08:30:35.729 D/GraalCompiled(29525):  at java.net.InetAddress.getAllByName(InetAddress.java:1302)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:112)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:376)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
05-31 08:30:35.729 D/GraalCompiled(29525):  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
05-31 08:30:35.729 D/GraalCompiled(29525):  at com.nphcda.views.SecondaryPresenter.authenticate(SecondaryPresenter.java:118)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.nphcda.views.SecondaryPresenter.lambda$sendData$2(SecondaryPresenter.java:71)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
05-31 08:30:35.730 D/GraalCompiled(29525):  at java.security.AccessController.executePrivileged(AccessController.java:169)
05-31 08:30:35.730 D/GraalCompiled(29525):  at java.security.AccessController.doPrivileged(AccessController.java:91)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.sun.glass.ui.monocle.RunnableProcessor.runLoop(RunnableProcessor.java:92)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.sun.glass.ui.monocle.RunnableProcessor.run(RunnableProcessor.java:51)
05-31 08:30:35.730 D/GraalCompiled(29525):  at java.lang.Thread.run(Thread.java:829)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:704)
05-31 08:30:35.730 D/GraalCompiled(29525):  at com.oracle.svm.core.posix.thread.PosixPlatformThreads.pthreadStartRoutine(PosixPlatformThreads.java:202)

06-01 08:55:53.507 D/GraalCompiled(29103): Caused by: java.lang.NoClassDefFoundError: Could not initialize class java.net.SocksSocketImpl
06-01 08:55:53.507 D/GraalCompiled(29103):  at java.net.Socket.setImpl(Socket.java:523)
06-01 08:55:53.507 D/GraalCompiled(29103): Caused by: java.lang.NoClassDefFoundError: Could not initialize class java.net.SocksSocketImpl
06-01 08:55:53.507 D/GraalCompiled(29103):  at java.net.Socket.setImpl(Socket.java:523)
06-01 08:55:53.507 D/GraalCompiled(29103):  ... 56 more
06-01 08:55:53.507 D/GraalCompiled(29103):  ... 56 more
06-01 08:55:53.507 D/GraalCompiled(29103): Caused by: java.lang.NoClassDefFoundError: Could not initialize class java.net.SocksSocketImpl
06-01 08:55:53.507 D/GraalCompiled(29103):  at java.net.Socket.setImpl(Socket.java:523)
06-01 08:55:53.507 D/GraalCompiled(29103): Caused by: java.lang.NoClassDefFoundError: Could not initialize class java.net.SocksSocketImpl

Upvotes: 0

Views: 105

Answers (0)

Related Questions