无法从JSON加载图像

时间:2017-10-24 11:38:47

标签: android json image load

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

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

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

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

...谢谢

NB。这是我的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();
    }
}
}

1 个答案:

答案 0 :(得分:0)

只需编辑此代码......

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);

imageView5 imageView的正确ID是什么?

相关问题