HTTP GET通过发送参数RETROFIT android

ndasle7k  于 2023-03-27  发布在  Android
关注(0)|答案(1)|浏览(140)

Postman refernce image当我通过在postman的body中传递以下参数来命中HTTP GET请求时,我可以获得当前响应{“pickup_postcode”:“600114”,“delivery_postcode”:“600125”,“cod”:“1”,“权重”:“1”}
但是当我在改进的android中实现它时,我得到了null对象。我使用@Query传递上述参数。我已经附上了下面的代码。提前感谢

public interface ShipRocketServiceAbilityInterface {
    @GET("courier/serviceability/")
    Call<ShipRocketServiceabilityResponse> getService(
 @Query("pickup_postcode") String pickupPostcode,
 @Query("delivery_postcode") String deliveryPostcode,
 @Query("cod") String cod,
 @Query("weight") String weight);
}
//API Call

        String pickupPostcode = "600114";
        String deliveryPostcode = "600125";
        boolean cod = "1";
        float weight = "1";
        Call<ShipRocketServiceabilityResponse> service =
        shipRocket.getService(pickupPostcode,deliveryPostcode,cod,weight);
        service.enqueue(new Callback<ShipRocketServiceabilityResponse>() {
            @Override
            public void onResponse(Call<ShipRocketServiceabilityResponse> call, Response<ShipRocketServiceabilityResponse> response) {
                String  dataList = response.body().getCurrency();
              

            }

            @Override
            public void onFailure(Call<ShipRocketServiceabilityResponse> call, Throwable t) {

                Log.d("TAG","FAIL");

            }
        });
tzdcorbm

tzdcorbm1#

我看到你提供的代码中有很多问题。事实上,它甚至不应该在编译时通过。
正如Shivam指出的ShipRocketServiceabilityResponse,我们首先需要了解模型是什么样子的。

public interface ShipRocketServiceAbilityInterface {
    @GET("courier/serviceability/")
    Call<ShipRocketServiceabilityResponse> getService(
    @Query("pickup_postcode") String pickupPostcode,
    @Query("delivery_postcode") String deliveryPostcode,
    @Query("cod") String cod,
    @Query("weight") String weight);
}

// API Call
String pickupPostcode = "600114";
String deliveryPostcode = "600125";
String cod = "1";
String weight = "1";

理想情况下,这是我们可以立即解析值的方式。不妨运行调试器来查看来自GET请求的值。
希望这个有用。

相关问题