android - 使用 Post API 尝试 Retrofit2,响应显示成功但返回垃圾值
问题描述
我在清单中启用了互联网权限,并包含了 retrofit2 库...任何建议都会有很大帮助.. 使用 jsonschemapojo 创建 jsonresponse 类
这是实用类
public class ApiUtils {
private ApiUtils() {}
public static final String BASE_URL = "http://some.com/";
public static APIService getAPIService() {
return RetrofitClient.getClient(BASE_URL).create(APIService.class);
}
}
这是接口类
public interface APIService {
@POST("/api/passenger-login")
@FormUrlEncoded
Call<JsonResponse> savePost(@Field("mobile_no") String mobile_no,
@Field("password") String password,
@Field("device_token") String device_token);
}
这是 json 响应类 public class JsonResponse {
@SerializedName("user")
@Expose
private User user;
@SerializedName("token")
@Expose
private String token;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
这是 MainActivity 类
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
final EditText edtmobilenumber = (EditText) findViewById(R.id.et_title);
final EditText edtpassword = (EditText) findViewById(R.id.et_body);
Button submitBtn = (Button) findViewById(R.id.btn_submit);
mResponseTv = (TextView) findViewById(R.id.tv_response);
mAPIService = ApiUtils.getAPIService();
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String title = edtmobilenumber.getText().toString().trim();
String body = edtpassword.getText().toString().trim();
if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(body)) {
sendPost(title, body);
}
}
});
}
public void sendPost(String edtmobilenumber, String edtpassword) {
mAPIService.savePost(edtmobilenumber, edtpassword, "abcdef").enqueue(new Callback<JsonResponse>() {
@Override
public void onResponse(Call<JsonResponse> call, Response<JsonResponse> response) {
if(response.isSuccessful()) {
showResponse(response.body().toString());
Log.i(TAG, "post submitted to API." + response.body().toString());
}
}
@Override
public void onFailure(Call<JsonResponse> call, Throwable t) {
Log.e(TAG, "Unable to submit post to API."+t.getCause());
}
});
}
public void showResponse(String response) {
if(mResponseTv.getVisibility() == View.GONE) {
mResponseTv.setVisibility(View.VISIBLE);
}
mResponseTv.setText(response);
}
}
提前致谢
解决方案
Log.i(TAG, "post submitted to API." + response.body().toString());
这将打印类名。主体被转换为 JsonResponse 并且 toString() 只打印类名。为了访问您的字段,请尝试以下操作。
JsonResponse jsonResponse = response.body()
Log.i(TAG, "Token from JsonResponse: " + jsonResponse.token);
这将打印令牌,但您可以对 JSONResponse 的其他元素执行相同操作
推荐阅读
- css - 在 CSS 代码中创建异常
- ruby-on-rails - Wicked_pdf 多个 pdf 的同一页
- bash - 这个涉及“查找”和“此处字符串”的命令是如何执行的?
- java - Java Files.Copy 删除第一个文件的内容
- r - 如何在面板空间模型中包含解释变量的空间滞后?
- amazon-web-services - AWS-DeepLens:如何在 S3 中访问预训练模型以进行对象识别
- vba - 逐行连接所有列
- python - 如何添加/添加两个或多个 Python 字典?
- python - UI 不接受文件丢弃
- javascript - 如何在消息框弹出窗口上对按钮进行样式设置