Android异步任务竞争条件

Android异步任务竞争条件,android,android-asynctask,Android,Android Asynctask,我写了一些看起来很奇怪的代码。希望其他人看一看,看看他们是否能发现任何问题。我认为这可能会导致一些竞争条件,但我还没有创建我的网络api,所以我还没有对它进行全面测试 在network类中进行回调让我感到不安 网络类-该类将处理我的android应用程序的所有post操作 import android.graphics.Bitmap; import android.os.AsyncTask; public class NetworkSend extends AsyncTask<Void,

我写了一些看起来很奇怪的代码。希望其他人看一看,看看他们是否能发现任何问题。我认为这可能会导致一些竞争条件,但我还没有创建我的网络api,所以我还没有对它进行全面测试

在network类中进行回调让我感到不安

网络类-该类将处理我的android应用程序的所有post操作

import android.graphics.Bitmap;
import android.os.AsyncTask;

public class NetworkSend extends AsyncTask<Void, Void, String> {
    private static NetworkSend singleton = new NetworkSend();
    final String BASE_URL = "http://azure/api";
    final String LOGOUT_URL = "/logout";
    final String PICTURE_NOTE_URL = "/picture_note";
    final String POST_LOCATION_URL = "/post_location";
    final String LOGIN_URL = "/login";

    String currentCall = null;
    String authKey = null;
    double lat = -1.0;
    double log = -1.0;
    String note = null;
    Bitmap picture = null;
    int warning = -1;
    boolean variablesSet = false;
    String status;
    String user;
    String password;
    NetworkCallBack callBack;
    String returnValue;

    private NetworkSend(){
        this.callBack = new NetworkCallBack() {
            @Override
            public void done(String status) {
                returnValue = status;
            }
        };
    }

    public static NetworkSend getInstance( ) {
        return singleton;
    }

//    public NetworkSend(NetworkCallBack callback){
//        this.callback = callback;
//    }

    public boolean setVariables(String key, double latitude, double logitude, String note, Bitmap picture){
        this.authKey = key;
        this.lat = latitude;
        this.log = logitude;
        this.note = note;
        this.picture = picture;
        this.currentCall = PICTURE_NOTE_URL;
        this.variablesSet = true;
        return true;
    }

    public boolean setVariables(String username, String password){
        this.currentCall = LOGIN_URL;
        this.user = username;
        this.password = password;
        return true;
    }

    public boolean setVariables(String key){
        this.authKey = key;
        this.currentCall = LOGOUT_URL;
        this.variablesSet = true;
        return true;
    }

    public boolean setVariables(String key, double latitude, double logitude, int warning){
        this.authKey = key;
        this.lat = latitude;
        this.log = logitude;
        this.warning = warning;
        this.currentCall = POST_LOCATION_URL;
        this.variablesSet = true;
        return true;
    }

    public boolean resetVariables(){
        this.currentCall = null;
        this.authKey = null;
        this.lat = -1.0;
        this.log = -1.0;
        this.note = null;
        this.picture = null;
        this.warning = -1;
        this.variablesSet = false;
        this.user = null;
        this.password = null;
        return true;
    }

    @Override
    protected String doInBackground(Void... params) {
        //resetVariables();
        status = this.authKey;
        return status;
    }

    @Override
    protected void onPostExecute(String status) {
        super.onPostExecute(status);
        callBack.done(status);
    }
}
测试类-当前只有一个测试

public class NetworkSendTests {
    NetworkSend network;

    @Test
    public void setVariables1 () {
        network = NetworkSend.getInstance();
        network.setVariables("apple");
        assertEquals(network.doInBackground(), "apple");
    }
}
我知道我可以在任何需要打网络电话的地方做类似的事情。我只是想减少代码重复。不过,我相信我误用了/完全取消了回调的提示

network = new NetworkSend(new NetworkCallBack() {
    @Override
    public void done(String status) {
        Toast.makeText(Warning.this, "Network Status: " + status, Toast.LENGTH_LONG).show();
    }
}); 
network = new NetworkSend(new NetworkCallBack() {
    @Override
    public void done(String status) {
        Toast.makeText(Warning.this, "Network Status: " + status, Toast.LENGTH_LONG).show();
    }
});