亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 POST 方法在 Retrofit api 中傳遞參數時出現問題

使用 POST 方法在 Retrofit api 中傳遞參數時出現問題

白豬掌柜的 2023-03-02 15:48:38
我想使用 API 從服務器獲取通知。我正在使用 Retrofit2 來處理 API。問題是當我在 POST 方法中傳遞參數時,我收到“IllegalArgumentException URL 不包含該參數”。參數正確并在 iOS 中工作。我想在android中實現。以下是調試應用程序時的結果。      Caused by: java.lang.IllegalArgumentException: URL       "notification/newnotification" does not contain "{userid}". (parameter #1)我試過更改參數并詢問 API 開發人員。他說參數是正確的。這是 API 接口的代碼:public interface API{    @FormUrlEncoded    @POST("notification/newnotification")    Call<ResponseBody> getUserNotification(        @Path("userid") int userid    ); }RetrofitClient.javapublic class RetrofitClient {public static final String BASE_URL = "http://danceglobe.co/dance/api/";private static RetrofitClient mInstance;private Retrofit retrofit;private RetrofitClient(){    retrofit = new Retrofit.Builder()            .baseUrl(BASE_URL)            .addConverterFactory(GsonConverterFactory.create())            .build();}public static synchronized RetrofitClient getInstance(){    if(mInstance == null) {        mInstance = new RetrofitClient();    }    return mInstance;}public API getApi(){    return retrofit.create(API.class);}}從 MainActivity 調用函數    private void getNotification(String currentUserId) {    Call<ResponseBody> call = RetrofitClient.getInstance().getApi().getUserNotification(Integer.parseInt(currentUserId));    call.enqueue(new Callback<ResponseBody>() {        @Override        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {            if(!response.isSuccessful()){                Toast.makeText(MainActivity.this,response.message(),Toast.LENGTH_SHORT).show();            }            try {                String s = response.body().string();                Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();            } catch (IOException e) {                e.printStackTrace();            }        }我希望它響應一些數據。請幫我。從過去的兩天開始,我就陷入了困境。
查看完整描述

2 回答

?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

 Caused by: java.lang.IllegalArgumentException: URL 

  "notification/newnotification" does not contain "{userid}". (parameter #1)

意味著您應該將 userId 添加到路徑中


public interface API {

    @POST("notification/newnotification/{userid}")

    Call<ResponseBody> getUserNotification(

        @Path("userid") int userid

    );

}


查看完整回答
反對 回復 2023-03-02
?
一只斗牛犬

TA貢獻1784條經驗 獲得超2個贊

我通過對 API 接口進行一些更改使其工作。以下是 API 接口的新代碼


@FormUrlEncoded

@POST("notification/newnotification")

Call<ResponseBody> getUserNotification(

        @Field("userid") String userid   // changed line 

);


查看完整回答
反對 回復 2023-03-02
  • 2 回答
  • 0 關注
  • 300 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號