这是代码。 我想把params EMAIL!但我得到属性值必须是costant
public interface Api{
Context context=null;
SharedPreferences sharedPref = context.getSharedPreferences("login",Context.MODE_PRIVATE);
static String email = sharedPref.getString("email","");
@GET("Home.php?email="+email+"&")
Call<List<Variabili_Main>> getHome(@Query("page") int index);
}
帮助我!
好的,我有编辑但是现在怎么编辑这个? 在这我做什么?
private void load(int index){
Call<List<Variabili_Main>> call = api.getHome(index);
call.enqueue(new Callback<List<Variabili_Main>>() {
@Override
public void onResponse(Call<List<Variabili_Main>> call, Response<List<Variabili_Main>> response) {
if(response.isSuccessful()){
movies.addAll(response.body());
adapter.notifyDataChanged();
}else{
Log.e(TAG," Response Error "+String.valueOf(response.code()));
}
}
@Override
public void onFailure(Call<List<Variabili_Main>> call, Throwable t) {
Log.e(TAG," Response Error "+t.getMessage());
}
});
}
答案 0 :(得分:2)
重新阅读@ cricket_007之后的问题是正确的,因为电子邮件是作为查询发送的
我会更新它以供将来参考以获得两个答案
我认为这应该是我在
之前与标题类似的问题尝试此解决方案
这是因为电子邮件本身在路径中而不是查询
@GET("Home.php/{email}")
Call<List<Variabili_Main>> getHome(@Path("email") String email,@Query("page") int index);
这是将其作为查询发送
@GET("Home.php")
Call<List<Variabili_Main>> getHome(@Query("email") String email,@Query("page") int index);
致电您需要在通话中添加电子邮件
SharedPreferences sharedPref = context.getSharedPreferences("login",Context.MODE_PRIVATE);
static String email = sharedPref.getString("email","");
api.getHome(email,index)
检查更多详情Dynamic Paths in Retrofit和https://square.github.io/retrofit/
答案 1 :(得分:0)
如果要在注释中连接字符串,则它必须是编译时常量。从SharedPreferences运行时收到的东西不是常量。
可能最好将其添加为查询参数。
@GET("Home.php")
Call<List<Variabili_Main>> getHome(@Query("email") String email, @Query("page") int index);
然后,只需在实际需要时传递电子邮件。
private void load(int index){
// TODO: getContext ... shared preferences...
String email = prefs.getString("email");
Call<List<Variabili_Main>> call = api.getHome(email, index);
call.enqueue(new Callback<List<Variabili_Main>>() {
@Override
public void onResponse(Call<List<Variabili_Main>> call, Response<List<Variabili_Main>> response) {
if(response.isSuccessful()){
movies.addAll(response.body());
adapter.notifyDataChanged();
}else{
Log.e(TAG," Response Error "+String.valueOf(response.code()));
}
}
@Override
public void onFailure(Call<List<Variabili_Main>> call, Throwable t) {
Log.e(TAG," Response Error "+t.getMessage());
}
});
}
答案 2 :(得分:0)
如您的评论中所述,您的目标是处理对运行时确定电子邮件查询参数的特定路由的HTTP GET请求(从共享首选项加载)。
要实现此目标,您应该使用改造库提供的@Query表示法。
来自https://square.github.io/retrofit/“API声明”
的示例@GET("group/{id}/users")
Call<List<User>> groupList(@Path("id") int groupId, @Query("sort") String sort);
在您的情况下,您可以将电子邮件值作为参数传递给处理函数并从那里继续。 E.g。
@GET("Home.php")
Call<List<Variabili_Main>> getHome(@Query("page") int index, @Query("email") String email);
为什么无法在注释中提供电子邮件:
正如我的评论所述,如JLS 9.7.1所述,无法提供注释的运行时字符串:
其中V是注释内的值。