添加查询参数以改进Android Studio中的get
我正在尝试使用改型从我的API获取记录,当我这样做时,它工作得很好 公共接口材料API{添加查询参数以改进Android Studio中的get,android,retrofit,Android,Retrofit,我正在尝试使用改型从我的API获取记录,当我这样做时,它工作得很好 公共接口材料API{ @GET("/mlearningServices/Course") public void getMaterials(Callback<List<materialClass>> response); } public void getMaterials() { RestAdapter adapter = new RestAdapter.Builder().setEn
@GET("/mlearningServices/Course")
public void getMaterials(Callback<List<materialClass>> response); } public void getMaterials()
{
RestAdapter adapter = new RestAdapter.Builder().setEndpoint(Root_Url).build();
Log.i(TAG , "hERE IS THE LINK"+adapter.toString());
materialAPI api = adapter.create(materialAPI.class);
api.getMaterials(new Callback <List<materialClass>>() {
@Override
public void success(List<materialClass> list, Response response) {
materials = list;
showList();
customAdapter customAdapter = new customAdapter();
listView.setAdapter(customAdapter);
}
@Override
public void failure(RetrofitError error) {
}
});
}
如何将myId添加到回调方法。这是我第一次使用您询问的改型称为
路径变量。若要设置一个变量,您必须重写方法签名,如下所示:
public void getMaterials(@Path("myId") String id, Callback<List<materialClass>> response);
public void getMaterials(@Path(“myId”)字符串id,回调响应);
这样,定义为/path/to/your/endpoint/{nameOfPathVariable}的变量将被注入传递给该方法的字符串参数中。您也可以将其定义为整数,改型将尝试相应地强制转换它。您所询问的称为路径变量。
。若要设置一个,您必须将方法签名重写为:
public void getMaterials(@Path("myId") String id, Callback<List<materialClass>> response);
public void getMaterials(@Path(“myId”)字符串id,回调响应);
这样,定义为/path/to/your/endpoint/{nameOfPathVariable}的变量将被注入传递给该方法的字符串参数中。您也可以将其定义为整数,改型将尝试相应地强制转换它。使用@path
注释
@POST("/mlearningServices/Course/{myId}")
public void getMaterials(@Path("myId") String id, Callback<Response> response);
@GET("/mlearningServices/Course/{myId}")
Call<materialClass> getMaterials(@Path("myId") String id);
@POST(/mlearningServices/Course/{myId})
public void getMaterials(@Path(“myId”)字符串id,回调响应);
参考资料:
使用@Path
注释
@POST("/mlearningServices/Course/{myId}")
public void getMaterials(@Path("myId") String id, Callback<Response> response);
@GET("/mlearningServices/Course/{myId}")
Call<materialClass> getMaterials(@Path("myId") String id);
@POST(/mlearningServices/Course/{myId})
public void getMaterials(@Path(“myId”)字符串id,回调响应);
参考资料:
解决方案:
您可以使用它来传递您的id,使用@Path注释
@POST("/mlearningServices/Course/{myId}")
public void getMaterials(@Path("myId") String id, Callback<Response> response);
@GET("/mlearningServices/Course/{myId}")
Call<materialClass> getMaterials(@Path("myId") String id);
@GET(“/mlearningServices/Course/{myId}”)
调用getMaterials(@Path(“myId”)字符串id);
@Path是您希望在问号(“?”)之前提供给GET方法的一些数据,@Query(“…”)是您希望在“?”之后提供的一些数据
希望您理解。解决方案:
您可以使用它来传递您的id,使用@Path注释
@POST("/mlearningServices/Course/{myId}")
public void getMaterials(@Path("myId") String id, Callback<Response> response);
@GET("/mlearningServices/Course/{myId}")
Call<materialClass> getMaterials(@Path("myId") String id);
@GET(“/mlearningServices/Course/{myId}”)
调用getMaterials(@Path(“myId”)字符串id);
@Path是您希望在问号(“?”)之前提供给GET方法的一些数据,@Query(“…”)是您希望在“?”之后提供的一些数据
希望你已经明白了