我尝试使用 Glide
从服务器下载多张图片这是代码
for (String url : list) {
RequestOptions requestOptions = RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.ALL);
Glide.with(this)
.asBitmap()
.load(url).addListener(new RequestListener<Bitmap>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
Log.e("ProgressCheck", "onResourceReady: " + progress);
return false;
}
})
.apply(requestOptions)
.submit();
}
代码运行正常,但是当下载映像失败(由于无线网络断开或服务器不响应等原因)时,如何再次发送相同的请求?
还是有更好的方法使用 Glide
下载多张图片答案 0 :(得分:0)
我建议您采取另一种通过滑行加载图像的方法。
这是伪代码
JSONObject params = new JSONObject();
params.put("tag", "login");
params.put("username", username);
params.put("password", password);
final String requestBody = params.toString();
StringRequest request = new StringRequest(Request.Method.POST, ApiService.auth,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
hideDialog();
try {
JSONObject jsonObject = new JSONObject(response);
boolean error = jsonObject.getBoolean("error");
// Check for error node in json
if (!error) {
// user successfully logged in
// Create login_view session
session.setLogin(true);
// Now store the user in SQLite
String uid = jsonObject.getString("uid");
JSONObject user = jsonObject.getJSONObject("user");
String username1 = user.getString("username");
String email = user.getString("email");
String city = user.getString("city");
String subdistrict = user.getString("subdistrict");
String name = user.getString("name");
String nickname = user.getString("nickname");
String address = user.getString("address");
String phone = user.getString("phone");
String birth_date = user.getString("birth_date");
String gender = user.getString("gender");
String created_at = user.getString("created_at");
String updated_at = user.getString("updated_at");
String weight = user.getString("weight");
String height = user.getString("height");
String prohibition = user.getString("prohibition");
Toast.makeText(Login.this, "Successfully logged in, congrats!", Toast.LENGTH_SHORT).show();
// Inserting row in users table
db.addUser(uid, username1, email, city, subdistrict, name, nickname, address, phone, birth_date, gender, created_at, updated_at);
db.updateMedicalInfo(weight, height, prohibition, updated_at);
// Launch main activity
Intent intent = new Intent(Login.this, Home.class);
startActivity(intent);
finish();
} else {
// Error in login_view. Get the error message
String errorMsg = jsonObject.getString("error_msg");
Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting parameters to login_view url
Map<String, String> params = new HashMap<>();
params.put("tag", "login");
params.put("username", username);
params.put("password", password);
return params;
}
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
};
答案 1 :(得分:0)
如果error或fallback策略对您没有用,那么在4.3.0版本中,您可以在失败时启动新请求:
Glide.with(fragment)
.load(url)
.error(
Glide.with(fragment)
.load(url))
.into(imageView);
在https://bumptech.github.io/glide/doc/options.html#starting-a-new-request-on-failure
了解更多信息