等待另一个类输出结果

时间:2017-07-05 03:06:39

标签: java android

用户按下登录按钮后,登录类会将信息发送到另一个班级(后台工作人员)以验证登录信息。 backgroundworker将与服务器通信,然后返回结果。 目前我睡眠线程2秒,以确保它有足够的时间来沟通服务器。 是否有任何方法可以使用登录类来检测backgroundworker获得结果?这样我就可以防止硬编码睡眠时间。

登录:

public void OnLogin(View view) throws InterruptedException {
    final String email = emailEt.getText().toString();
    final String type = "login";
    final BackgroundWorker backgroundWorker = new BackgroundWorker(this);
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected void onPostExecute(Void result) {
            if(backgroundWorker.getResult().equals("<meta charset=\"utf-8\">login success")){
                SharedPreferences setting = getSharedPreferences("mceDiscussBoard", 0);
                SharedPreferences.Editor editor = setting.edit();
                editor.putString("email",email);
                editor.commit();
                Intent myIntent = new Intent(MainActivity.this, RoomActivity.class);
                startActivity(myIntent);
            }else{
            }
        }
        @Override
        protected void onPreExecute() {
            Toast.makeText(getBaseContext(), " Loading......", Toast.LENGTH_LONG).show();
            backgroundWorker.execute(type, email);
        }
        @Override
        protected Void doInBackground(Void... params) {
            // Try to sleep for roughly 2 seconds
            try {
                sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }
    }.execute();
}

BackgroundWorker的:

public class BackgroundWorker extends AsyncTask<String,Void,String>  {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx) {
    context = ctx;
}
int singal=0;
String result="";
private static final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

protected String doInBackground(String... params) {
    String type = params[0];
    String login_url = "http://www.xxx.php";
    if(type.equals("login")) {
        try {
            String email = params[1];
            URL url = new URL(login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String post_data = URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8");
            bufferedWriter.write(post_data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
            String line="";
            while((line = bufferedReader.readLine())!= null) {
                result += line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

0 个答案:

没有答案