Android Retrofit2传递两参数至API验证存在性及POJO类适配求助
嘿,我帮你把用Retrofit2实现Android端参数验证的完整流程梳理清楚啦,一步步来:
1. 先定义Retrofit的API接口
你的API是GET请求,参数挂在URL里,所以先写一个接口来描述这个请求:
public interface UserProfileApi { // 重点注意:参数名要和API里的完全对应——`phone`和`Password`(第二个首字母大写,别写错!) @GET("api/userprofile") Call<UserProfile> getUserProfile( @Query("phone") String phone, @Query("Password") String password ); }
2. 完善你的POJO实体类
根据你给出的JSON响应,我帮你补全了正确的POJO类,用@SerializedName来映射JSON字段,避免大小写或者命名风格的坑:
import com.google.gson.annotations.SerializedName; public class UserProfile { @SerializedName("Username") private String username; @SerializedName("ProfileId") private String profileId; @SerializedName("Password") private String password; @SerializedName("SaccaUssdId") private String saccaUssdId; @SerializedName("PaybillNo") private String paybillNo; @SerializedName("Phonenumber") private String phoneNumber; @SerializedName("CustomerType") private String customerType; @SerializedName("RegistrationDate") private String registrationDate; @SerializedName("TimeStamp") private String timeStamp; @SerializedName("PreferredModeOfComm") private String preferredModeOfComm; @SerializedName("APIURL") private String apiUrl; @SerializedName("APIUsername") private String apiUsername; @SerializedName("APIPassword") private String apiPassword; // 记得给所有字段生成getter和setter方法,或者用Lombok插件简化代码 public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } // 其他字段的getter/setter我就不一一写了,照着上面的格式补全就行 }
3. 初始化Retrofit实例
先确保你已经在Module级别的build.gradle里加了Retrofit和Gson转换器的依赖:
// Retrofit核心库 implementation 'com.squareup.retrofit2:retrofit:2.9.0' // Gson解析器,用来把JSON转成POJO implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
然后在你的代码里初始化Retrofit(建议放在ViewModel里,避免Activity销毁时的内存泄漏):
Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://192.168.0.19:2000/") // 注意baseUrl必须以/结尾! .addConverterFactory(GsonConverterFactory.create()) .build(); // 创建API接口的实例 UserProfileApi userProfileApi = retrofit.create(UserProfileApi.class);
4. 发起请求并验证参数
调用接口方法,传入你要验证的phone和password,然后异步处理响应:
// 替换成你要验证的实际参数 Call<UserProfile> call = userProfileApi.getUserProfile("0722931069", "1111"); call.enqueue(new Callback<UserProfile>() { @Override public void onResponse(Call<UserProfile> call, Response<UserProfile> response) { if (response.isSuccessful() && response.body() != null) { // 这里说明服务器返回了有效用户数据,证明你传的参数是存在的 UserProfile profile = response.body(); // 可以拿到用户信息做后续操作,比如打印用户名 Log.d("UserCheck", "验证成功!用户名:" + profile.getUsername()); } else { // 响应失败,可能是参数不存在,或者服务器返回了错误码 Log.e("UserCheck", "参数验证失败,服务器返回码:" + response.code()); // 这里可以和后端确认:如果参数不存在,服务器会返回什么状态码,比如404,然后针对性处理 } } @Override public void onFailure(Call<UserProfile> call, Throwable t) { // 网络请求失败,比如没网、服务器连不上 Log.e("UserCheck", "网络请求出错:" + t.getMessage()); } });
几个关键注意事项
- 别忘了在
AndroidManifest.xml里加网络权限:<uses-permission android:name="android.permission.INTERNET" /> - Android 9及以上默认禁止明文HTTP请求,你需要配置网络安全配置,或者让后端改用HTTPS
- 网络请求绝对不能在主线程执行,Retrofit的
enqueue是异步执行的,刚好符合要求;如果用execute的话,一定要放在子线程里 - 建议用ViewModel+LiveData来管理请求状态和结果,避免Activity销毁时出现内存泄漏
内容的提问来源于stack exchange,提问作者isaac kiplagat
相关产品推荐
相关产品推荐

