Answer the question
In order to leave comments, you need to log in
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);
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;
}
public interface getUser{
@GET("/getUser/{user}")
Call<Info> CurrUser(@Path("user") String str);
}
public class Info {
@SerializedName("information")
@Expose
private String information;
public Info(String information) {
this.information = information;
}
public String getInformation() {
return information;
}
}
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;
}
}
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);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question