我想使用 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
);
}

一只斗牛犬
TA貢獻1784條經驗 獲得超2個贊
我通過對 API 接口進行一些更改使其工作。以下是 API 接口的新代碼
@FormUrlEncoded
@POST("notification/newnotification")
Call<ResponseBody> getUserNotification(
@Field("userid") String userid // changed line
);
添加回答
舉報
0/150
提交
取消