Android:启动线程,加入它并且中断不能正常工作

时间:2016-01-20 19:10:18

标签: java android multithreading join interrupt

我遇到了问题,我无法在互联网上找到解决方案。我看到很多例子,但没有人真正回答我的问题。

我有一个登录页面,然后在检查两个字段(登录/通过)是否已填写后,尝试通过另一个线程连接。

public class LoginActivity extends AppCompatActivity {

private EditText login = null;
private EditText password = null;
private RadioButton radioButton = null;
private Button button = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    login = (EditText)findViewById(R.id.loginEditText);
    password = (EditText)findViewById(R.id.passwordEditText);
    radioButton = (RadioButton)findViewById(R.id.saveRadioButton);
    button = (Button)findViewById(R.id.ConnectionButton);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (login.getText().toString().equals("")
                    || password.getText().toString().equals("")) {
                alert(getString(R.string.alertDialoguErrorTitle), getString(R.string.UnfilledFieldLogin));
            } else {
                boolean haveToSave = radioButton.isChecked();

                User user = User.getUser(login.getText().toString(), password.getText().toString());
                try {
                    Intranet.login.start();
                    Intranet.login.join();
                    Intranet.login.interrupt();
                    Intranet.login.join();
                } catch (InterruptedException e) {
                    alert(getString(R.string.alertDialoguErrorTitle), e.toString());
                    login.setText("");
                    password.setText("");
                } finally {
                    if (!user._token.equals("")) {
                        if (haveToSave) {
                            // SAVE DATA
                        }
                        finish();
                    } else {
                        login.setText("");
                        password.setText("");
                        alert(getString(R.string.alertDialoguErrorTitle), getString(R.string.badLoginPassword));
                    }
                }
            }
        }
    });
}

public void alert(String title, String message) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(LoginActivity.this);
    alertDialog.setTitle(title);
    alertDialog.setMessage(message);
    alertDialog.setPositiveButton("Close",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // nothing
                }
            });
    alertDialog.show();
}

}

//在另一个线程上使用的类

public class Intranet {

public static int responseCode = 0;
public static String responseString = "";

public static Thread login = new Thread(new Runnable() {
    private OkHttpClient client = new OkHttpClient();
    private String url = "https://epitech-api.herokuapp.com/login";
    private User user = User.getUser();

    public void run() {
        try {
            // Build the request
            RequestBody formBody = new FormEncodingBuilder()
                    .add("login", user._login)
                    .add("password", user._password)
                    .build();
            Request request = new Request.Builder()
                    .url(url)
                    .post(formBody)
                    .build();
            Response responses = null;

            // Reset the response code
            responseCode = 0;

            // Make the request
            responses = client.newCall(request).execute();

            if ((responseCode = responses.code()) == 200) {
                // Get response
                String jsonData = responses.body().string();

                // Transform reponse to JSon Object
                JSONObject json = new JSONObject(jsonData);

                // Use the JSon Object
                user._token = json.getString("token");
            }

        } catch (IOException e) {
            responseString = e.toString();
        } catch (JSONException e) {
            responseString = e.toString();
        }
        ;
    }
});
}

我尝试了很多使用join()的解决方案,等待线程结束。但最后,在第二次的时候,当我尝试连接自己时,会出现异常(线程已经启动)。那么如果这个线程在继续之前被中断,它怎么能继续运行呢?

0 个答案:

没有答案