在Android中使用retrofit添加到收藏夹?

时间:2018-02-26 05:58:48

标签: android retrofit2 android-toolbar

我有一个带有喜欢的项目的工具栏。我想通过调用具有body参数的api作为user_id和property_id Api for adding property to fav来点击工具栏中的fav图标时添加属性 我已经做了一个实用程序功能,但我无法做到: 我的功能:

public class APIUtilityfunctions {

private static final String TAG = "APIUtilityFunctions";
public static int twoHrsMilliseconds = 120000;
public static int fiveHrsMilliseconds = 300000;
public ApiInterface apiInterface ;

public static void addFavouriteProperty(final Context context, int user_id, int property_id) {


    Favourites favourites = new Favourites();
    favourites.setUserId(user_id);
    favourites.setPropertyId(user_id);
    Session session = new Session(context);

    ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
    Call<List<Property>> call = apiInterface.addToFav(session.getJwtToken(), favourites);
    call.enqueue(new Callback<List<Property>>() {
        @Override
        public void onResponse(Call<List<Property>> call, Response<List<Property>> response) {
            Log.d(TAG, "onResponse: " + response.body());
        }

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

        }
    });
}
public static void setUserToSession(final Context context, int user_id) {
    ApiInterface apiInterface=ApiClient.getClient().create(ApiInterface.class);

    final Session session = new Session(context);

    Call<User> call =apiInterface.getUser(session.getJwtToken(),user_id);
    call.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {
            if (response.code() == 200) {
                User user = response.body();
                    session.setUserName(user.getName());

            }
        }

        @Override
        public void onFailure(Call<User> call, Throwable t) {
        }
    });
}

}

我有一个会话类来获取user_id 我的课程课程:

public class Session {
private SharedPreferences sp;
private SharedPreferences.Editor spe;
Context context;
    int id;

public Session(Context cntx) {
    // TODO Auto-generated constructor stub
    this.context = cntx;
    sp = context.getSharedPreferences("MyData", Context.MODE_PRIVATE);
    spe = sp.edit();
}


public void setUserID(int userID) {
    sp.edit().putInt("UserID", userID).apply();
}

public int getUserID() {
   return sp.getInt("",id);
}

public void setUserName(String userName) {
    sp.edit().putString("UserName", userName).commit();
}

public String getUserName() {
    return sp.getString("UserName", "");
}

public String getJwtToken() {
    String token = sp.getString("token", "");
    if (token == null || token.isEmpty()) {
        token =  "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjIxNzc0NTI3OTksImlhdCI6MTUxNjAyMjk5OSwiaXNzIjoiQmFzb2JhYXMgTmVwYWwiLCJuYmYiOjE1MTYwMjI5OTksImp0aSI6Ikd1ZXN0VG9rZW4iLCJzdWIiOjB9.QikmNgBYmqch5HREGFEpUs4Xk3x-zFfDg5mhYJO7jM8";
    }
    return token;
}

public void setJwtToken(String token) {
    sp.edit().putString("token", token).apply();
}

public Date getExpiresAt() {

    String token =  "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjIxNzc0NTI3OTksImlhdCI6MTUxNjAyMjk5OSwiaXNzIjoiQmFzb2JhYXMgTmVwYWwiLCJuYmYiOjE1MTYwMjI5OTksImp0aSI6Ikd1ZXN0VG9rZW4iLCJzdWIiOjB9.QikmNgBYmqch5HREGFEpUs4Xk3x-zFfDg5mhYJO7jM8";
    JWT jwt = new JWT(token);
    Date expiresAt = jwt.getExpiresAt();
    Toast.makeText(context, expiresAt.toString(), Toast.LENGTH_SHORT).show();
    Log.d("Expires At ", expiresAt.toString());
    return expiresAt;
}

}

当用户点击属性详细信息页面中的fav图标时,我想将该属性添加到fav:

1 个答案:

答案 0 :(得分:0)

尝试使用此

    JsonObject favourites=new JsonObject();
    favourites.addProperty("user_id", user_id);
    favourites.addProperty("property_id", user_id);

而不是

    Favourites favourites = new Favourites();
    favourites.setUserId(user_id);
    favourites.setPropertyId(user_id);

并在addToFav()内的ApiInterface中,将身体参数的参数更改为Call<JsonObject> addToFav(@Body JsonObject body);

注意:将getJwtToken也传递给addToFav()

编辑:首先创建一个像这样的菜单资源menu_item.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/action_fav"
    android:enabled="true"
    android:icon="@drawable/fav_icon"
    android:title="Favourite"
    app:showAsAction="always" />
</menu>

然后像你一样在你的布局中充气

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_item, menu);
return true;
}

然后处理菜单项的点击,

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
  case R.id.action_fav:
    callApi();
    return true;
  default:
    return super.onOptionsItemSelected(item);
  }
  }

函数callApi();应执行api调用以发布您的user_id和property_id