使用参数改造 GET 请求

编程入门 行业动态 更新时间:2024-10-10 09:16:56
本文介绍了使用参数改造 GET 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用 Retrofit GET 请求调用 api.此 GET 请求需要一个参数.当我使用 POSTMAN 进行测试时,API 工作得很好,但是当我尝试使用下面的 API 调用时,它返回

I make a call to an api using Retrofit GET request. This GET request requires a parameter. The API works perfectly when i use POSTMAN for testing but when i try to use the API call below it returns

未将对象引用设置为对象的实例.

Object reference not set to an instance of an object.

@GET("/api/account/*******") Call<ResetPassword> requestPasswordResetToken(@Query("phoneNumber") String phoneNumber);

以及我如何在活动中提出该请求的代码.

And the code how i make that request in my activity.

public void requestPasswordResetToken(String phoneNumber) { Retrofit retrofit = RetrofitClient.getClient(""); APIService mAPIService = retrofit.create(APIService.class); final ProgressDialog loading = ProgressDialog.show(this, "Please Wait", "Loading your information...", false, false); loading.setCancelable(true); loading.show(); mAPIService.requestPasswordResetToken(phoneNumber).enqueue(new Callback<ResetPassword>() { @Override public void onResponse(Response<ResetPassword> response, Retrofit retrofit) { if(response.isSuccess()) { String loginSuccess = response.body().getSuccess(); String message = response.body().getMessage(); if (loginSuccess.equals("true")) { loading.dismiss(); showSnackMessage(message); }else { Log.e("loginError", message); Toast.makeText(RequestPasswordResetActivity.this, message, Toast.LENGTH_LONG).show(); loading.dismiss(); } } } @Override public void onFailure(Throwable throwable) { Log.e("ResetPasswordError", throwable.getMessage()); Toast.makeText(RequestPasswordResetActivity.this, "Unable to Login, Please Try Again", Toast.LENGTH_LONG).show(); loading.dismiss(); } }); }

API 期望的屏幕截图.字段名称正确.

the screenshot of the what the API expects. The field names are correct.

推荐答案

你的代码看起来不错.您是否检查了您正在处理的 api 的结果格式.并且 ResetPassword 类属性(变量名称和类型)必须与 api 的响应相同.(注意大写或小写字母).

Your codes looks fine. Do you check the result format of the api you are working on. And also ResetPassword class properties (variable names and types) must be same with response of the api. (be careful about upper case or lower case letters).

也试试这个格式请求

@GET("methodName/{PARAMETER}") Call<Object> getData( @Path("telephoneNumber") String telephoneNumber );

更多推荐

使用参数改造 GET 请求

本文发布于:2023-11-25 10:23:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629380.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!