Hi guys I am stuck on something and I was wondering if any of you would have any ideas. I wrote a networking API for my own server for an app. It runs asynchronously so I was trying to make a separate thread call it and wait while the man thread waits on that thread. I know how to do what I want to without multi threading but I am curious as to if I could make this work.
This code starts everything
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String password = ((EditText) findViewById(R.id.password)).getText().toString();
String email = ((EditText) findViewById(R.id.email)).getText().toString();
CustomerBuilder builder = new CustomerBuilder();
builder.addEmail(email);
builder.addPassword(password);
State state = new State();
state.setState(false);
Object lock = new Object();
ShaneConnectCustomerLoginAdapter adapter = new ShaneConnectCustomerLoginAdapter(getShaneConnect(),lock,state);
adapter.checkCredentials(builder.getCustomer());
synchronized (state){
while(!state.getState()){
try {
state.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Intent myIntent = new Intent(CustomerLoginActivity.this, Employee_MainScreen.class); /** Class name here */
CustomerLoginActivity.this.startActivity(myIntent);
}
}
});
public void checkCredentials(final Customer customer){
new Thread(new Runnable() {
@Override
public void run() {
credentials(customer);
}
}).run();
}
This is what makes the REST API call to get the data I require for a login. When I run this code the main thread sleeps and is never notified?
private void credentials(final Customer customer) {
synchronized (state) {
connection.getCustomer(customer.getEmail(), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (!response.has("none")) {
try {
String correctpass = (String) response.get("pass");
if (customer.getPassword().equals(correctpass)) {
state.setState(true);
state.notify();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
}