if-else语句出现意外行为

时间:2017-05-08 17:50:25

标签: java android android-studio okhttp3

我点击按钮

后会触发以下if (!rf.registerUser(url, username.getText().toString(), phoneNumber.getText().toString(), materialDesignSpinner.getText().toString(), password.getText().toString(), registrationjson)) { // below registeringError is to show an alert when error occurs rf.registeringError(v.getContext(), warning, getResources().getString(R.string.registeringError)); System.out.println("Failure!"); } else if (rf.registerUser(url, username.getText().toString(), phoneNumber.getText().toString(), materialDesignSpinner.getText().toString(), password.getText().toString(), registrationjson)) { // below registrationSuccess method is to show an alert after successful registration rf.registrationSuccess(v.getContext(), getResources().getString(R.string.congrats), getResources().getString(R.string.successfull_registration)); System.out.println("Success!"); } 语句
 public boolean registerUser(String uri, String username, String phone, String state, String password, registrationJson registrationjson) {
        OkHttpClient client;
        RequestBody body;

        final MediaType JSON
                = MediaType.parse("application/json; charset=utf-8");

        registrationjson.setUsername(username);
        registrationjson.setPhone(phone);
        registrationjson.setState(state);
        registrationjson.setPassword(password);

        Gson gson = new Gson();
        final String jsonFromform = gson.toJson(registrationjson);

        client = new OkHttpClient();
        body = RequestBody.create(JSON, jsonFromform);
        Request request = new Request.Builder()
                .url(uri)
                .post(body)
                .addHeader("content-type", "application/json; charset=utf-8")
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("Failure!!");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                switch (response.code()){
                    case 400:
                        changeStatusFalse(); // change status boolean to false
                    case 200:
                        changeStatusTrue(); //change status boolean to true
                }
            }

        });
        return status; // return status
    }

下面是registerUser方法代码

registeringError

我面临的问题是,当我填写注册表并按下按钮(希望一切正常)我得到registrationSuccess警报,然后我再次按下按钮而不关闭屏幕,我得到了if-else提醒

我尝试在registerUser中使用switch statement代替return

WIERD THING:我尝试将return statusreturn true更改为registeringError。首先执行successRegistration警报,然后再次按下按钮onResponse已执行。

注意:当我按下按钮时,错误警报会在服务器记录成功消息之前显示一个纳秒(以描述速度有多快),但我不确定这是否真的是问题,因为检查响应的状态在// C program for generic linked list #include<stdio.h> #include<stdlib.h> /* A linked list node */ struct node { // Any data type can be stored in this node void *data; struct node *next; }; /* Function to add a node at the beginning of Linked List. This function expects a pointer to the data to be added and size of the data type */ void push(struct node** head_ref, void *new_data, size_t data_size) { // Allocate memory for node struct node* new_node = (struct node*)malloc(sizeof(struct node)); new_node->data = malloc(data_size); new_node->next = (*head_ref); // Copy contents of new_data to newly allocated memory. // Assumption: char takes 1 byte. int i; //Why loop is used for copying data from new_data to new_node data. for (i=0; i<data_size; i++) *(char *)(new_node->data + i) = *(char *)(new_data + i); // Change head pointer as new node is added at the beginning (*head_ref) = new_node; } /* Function to print nodes in a given linked list. fpitr is used to access the function to be used for printing current node data. Note that different data types need different specifier in printf() */ void printList(struct node *node, void (*fptr)(void *)) { while (node != NULL) { (*fptr)(node->data); node = node->next; } } // Function to print an integer void printInt(void *n) { printf(" %d", *(int *)n); } // Function to print a float void printFloat(void *f) { printf(" %f", *(float *)f); } /* Driver program to test above function */ int main() { struct node *start = NULL; // Create and print an int linked list unsigned int_size = sizeof(int); int arr[] = {10, 20, 30, 40, 50}, i; for (i=4; i>=0; i--) push(&start, &arr[i], int_size); printf("Created integer linked list is \n"); printList(start, printInt); // Create and print a float linked list unsigned float_size = sizeof(float); start = NULL; float arr2[] = {10.1, 20.2, 30.3, 40.4, 50.5}; for (i=4; i>=0; i--) push(&start, &arr2[i], float_size); printf("\n\nCreated float linked list is \n"); printList(start, printFloat); return 0; } 方法

0 个答案:

没有答案