T
T
Thymomenos Gata2018-05-04 23:00:41
Java
Thymomenos Gata, 2018-05-04 23:00:41

Why is information not being returned?

The situation is this, I'm trying to comprehend spark and retrofit2. There is a base on the server, I'm trying to create an authorization,
I made a rest service:

get("/getUser/:user",(request,response)-> {
            response.type("application/json");
            Account acc = gson.fromJson(request.params(":user"), Account.class);
            return getUser(acc);
        }, gson ::toJson);

getUser() :
private static Info getUser(Account acc) throws ClassNotFoundException, SQLException, UnsupportedEncodingException, NoSuchAlgorithmException {
        Connection c;
        Statement stm = null;
        Class.forName("org.postgresql.Driver");
        c = DriverManager.getConnection("jdbc:postgresql://<myIP>:5432/egecalc", "postgres", "itsMyAppBitches");
        String sql = "select * from users;";
        Info result = null;
        String hash = getHash(acc.getPassword());
        stm = c.createStatement();
        ResultSet rs = stm.executeQuery(sql);
        while (rs.next()) {
            if (rs.getString("username").equals(acc.getUsername())) {
                if (rs.getString("password").equals(acc.getPassword())) {
                    result = new Info("successful");
                    break;
                } else {
                    result = new Info("invalid password");
                    break;
                }
            }
            else
                result = new Info("invalid username");
        }
        rs.close();
        c.close();
        return result;
    }

and on the client side I do restful:
public interface getUser{
        @GET("/getUser/{user}")
        Call<Info> CurrUser(@Path("user") String str);
    }

POJO class:
public class Info {
    @SerializedName("information")
    @Expose
    private String information;

    public Info(String information) {
        this.information = information;
    }

    public String getInformation() {
        return information;
    }
}

I create an apache:
public class AutirizationAPI {
    private static String BASE_URL = "http://<myIP>:4567";
    public static EgeCalcApi.getUser getApi() {
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

        EgeCalcApi.getUser getuser = retrofit.create(EgeCalcApi.getUser.class);
        return getuser;

    }
}

Well, in fact, I turn to the api to the service in order to get information:
public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {

        private final String mLogin;
        private final String mPassword;
        private String mError = "1";
        private int i = 1;
        private EgeCalcApi.getUser getUser;
        private Account acc;
        UserLoginTask(String login, String password) throws UnsupportedEncodingException, NoSuchAlgorithmException {
            mLogin = login;
            mPassword = getHash(password);
            acc = new Account(mLogin, mPassword, "",0);
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            // TODO: attempt authentication against a network service.
                Gson gson = new Gson();
                String str = gson.toJson(acc);
                getUser = AutirizationAPI.getApi();
                getUser.CurrUser(str).enqueue(new Callback<Info>() {
                    @Override
                    public void onResponse(@NonNull Call<Info> call, @NonNull Response<Info> response) {
                        System.out.print(response.body());
                        Info info = response.body();
                        assert info != null;
                        mError = info.getInformation();
                        System.out.print(mError);
                        if (mError.equals("successful"))
                            i = 0;
                        else {
                            i = 1;
                        }
                    }

                    @Override
                    public void onFailure(@NonNull Call<Info> call, @NonNull Throwable t) {
                        Toast.makeText(LoginActivity.this, "Ошибка сети" + t, Toast.LENGTH_SHORT).show();
                    }
                });
                return i == 0;
        }

        @Override
        protected void onPostExecute(final Boolean success) {
            mAuthTask = null;
            showProgress(false);

            if (success) {
                Intent i = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(i);
                finish();
            } else {
                if(mError.equals("invalid password")) {
                    mPasswordView.setError(mError);
                    mPasswordView.requestFocus();
                }
                else if(mError.equals("invalid username")){
                    mEmailView.setError(mError);
                    mEmailView.requestFocus();
                }
            }
        }

        @Override
        protected void onCancelled() {
            mAuthTask = null;
            showProgress(false);
        }
    }

The situation is as follows, response does not return anything for some reason, however, when I test the server part at the link [myIP]:4567/getUser/{"username":"TestUser","password":"TestPassword","email":"Test @gmail.com","phone":0}
it returns me the {"information":"invalid password"} structure, which is what the server should send. And now I'm confused, why is this, and how is it treated? I've been searching the internet for 2 days and haven't found anything yet. I will be very grateful for your help.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question