首页 > 解决方案 > 使用 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);
    }
}

提前致谢

标签: androidpostretrofit2

解决方案


 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 的其他元素执行相同操作


推荐阅读