Java 等待另一个类输出结果
用户按下login按钮后,login类将信息发送给另一个类(backgroundworker)以验证登录信息。backgroundworker将与服务器通信,然后返回结果。 目前,我将线程休眠2秒钟,以确保它有足够的时间与服务器通信。 是否有任何方法可以让登录类检测backgroundworker获得结果?这样我就可以避免硬编码睡眠时间 登录:Java 等待另一个类输出结果,java,android,Java,Android,用户按下login按钮后,login类将信息发送给另一个类(backgroundworker)以验证登录信息。backgroundworker将与服务器通信,然后返回结果。 目前,我将线程休眠2秒钟,以确保它有足够的时间与服务器通信。 是否有任何方法可以让登录类检测backgroundworker获得结果?这样我就可以避免硬编码睡眠时间 登录: public void OnLogin(View view) throws InterruptedException { final Strin
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();
}
public void OnLogin(视图)引发中断异常{
最后一个字符串email=emailEt.getText().toString();
最终字符串type=“login”;
最终后台工作人员后台工作人员=新后台工作人员(此);
新建异步任务(){
@凌驾
受保护的void onPostExecute(void结果){
if(backgroundWorker.getResult().equals(“登录成功”)){
SharedReferences设置=获取SharedReferences(“mceDiscussBoard”,0);
SharedReferences.Editor=setting.edit();
编辑器.putString(“email”,email);
commit();
Intent myIntent=新的Intent(MainActivity.this、RoomActivity.class);
星触觉(myIntent);
}否则{
}
}
@凌驾
受保护的void onPreExecute(){
Toast.makeText(getBaseContext(),“Loading…”,Toast.LENGTH_LONG.show();
backgroundWorker.execute(类型,电子邮件);
}
@凌驾
受保护的Void doInBackground(Void…参数){
//试着睡大约2秒钟
试一试{
睡眠(2000年);
}捕捉(中断异常e){
e、 printStackTrace();
}
返回null;
}
}.execute();
}
背景工作人员:
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();
}
}
}
公共类BackgroundWorker扩展异步任务{
语境;
警报对话框警报对话框;
BackgroundWorker(上下文ctx){
上下文=ctx;
}
int信号=0;
字符串结果=”;
private static final ScheduledExecutorService executor=Executors.newSingleThreadScheduledExecutor();
受保护的字符串doInBackground(字符串…参数){
字符串类型=参数[0];
字符串登录\u url=”http://www.xxx.php";
if(type.equals(“登录”)){
试一试{
字符串email=params[1];
URL=新URL(登录\ URL);
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod(“POST”);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream OutputStream=httpURLConnection.getOutputStream();
BufferedWriter BufferedWriter=新的BufferedWriter(新的OutputStreamWriter(outputStream,UTF-8));
字符串post_data=urlcoder.encode(“电子邮件”,“UTF-8”)+“=”+urlcoder.encode(电子邮件,“UTF-8”);
bufferedWriter.write(post_数据);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream InputStream=httpURLConnection.getInputStream();
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(inputStream,“iso-8859-1”);
字符串行=”;
而((line=bufferedReader.readLine())!=null){
结果+=行;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
返回结果;
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
我觉得你的问题有点放错地方了。通常,async应该是在逻辑失败或成功时决定做什么的人。例如,成功时你可能会前进到另一个活动,或者失败时显示错误消息。AsyncTask
是另一个与主线程并行运行的线程。方法onPreE应使用xecute
和onPostExecute
在doInBackground
之前和之后运行代码。后台线程运行时,您可以向用户显示进度条