用Android studio写一个登录页面,输入用户名和密码后点击登录,用OKhttp请求服务器并返回服务器响应的json数据,请用代码实现
时间: 2024-06-09 22:05:09 浏览: 22
好的,下面是一个简单的示例代码:
1. 首先,在你的 build.gradle 文件中添加 OkHttp 的依赖:
```
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
```
2. 在布局文件中,创建一个包含用户名和密码输入框、登录按钮的视图:
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户名" />
<EditText
android:id="@+id/usernameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="密码" />
<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
</LinearLayout>
```
3. 在 Activity 中,获取输入框和登录按钮的引用,并设置按钮的点击事件:
```java
public class LoginActivity extends AppCompatActivity {
private EditText usernameEditText, passwordEditText;
private Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usernameEditText = findViewById(R.id.usernameEditText);
passwordEditText = findViewById(R.id.passwordEditText);
loginButton = findViewById(R.id.loginButton);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
login();
}
});
}
private void login() {
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
// TODO: 发送网络请求
}
}
```
4. 在 login() 方法中,使用 OkHttp 发送网络请求,并解析服务器返回的 JSON 数据:
```java
private void login() {
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new FormBody.Builder()
.add("username", username)
.add("password", password)
.build();
Request request = new Request.Builder()
.url("http://example.com/login")
.post(requestBody)
.build();
try {
Response response = client.newCall(request).execute();
String responseData = response.body().string();
// TODO: 解析服务器返回的 JSON 数据
} catch (IOException e) {
e.printStackTrace();
}
}
```
这里的 URL 和参数只是示例,请根据实际情况替换成你自己的服务器地址和参数。至于如何解析 JSON 数据,可以使用 Android 自带的 JSONObject 或者第三方库如 Gson 等。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)