使用 Post API 尝试 Retrofit2,响应显示成功但 returns 垃圾值

Trying Retrofit2 with Post API, Response shows sucess but returns junk value

enter image description here

我已经在清单中启用了 Internet 权限并包含了 retrofit2 库...任何建议都会有很大帮助.. 使用 jsonschemapojo[= 创建 jsonresponse class 18=]

这是实用程序class

    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);
        }
    }

这是界面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回应class 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;
        }    
    }

这是 MainActivityclass

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());

这将打印 class 名称。主体被转换为 JsonResponse 并且 toString() 仅打印 class 名称。要访问您的字段,请尝试以下操作。

JsonResponse jsonResponse = response.body()
Log.i(TAG, "Token from JsonResponse: " + jsonResponse.token);

这会打印令牌,但您可以对 JSONResponse 的其他元素执行相同的操作