在B类完成异步操作后,在A类中执行方法

时间:2018-02-21 17:04:09

标签: android android-volley

我有3个类:LogIn,LogicController和WebService。

LogIn是一个Activity,通过按下按钮,可以执行静态LogicController方法,该方法又在WebService中执行静态方法。 WebService使用JsonObjectRequest向服务器发出请求。我需要这种交互来代表MVC模型(学校工作需要它),如果没有静态方法我就无法做到,因为我无法“序列化”LogicController作为变量传递给LogIn。

问题是我无法让LogIn在继续之前等待WebService完成其工作,我需要使用它返回的参数。

LogIn课程:

public class LogIn extends AppCompatActivity {
    EditText txtUsr;
    EditText txtPass;
    Button btnLogIn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        txtUsr = (EditText) findViewById(R.id.txtUsr);
        txtPass = (EditText) findViewById(R.id.txtPass);

        btnLogIn = (Button) findViewById(R.id.btnIS);
        btnLogIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                logInWebService();
            }
        });
    }

    private void logInWebService() {
        String usr = txtUsr.getText().toString();
        String pass = txtPass.getText().toString();

        boolean result;
        result = LogicController.logInWebService(this, usr, pass);

        if(result){
            //doSomething in response to the result
            //need to execute this after logInWebService is done
        }
    }
}

LogicController类:

public class LogicController extends AppCompatActivity {
    private WebService myWebService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myWebService = new WebService(1);
    }

    public static boolean logInWebService(Context context, String usr, String pass){
        boolean result = WebService.logInWebService(context, usr, pass);
        return result;
    }
}

WebService类:

    public class WebService {
    private static boolean result;

    public WebService(int idGestor) {
        this.idGestor = idGestor;
    }

    public static boolean logInWebService(Context context, final String usr, final String pass) {
        String url = "https://webpage.myPhpWS.php?"+"idusr="+usr+"&pass="+pass;
        result = false

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                if(someOperation){
                    result = true;
                }
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                System.out.println("Error"+error.getMessage());
            }
        });

        RequestQueue request = Volley.newRequestQueue(context);
        request.add(jsonObjectRequest);

        return result; //this is the final result needed
    }
}
  • 我使用嵌套类,因为WebService类发出了几个请求,我必须能够单独处理它们。如果可能的话,我希望不要在这个方案中添加类。
  • 我找不到涉及这个特定主题的帖子。
  • 任何帮助都会有用,即使我从一开始就采取了糟糕的做法。

感谢您提前。

2 个答案:

答案 0 :(得分:1)

您可以使用回调接口并将其作为参数传递给LogicController.logInWebService(this, usr, pass,callback);

1-创建界面ResultCallback

interface ResultCallback {

void on success();
void onFailure();
   }

2-将其传递给logInWebService

 //show progress dialog before making the request
 LogicController.logInWebService(this, usr, pass,new ResultCallback (){

                @Override
                public void onSuccess() {
                    //hide progress and show success message
                }

                @Override
                public void onFailure() {
                    //hide progress and show error
                }
            }
    );

3-然后修改您的方法logInWebService()

 public static boolean logInWebService(Context context, final String usr, final String pass,ResultCallback callback) {
    String url = "https://webpage.myPhpWS.php?"+"idusr="+usr+"&pass="+pass;
    result = false

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
        callback.onSuccess(); //you can pass sth to this method
            if(someOperation){
                result = true;
            }
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            System.out.println("Error"+error.getMessage());
             callback.onSuccess();
        }
    });

    RequestQueue request = Volley.newRequestQueue(context);
    request.add(jsonObjectRequest);

    return result; //remove this and return void instead.
}

更好的方法是使用Rxjava可观察量。

答案 1 :(得分:0)

好的,我有同样的问题,我用ObservableInteger解决了它,只是声明它

private ObservableInteger mObsInt;

然后在onCreate上设置一个监听器

 //Listener
        mObsInt = new ObservableInteger();
        mObsInt.set(0);

        mObsInt.setOnIntegerChangeListener(new OnIntegerChangeListener()
        {
            @Override
            public void onIntegerChanged(int newValue)
            {
                if (mObsInt.get()==1)
                    Log.e("Downloads"," mObsInt 1");
                Log.e("Download1"," Finished first process ");
                if (mObsInt.get()==2){
                    Log.e("Downloads"," mObsInt 2");
                    Log.e("Download2"," Finished second process ");
                    mProgressDialog.dismiss();
                    Intent mainIntent = new Intent().setClass(LoginActivity.this, Principal.class);
                    startActivity(mainIntent);
                    finish();
                }
            }
        });

然后就这样做(在一个过程结束后)

mObsInt.set(mObsInt.get()+1);

所以它会计数,如果第一个完成的黑暗将是1,当第二个完成时,黑暗将是2,所以在黑暗== 2之后,你可以继续进行你需要的其他活动或过程< / p>

快乐的编码!