【问题标题】:Can't Load Image from JSON无法从 JSON 加载图像
【发布时间】:2018-04-05 04:41:11
【问题描述】:

我想显示一个包含文本和图像的列表。 存储在我的在线数据库中的文本和图像, 我使用 JSON 将它们带到我的 android 应用程序中。

JSON 不显示任何错误,显示文本但不显示图像。

我检查了 logcat,这个过程没有错误。我使用 viewAdapter 在列表中显示图像。

请帮帮我,你能给我一些简单的解释如何解决这个问题吗?

谢谢...

注意。这是我的 CustomListAdaper2 代码:

public class CustomListAdaper2 extends ArrayAdapter<contenus> {
ArrayList<contenus> contenus;
Context context;
int resource;
public CustomListAdaper2(@NonNull Context context, @LayoutRes int resource, 
@NonNull ArrayList<contenus> contenus) {
    super(context, resource, contenus);
    this.contenus = contenus;
    this.context = context;
    this.resource = resource;
}@NonNull
@Override
public View getView(int position2, @Nullable View convertView, @NonNull ViewGroup parent) {
    if(convertView==null){
        LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.custom_list_layout_layout, null, true);
    }
    contenus contenus = getItem(position2);
    ImageView imageView = (ImageView) convertView.findViewById(imageView5);
    Picasso.with(context).load(contenus.getImage()).into(imageView);
    TextView textViewtitle = (TextView) convertView.findViewById(R.id.textViewtitle);
    textViewtitle.setText((replacehtml(contenus.getNom())));
    TextView textViewcity = (TextView) convertView.findViewById(R.id.textViewcity);
    textViewcity.setText((replacehtml(contenus.getVille())));
    TextView textViewtime = (TextView) convertView.findViewById(R.id.textViewtime);
    textViewtime.setText((replacehtml(contenus.getTime())));
    TextView textViewprice = (TextView) convertView.findViewById(R.id.textViewprice);
    textViewprice.setText((replacehtml(contenus.getPrix())));




    return convertView;
}

}

这是我的 json 代码:

public class Multimedia extends AppCompatActivity {
ArrayList<contenus> arrayList2;
ListView lv2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_multimedia);
    arrayList2 = new ArrayList<>();
    lv2 = (ListView) findViewById(R.id.ListView2);

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            new ReadJSON().execute("http://wach.ma/mobile/category.php?id=Pour_La_Maison_Et_Jardin");
        }
    });
}

class ReadJSON extends AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... params) {
        return readURL(params[0]);
    }

    @Override
    protected void onPostExecute(String content) {
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(content);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JSONArray jsonArray = null;
        try {
            jsonArray = jsonObject.getJSONArray("articles");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject contenusobject = null;
            try {
                contenusobject = jsonArray.getJSONObject(i);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            try {
                arrayList2.add(new contenus(
                        contenusobject.getString("picture"),
                        contenusobject.getString("name"),
                        contenusobject.getString("city"),
                        contenusobject.getString("add_time"),
                        contenusobject.getString("price")

                ));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            CustomListAdaper2 adaper2 = new CustomListAdaper2(
                    getApplicationContext(), R.layout.custom_list_layout_layout, arrayList2
            );
            lv2.setAdapter(adaper2);
        }

    }


    @NonNull
    private String readURL(String theURL) {
        StringBuilder content = new StringBuilder();
        try {
            URL url = new URL(theURL);
            URLConnection urlConnection = url.openConnection();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                content.append(line + "\n");
            }
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content.toString();
    }
}
}

【问题讨论】:

  • 为什么不用RecyclerView?
  • 这条线 ImageView imageView = (ImageView) convertView.findViewById(imageView5); 看起来不对。 imageView5 是什么?
  • @gilbertxenodike 因为我在其他项目中使用相同的代码并且它工作正常。另一个项目显示 1 个图像视图和 1 个文本视图。这个项目必须显示 1 个图像视图和 4 个文本视图。但它仍然没有显示图像视图!
  • @Neeraj imageView5 是图像布局中的项目:
  • 那么为什么是 convertView.findViewById(imageView5) 而不是 convertView.findViewById(R.id.imageView5) 呢?是静态导入吗?

标签: android json image load


【解决方案1】:

只需编辑此代码...

ImageView imageView = (ImageView) convertView.findViewById(R.id.your_image_view_id);

否则一切都很好......我测试它。

更新

在使用图片的网址之前,请确保

当您设置值时,您将在模型承包商上分配写入位置。

arrayList2.add(new contenus(
                    //is this "picture" is first argument 
                    contenusobject.getString("picture"), 
                    contenusobject.getString("name"),
                    contenusobject.getString("city"),
                    contenusobject.getString("add_time"),
                    contenusobject.getString("price")

));

//Or Check hare

String url = contenus.getImage();
Log.d("image url", url);
Picasso.with(context).load(url).into(imageView);

那么imageView5imageView 的正确ID 吗?

【讨论】:

  • 是的。一切顺利。没有错误。但是当我在模拟器中运行它时,它不会显示图像!
猜你喜欢
  • 2016-10-12
  • 2015-07-25
  • 2013-01-11
  • 2012-06-30
  • 1970-01-01
  • 1970-01-01
  • 2016-05-25
  • 2020-08-03
相关资源
最近更新 更多