当应用程序首次在设备中运行时,我从数据库中获取数据并将其保存到SQLite,以便用户可以在脱机时使用它。
我的代码是这样的。
public class AsyncDatas extends AsyncTask<Void,Void,Void> {
private Context context;
private List<ModelAccounts> accountDatas;
private List<ModelPoi> poiDatas;
private List<ModelComments> commentDatas;
private List<ModelImage> imageDatas;
private List<ModelFavorite> favoriteDatas;
final String MY_PREFS_NAME = "USER";
private List<ModelAccounts> accountDatas2;
private int count;
private Bitmap[] thumbnails;
private Activity activity;
public AsyncDatas(Context context,Activity activity){
this.context = context;
this.activity = activity;
}
@Override
protected Void doInBackground(Void... params) {
RequestQueue requestUsers = Volley.newRequestQueue(context);
JsonObjectRequest jsonObjectUsers = new JsonObjectRequest(Request.Method.POST,
GET_USER_URL, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
ModelAccounts modelAccounts = new ModelAccounts();
try {
JSONArray jsonArray = response.getJSONArray("poi");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
int getID = jsonObject.getInt("id");
accountDatas = new Select().from(ModelAccounts.class).where(Condition.column(ModelAccounts$Table.USER_ID).is(getID)).queryList();
if (accountDatas.size() == 0) {
modelAccounts = new ModelAccounts();
modelAccounts.setFname(jsonObject.getString("firstname"));
modelAccounts.setLname(jsonObject.getString("lastname"));
modelAccounts.setUname(jsonObject.getString("username"));
modelAccounts.setPword(jsonObject.getString("password"));
modelAccounts.setImg(jsonObject.getString("img"));
modelAccounts.setUser_id(String.valueOf(getID));
modelAccounts.save();
}
Log.e("DONE","DONE USEr");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestUsers.add(jsonObjectUsers);
RequestQueue requestFavorites = Volley.newRequestQueue(context);
JsonObjectRequest jsonObjectFav = new JsonObjectRequest(Request.Method.POST,
GET_FAV_URL, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
ModelFavorite modelFavorite = new ModelFavorite();
try {
JSONArray jsonArray = response.getJSONArray("poi");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
int getID = jsonObject.getInt("id");
favoriteDatas = new Select().from(ModelFavorite.class).where(Condition.column(ModelFavorite$Table.FAVORITE_ID).is(getID)).queryList();
if (favoriteDatas.size() == 0) {
modelFavorite = new ModelFavorite();
modelFavorite.setLatitude(jsonObject.getString("latitude"));
modelFavorite.setUser_id(jsonObject.getString("user_id"));
modelFavorite.setType(jsonObject.getString("type"));
modelFavorite.setFavorite_id(String.valueOf(getID));
modelFavorite.save();
}
}
Log.e("DONE","DONE FAV");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestFavorites.add(jsonObjectFav);
RequestQueue requestPOI = Volley.newRequestQueue(context);
JsonObjectRequest jsonObjectPOI = new JsonObjectRequest(Request.Method.POST,
INSERT_POI_URL, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
ModelPoi modelPoi = new ModelPoi();
try {
JSONArray jsonArray = response.getJSONArray("poi");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
int getID = jsonObject.getInt("id");
poiDatas = new Select().from(ModelPoi.class).where(Condition.column(ModelPoi$Table.POI_ID).is(getID)).queryList();
if(poiDatas.size()==0){
modelPoi = new ModelPoi();
modelPoi.setPOI(jsonObject.getString("POI"));
modelPoi.setPOIAddress(jsonObject.getString("POIAddress"));
modelPoi.setPOIType(jsonObject.getString("POIType"));
modelPoi.setPOILat(jsonObject.getString("POILat"));
modelPoi.setPOILong(jsonObject.getString("POILong"));
modelPoi.setPOIInfo(jsonObject.getString("POIInfo"));
modelPoi.setPoi_id(String.valueOf(getID));
modelPoi.save();
}
Log.e("DONE","DONE POI");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestPOI.add(jsonObjectPOI);
RequestQueue requestComments = Volley.newRequestQueue(context);
JsonObjectRequest jsonObjectComments = new JsonObjectRequest(Request.Method.POST,
COMMENTS_URL, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
ModelComments modelComments;
try {
JSONArray jsonArray = response.getJSONArray("poi");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
int getID = jsonObject.getInt("id");
commentDatas = new Select().from(ModelComments.class).where(Condition.column(ModelComments$Table.COMMENT_ID).is(getID)).queryList();
if (commentDatas.size() == 0) {
modelComments = new ModelComments();
modelComments.setLatitude(jsonObject.getString("latitude"));
modelComments.setComment_id(jsonObject.getString("id"));
modelComments.setComment(jsonObject.getString("comment"));
modelComments.setDate(jsonObject.getString("date"));
modelComments.setTitle(jsonObject.getString("title"));
modelComments.setUser_id(jsonObject.getString("user_id"));
modelComments.setRating(jsonObject.getString("rating"));
modelComments.setComment_id(String.valueOf(getID));
modelComments.save();
}
Log.e("DONE","DONE COMMENTS");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestComments.add(jsonObjectComments);
RequestQueue requestImage = Volley.newRequestQueue(context);
JsonObjectRequest jsonObjectImage = new JsonObjectRequest(Request.Method.POST,
GET_IMAGE_URL, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
ModelImage modelImage = new ModelImage();
try {
JSONArray jsonArray = response.getJSONArray("poi");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
int getId = jsonObject.getInt("id");
imageDatas = new Select().from(ModelImage.class).where(Condition.column(ModelImage$Table.IMAGE_ID).is(getId)).queryList();
if (imageDatas.size()==0) {
modelImage = new ModelImage();
modelImage.setLatitude(jsonObject.getString("latitude"));
modelImage.setImg(jsonObject.getString("imagepath"));
modelImage.setUser_id(jsonObject.getString("user_id"));
modelImage.setDatetime(jsonObject.getString("datetime"));
modelImage.setImage_id(String.valueOf(getId));
modelImage.save();
}
Log.e("DONE","DONE IMG");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestImage.add(jsonObjectImage);
final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID};
final String orderBy = MediaStore.Images.Media._ID;
Cursor imagecursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.thumbnails = new Bitmap[this.count];
Variable.IMAGES = new ArrayList<>();
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
context.getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
Variable.IMAGES.add("file://" + imagecursor.getString(dataColumnIndex));
Log.e("IMAGE","DOME IMAGE");
}
}
有时它确实有效,有时它没有。
无论如何都要在上面的代码中平滑过程吗?
有时它起作用,所有数据都取
有时它并不意味着,只有少数几个被取出
像只有第一个RequestQueue工作..
感谢。
答案 0 :(得分:-2)
你在一个AsyncTask中做了很多工作。同样为了代码的可读性,我建议将此代码分成更小的块,也许每个API(Web服务)调用一个任务。那么你将拥有你需要的所有数据。确定优先级,然后相应地调整任务。