首页 > 解决方案 > 如何在 Android 中实现 LoginActivity?

问题描述

我正在尝试LoginActivity在 Android Studio 中实现预制,但无济于事。我被困在文件LoginDataSource.java中。下面是整个课程。

public class LoginDataSource {
    
    public Result<LoggedInUser> login(final String username, final String password) {

        try {
            String url = "http://192.168.1.10:1234/my/api";
            
            // Instantiate the RequestQueue.
            RequestQueue requestQueue;

            // Instantiate the cache
            Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap

            // Set up the network to use HttpURLConnection as the HTTP client.
            Network network = new BasicNetwork(new HurlStack());

            // Instantiate the RequestQueue with the cache and network.
            requestQueue = new RequestQueue(cache, network);

            requestQueue.start();

            // Request a string response from the provided URL.
            StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            Log.i("Response:", response);
                            // Display the first 500 characters of the response string.
                            // textView.setText("Response is: " + response.substring(0, 500));
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // textView.setText("That didn't work!");
                    Log.e("Error:", error.toString());
                }
            }) {
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("user", username);
                    params.put("pass", password);

                    return params;
                }

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("Content-Type", "application/x-www-form-urlencoded");
                    return params;
                }
            };

            requestQueue.add(stringRequest);

            LoggedInUser fakeUser =
                    new LoggedInUser(
                            java.util.UUID.randomUUID().toString(),
                            "John Doe");
            return new Result.Success<>(fakeUser);
        } catch (Exception e) {
            return new Result.Error(new IOException("Error logging in", e));
        }
    }

    public void logout() {
        // TODO: revoke authentication
    }
}

getCacheDir()以红色突出显示,原因说明Cannot resolve method 'getCacheDir' in 'LoginDataSource'。我尝试将参数更改为,context.getCacheDir()但文件中没有context可用的参数,我无法从任何地方导入。

我在这里做错了什么?这让我发疯。

标签: javaandroid

解决方案


getCacheDir() 必须在 Context 对象上调用,因此您需要将 Context 传入才能登录。因此将登录名更改为登录名(最终字符串用户名,最终字符串密码,最终上下文上下文),然后调用 context.getCacheDir()


推荐阅读