【发布时间】:2016-08-04 08:30:50
【问题描述】:
我试图在用户登录时创建一个用户对象并将其保存在共享首选项中。用户对象具有我从 json 对象中检索的姓名和姓氏,以及我试图用 piccaso 获取的位图配置文件图片(图片的行也在 json 对象中)。在这两种情况下,我都尝试使用 volley 和 asynctask 进行尝试。
我的用户模型:
public class User{
private String name,lastName;
private Bitmap profilePicture;
Context context;
public User(JSONObject object , Context context){
this.context = context;
try {
this.name = object.getString("firstName");
this.lastName = object.getString("lastName");
this.profilePicture = Picasso.with(context).load(object.getString("image")).get();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
还有我的截击请求:
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Intent intent = new Intent(LoginActivityScreen.this, HomeActivityScreen.class);
SharedPreferences.Editor editor = preferences.edit();
RoomateModel model = new RoomateModel(response, context);
Gson gson = new Gson();
String json = gson.toJson(model);
editor.putString("USER", json);
editor.commit();
startActivity(intent);
}
}
【问题讨论】: