简体   繁体   English

Java 返回值返回 null 当分配给值

[英]Java return value returning null when assigned to value

I have a public static function that I am calling in another class and assigning a value to it, but when I Toast that value it is always null. I have a public static function that I am calling in another class and assigning a value to it, but when I Toast that value it is always null. I have tested the values that I am assigning the return value and they aren't null, so the variable I am returning isn't null, but is returning null.我已经测试了我分配返回值的值,它们不是 null,所以我返回的变量不是 null,而是返回 null。 Here is the class:这是 class:

public static String dataLogin(String mode, String table, String columns, String where, String whereId, Context context) {
        final String[] newString = {""};
        String returnString = "";
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("URL")
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        Proxy proxy = retrofit.create(Proxy.class);
        if (mode == "get") {
            //This is a proxy to the weservice
            call.enqueue(new Callback<List<Post>>() {
                @Override
                public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
                    if (!response.isSuccessful()) {
                        return;
                    }

                    List<Post> posts = response.body();
                    for (Post post : posts) {
                        newString[0] += post.getUsername();
                        newString[0] += ",";
                        newString[0] += post.getPassword();
                    }
                }
                @Override
                public void onFailure(Call<List<Post>> call, Throwable t) {
                }
            });
        }
        returnString = newString[0];
        return returnString;
    }

Here is where this class is used:这是使用此 class 的地方:

    public void login(String username, Context context) {
        // can be launched in a separate asynchronous job
        String thisString = Texts.dataLogin(/*Queries*/);
        if (thisString == null || thisString == "") {
            thisString = "null";
        }
        Toast.makeText(context, thisString, Toast.LENGTH_LONG).show();
        //byte[] data = Base64.decode(thisString[1], Base64.DEFAULT);
        //String text = new String(data, "UTF-8");
        Result<LoggedInUser> result = loginRepository.login(thisString, password);
        if (result instanceof Result.Success) {
            LoggedInUser data2 = ((Result.Success<LoggedInUser>) result).getData();
            loginResult.setValue(new LoginResult(new LoggedInUserView(data2.getDisplayName())));
        } else {
            loginResult.setValue(new LoginResult(R.string.login_failed));
        }
    }

Does anyone know why this class isn't returning a proper value?有谁知道为什么这个 class 没有返回正确的值?

Compare Strings' equiality via通过比较字符串的相等性

someString.equals(someOtherString);

Also, note that null is not the same as "null"另外,请注意null"null"不同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM