我怎么能在android studio中打破这个循环

时间:2018-06-13 16:43:27

标签: android loops android-studio break

嗨这是循环我怎么能在我输入的地方打破它我想在这里打破它 在此先感谢您的帮助,我使用android studio开发我的应用程序。

for (int i = 10; i < 100; i++) {
        if (serverconnection.equals("false")) {
            AsyncHttpPost post = new AsyncHttpPost(serveraddress + "/soton/serveraddress.php");
            post.setTimeout(5000);

            final int finalI = i;
            AsyncHttpClient.getDefaultInstance().executeString(post, new AsyncHttpClient.StringCallback() {
                @Override
                public void onCompleted(final Exception e, AsyncHttpResponse source, final String result) {

                    if (e != null) {
                        MainActivity.activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                e.printStackTrace();
                                servernum = finalI+1;
                                Toast.makeText(MainActivity.activity, "not this IP:"+ servernum, Toast.LENGTH_SHORT).show();

                            }
                        });
                    } else if (result.equals("correct address")) {
                        MainActivity.activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.activity,"آدرس جدید پیدا شد",Toast.LENGTH_LONG).show();
                                serverconnection = "true";
                                SharedPreferences.Editor editor = sp.edit();
                                editor.putString("IP", serveraddress+"/");
                                editor.apply();
                                I WHANT TO BREAK IT HERE



                            }
                        });
                    }
                }
            });
        } else {


            SharedPreferences.Editor editori = sp.edit();
            editori.putString("IP", serveraddress+"/");
            editori.apply();
        }
    }

3 个答案:

答案 0 :(得分:0)

只需使用

else if (result.equals("correct address")) {
                        MainActivity.activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.activity,"آدرس جدید پیدا شد",Toast.LENGTH_LONG).show();
                                serverconnection = "true";
                                SharedPreferences.Editor editor = sp.edit();
                                editor.putString("IP", serveraddress+"/");
                                editor.apply();


                         //use break

                         break;

                            }
                        });

打破循环。

  

IF语句中的break命令将退出FOR循环

答案 1 :(得分:0)

在for循环之前使用全局布尔(AtomicBoolean)变量,例如。 shouldStop。最初将其值设置为false。并且在进入循环时检查该值是否为真。如果真的使用了。或者只是在for语句中使用它作为条件。现在设置shouldStop = true代替我在这里打破

//initialize AtomicBoolean shouldStop = new AtomicBoolean(Boolean.FALSE); //update value shouldStop.set(Boolean.TRUE);

答案 2 :(得分:0)

试试这段代码:

/**
 * Try to connect to the server, with an ip address from 'minIndex' to 'maxIndex';
 * @param context Current Context
 * @param minIndex Index of the ip address to start with. (e.g. 10)
 * @param maxIndex max index
 */
public void testIpConnection(Context context,final int minIndex, final int maxIndex) {
    testConnection(minIndex, new Callback() {
        @Override
        public void onSuccess(String serverAddress) {
            Toast.makeText(context, "آدرس جدید پیدا شد", Toast.LENGTH_LONG).show();
            storeServerAddress(serverAddress);
        }

        @Override
        public void onFailure(int index, @Nullable Throwable e) {
            if (e != null)
                e.printStackTrace();
            Toast.makeText(context, "not this IP:" + index, Toast.LENGTH_SHORT).show();
            index++;
            if (index < maxIndex)
                // Try again with the same callback and the new index
                testConnection(index, this);
            else
                Toast.makeText(context, "Failure: Max IP reached" , Toast.LENGTH_SHORT).show();   
        }
    });
}

/**
 * Connect to the server
 *
 * @param index    index to generate the ip
 * @param callback To be call when the request returns.
 */
public void testConnection(final int index, final Callback callback) {
    final String serverAddress = ...; // TODO: Use the index to generate the serverAddress
    AsyncHttpPost post = new AsyncHttpPost(serverAddress + "/soton/serveraddress.php");
    post.setTimeout(5000);

    AsyncHttpClient.getDefaultInstance().executeString(post, new AsyncHttpClient.StringCallback() {
        @Override
        public void onCompleted(final Exception e, AsyncHttpResponse source, final String result) {
            if (result != null && result.equals("correct address")) {
                callback.onSuccess(serverAddress);
            } else {
                callback.onFailure(index, e);
            }
        }
    });
}

/**
 * Store the server address in the SharedPreferences
 *
 * @param serverAddress Address to store
 */
public void storeServerAddress(String serverAddress) {
    SharedPreferences sp = ... // TODO: Define sp here
    SharedPreferences.Editor editori = sp.edit();
    editori.putString("IP", serverAddress + "/");
    editori.apply();
}

/**
 * To be call when on a result of a server call
 */
private interface Callback {
    void onSuccess(String serverAddress);

    void onFailure(int index, Throwable e);
}