Why not return information?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
15 views
The situation is this, trying to comprehend the spark and retrofit2. There is a base on the server I'm trying to create authorization
Made 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://: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 do restful:
public interface getUser{ @GET("/getUser/{user}") Call 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; } }

Create apercu:
public class AutirizationAPI { private static String BASE_URL = "http://: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, actually, appeal to for API service to get info:
public class AsyncTask extends UserLoginTask { 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() { @Override public void onResponse(@NonNull Call call @NonNull Response 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 call, @NonNull Throwable t) { Toast.makeText(LoginActivity.this, "network Error" + 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 the following, response returns nothing for some reason, however, when I test the server part of the link [myIP]:4567/getUser/{"username":"TestUser","password":"TestPassword","email":"Test@gmail.com","phone":0}
it returns me a structure of {"information":"invalid password"}, well, that is something that needs to send the server. And now I'm confused, why is this, and how is it treated? 2 days on the Internet searched, but found nothing. I would be very grateful for your help.
by | 15 views

1 Answer

0 like 0 dislike
The problem is that I mixed synchronous and asynchronous logic.

@Override protected Boolean doInBackground(Void... params) { // ... getUser.CurrUser(str).enqueue(new Callback() { // Line (*) // All code below will be executed asynchronously @Override public void onResponse(@NonNull Call call @NonNull Response response) { // ... if (mError.equals("successful")) i = 0; else { // No one knows // when exactly this will happen i = 1; } } @Override public void onFailure(@NonNull Call call, @NonNull Throwable t) { // ... } // All the code above will be executed asynchronously }); // This line will execute immediately // after the line (*) return i == 0; }


Solution made the request synchronously, (continued to use AsyncTask), using execute instead of enqueue
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,187 answers
0 comments
40,796 users