我希望使用 json 从服务器加载图片并使用 //model
public function logo(){
return $this->belongsTo(Media::class, 'image_id');
}
//template
echo $product->logo()->count();
库将其显示到ImageView
,我写下面的代码但没有显示给我Glide
中的任何图片!!
对于此问题,我使用imageView
和Log.d
用于显示图片链接(服务器上的图片链接)并显示图片链接,但在toast
中没有任何图片!!
DataModel:
imageView
AsyncTask:
public class DataModel implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String title;
private String description;
private String image;
private String category;
private String date;
private String price;
public DataModel(int id, String title, String description, String category, String date, String image) {
this.id = id;
this.title = title;
this.description = description;
this.category = category;
this.date = date;
this.image = image;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
适配器:
public class freeDataInfo {
private Context mContext;
private String ServerAddress = freeServer_IP.getFreeIP();
public void getFreeDataInfo(Context context) {
mContext = context;
new getInfo().execute(ServerAddress + "limit=10");
}
private class getInfo extends AsyncTask<String, Void, String> {
EventBus bus = EventBus.getDefault();
private String ou_response;
private List<DataModel> infoModels = new ArrayList<>();
private ProgressDialog dialog;
@Override
protected void onPreExecute() {
//CustomProcessDialog.createAndShow(mContext);
dialog = new ProgressDialog(mContext);
this.dialog.setMessage("شکیبا باشید...");
this.dialog.show();
infoModels.clear();
}
@Override
protected String doInBackground(String... params) {
OkHttpClient client = new OkHttpClient();
//String url = (String) params[0];
Request request = new Request.Builder()
.url(ServerAddress + "limit=10")
.cacheControl(CacheControl.FORCE_NETWORK)
.build();
Response response;
try {
response = client.newCall(request).execute();
ou_response = response.body().string();
response.body().close();
if (ou_response != null) {
try {
JSONObject postObj = new JSONObject(ou_response);
JSONArray postsArray = postObj.optJSONArray("result");
for (int i = 0; i <= postsArray.length(); i++) {
JSONObject postObject = (JSONObject) postsArray.get(i);
int id = postObject.getInt("id");
Log.d("id", String.valueOf(id));
String title = postObject.getString("title");
String description = postObject.getString("description");
String image = postObject.getString("image");
String category = postObject.getString("categoryName");
String date = postObject.getString("publishDate");
Log.d("Data", "Post ID: " + id);
Log.d("Data", "Post title: " + title);
Log.d("Data", "Post image: " + image);
Log.d("Data", "---------------------------------");
//Use the title and id as per your requirement
infoModels.add(new DataModel(id, title, description, category, date, image));
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("error", String.valueOf(e));
}
}
} catch (IOException e) {
e.printStackTrace();
Log.e("error2", String.valueOf(e));
}
return ou_response;
}
@Override
protected void onPostExecute(String result) {
//CustomProcessDialog.dissmis();
//Stop Progress
if (dialog.isShowing()) {
dialog.dismiss();
}
if (result != null) {
bus.post(new MyEvent("forfragment1", infoModels));
} else {
Toast.makeText(mContext, "Empty", Toast.LENGTH_SHORT).show();
}
}
}
记录:
public class free_recycler_adapter extends RecyclerView.Adapter<free_recycler_adapter.ViewHolder> {
private List<DataModel> mDateSet;
private Context context;
// Provide a suitable constructor (depends on the kind of dataset)
public free_recycler_adapter(Context context, List<DataModel> dataSet) {
this.context = context;
this.mDateSet = dataSet;
}
// Create new views (invoked by the layout manager)
@Override
public free_recycler_adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.free_card_layout, parent, false);
// create ViewHolder
return new ViewHolder(view);
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
// - get data from your itemsData at this position
// - replace the contents of the view with that itemsData
viewHolder.free_titleText.setText(Html.fromHtml(mDateSet.get(position).getTitle()));
Glide.with(context)
.load(mDateSet.get(position).getImage())
.placeholder(R.drawable.ic_download_image)
.crossFade()
.into(viewHolder.free_avatarImage);
viewHolder.free_descText.setText(Html.fromHtml(mDateSet.get(position).getDescription()));
Log.d("ImageData", "image : " + mDateSet.get(position).getImage());
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDateSet.size();
}
public void remove(int position) {
mDateSet.remove(position);
notifyItemRemoved(position);
}
public void clear() {
mDateSet.clear();
notifyDataSetChanged();
}
public void add(List<DataModel> models) {
mDateSet.addAll(models);
notifyDataSetChanged();
}
public void update(List<DataModel> models) {
mDateSet.clear();
mDateSet.addAll(models);
notifyDataSetChanged();
}
// inner class to hold a reference to each item of RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView free_titleText, free_descText;
public ImageView free_avatarImage;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
free_titleText = (TextView) itemLayoutView.findViewById(R.id.pdf_card_title);
free_descText = (TextView) itemLayoutView.findViewById(R.id.pdf_card_content);
free_avatarImage = (ImageView) itemLayoutView.findViewById(R.id.pdf_card_image);
itemLayoutView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), ContentPage.class);
v.getContext().startActivity(intent);
}
});
}
}
}
如何解决此问题并在06-11 05:37:20.808 1692-1692/com.razemovafaghiat.tellfa.android D/ImageData: image : http:/razemovafaghiat.ir/uploads/download/egve.jpg
中显示图片?谢谢所有&lt; 3
答案 0 :(得分:3)
您遵循了正确的方法。但问题非常棘手,但易于解决。
您收到的网址是
http:/razemovafaghiat.ir/uploads/download/egve.jpg
但应该是这个,
http://razemovafaghiat.ir/uploads/download/egve.jpg
Glide 无法理解或解析以前的网址。我尝试过,它与第二个网址完美配合。
只需将您从服务器收到的网址更改为正确的网址格式即可。如果您无法控制后端服务器,请尝试更正客户端代码中的URL。
如果您希望看到此工作一次,请尝试一次,
Glide.with(getContext()).load("http://razemovafaghiat.ir/uploads/download/egve.jpg").into(mImageView);