【问题标题】:How to retrieve the images from a MySQL database using JSON in Android如何在 Android 中使用 JSON 从 MySQL 数据库中检索图像
【发布时间】:2012-04-08 15:39:21
【问题描述】:

我从 PHP 获取 JSON 数据。并传递给 Android。

这是我的 PHP 代码:

<?php
   mysql_connect("localhost","root","MobixMySQL");
   mysql_select_db("cozydine");
   $q=mysql_query("SELECT itemimage from fooditem");
   while($obj=mysql_fetch_object($q)){
       $arr[]=$obj;
   }
   echo '{"names":'.json_encode($arr).'}';
   mysql_close();
?>

这是我阅读图像的方式:

try {
    BufferedReader reader =
        new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    is.close();
    result = sb.toString();
    jArray = new JSONObject(result);
    JSONArray json = jArray.getJSONArray("names");
    for (int i = 0; i < json.length(); i++) {
        HashMap<String, String> map = new HashMap<String, String>();
        JSONObject e = json.getJSONObject(i);
        Qrimage=e.getString("itemimage");
        System.out.println(Qrimage);

        byte[] qrimage = Base64.decode(Qrimage.getBytes());

        System.out.println(qrimage);
        bmp = BitmapFactory.decodeByteArray(qrimage, 0,qrimage.length);
        ImageView imageview=(ImageView) findViewById(R.id.imageView1);
        imageview.setImageBitmap(bmp);

这是我如何解析来自 PHP 代码的响应:

codeInputStream is = null;
String result = "";
JSONObject jArray = null;
try {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(serverurl+"showall.php");
    HttpResponse response = httpClient.execute(httpPost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
} catch (Exception e) { }

这样,我只能在我的 Android 手机上查看最后一张图片...我想将数据库中的所有图片显示到 Android 手机中。如何使用 JSON 数据获取所有图像?

【问题讨论】:

  • 信息不足。我们需要更多地了解 php 代码提供的服务以及您从它们那里得到的响应。 Qrimage 是什么?这里e是什么:Qrimage=e.getString("itemimage")
  • 以上代码我的php代码
  • 请用这样的阐述修改您的问题。另外你还没有提供java端代码:构造这个e的逻辑。
  • 我的 java codeInputStream is = null;字符串结果 = ""; JSONObject jArray = null;尝试 { HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(serverurl+"showall.php"); HttpResponse 响应 = httpClient.execute(httpPost); HttpEntity 实体 = response.getEntity();是 = entity.getContent(); } catch (Exception e) { } 这里连接服务器我可以提供代码请看我不怎么修改我是新的堆栈溢出请在android中帮助我
  • 这是我对服务器的响应 try { BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder();字符串线=空; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close();结果 = sb.toString(); jArray = new JSONObject(结果); JSONArray json = jArray.getJSONArray("names"); for (int i = 0; i map = new HashMap(); JSONObject e = json.getJSONObject(i); Qrimage=e.getString("itemimage")

标签: php android json


【解决方案1】:

Low 你有一个相当复杂的代码。但是,我认为您的问题很简单:您实际上获得了所有图像,但是在这两行中:

ImageView imageview=(ImageView) findViewById(R.id.imageView1);
imageview.setImageBitmap(bmp);

你用下一张来覆盖上一张图片。因此最终只显示最后一个。您可以通过将 for 循环更改为:

for (int i = 0; i < json.length() - 1; i++) {

然后应该显示倒数第二张图片。如果确实是这种情况,您将需要找到一种同时显示所有图像的方法。

我只是在您的代码中找不到任何其他问题,但如果我弄错了,请回信,以便我可以尝试提供更多帮助。

编辑至于如何将图像显示在一起:您使用图像库。图片库只是另一种类型的 android 小部件(如图片视图)。

在您的活动中执行以下几行(初始化您的画廊):

try {
    BufferedReader reader =
        new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    is.close();
    result = sb.toString();
    jArray = new JSONObject(result);
    JSONArray json = jArray.getJSONArray("names");
    Gallery g = (Gallery) findViewById(R.id.gallery);
    g.setAdapter(new ImageAdapter(this, json));

这里我使用图片库适配器,我定义如下:

public class ImageAdapter extends BaseAdapter {

    private ImageView[] mImages;

    public ImageAdapter(Context context, JSONArray imageArrayJson) {
        this.mImages = new ImageView[imageArrayJson.length];
        String qrimage;

        for (int i = 0; i < imageArrayJson.length(); i++) {
            JSONObject image = imageArrayJson.getJSONObject(i);
            qrimage = image.getString("itemimage");

            byte[] qrimageBytes = Base64.decode(qrimage.getBytes());

            bmp = BitmapFactory.decodeByteArray(qrimageBytes,
                                                0,
                                                qrimageBytes.length);
            mImages[i] = new ImageView(context);
            mImages[i].setImageBitmap(bmp);
            mImages[i].setLayoutParams(new Gallery.LayoutParams(150, 100));
            mImages[i].setScaleType(ImageView.ScaleType.FIT_XY);
        }
    }

    public int getCount() {
        return mImages.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        return mImages[position];
    }
}

并且你需要在你的活动布局中声明这个图片库:

<Gallery xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>

希望这一切对您有所帮助,但是,我已经在文本编辑器中编写了它,没有尝试,所以可能是我犯了一些错误。如果有,请回信。

EDIT2如果您想垂直堆叠图像,只需使用ListView 而不是图库。您仍然需要设置适配器,但您可以使用我已经提供给您的同一个适配器。但是请记住,ListView 不一定会一张一张地显示图像。

【讨论】:

  • 先生,您所说的完全正确,我在模拟器中获得了以前的图像,以使用您的 for 循环。我想在我的模拟器中显示所有图像..请帮助我
  • 我害怕这个......好吧,我会试试这个给我一些时间
  • 先生,我想在我的模拟器中显示所有图像,请告诉我可能的解决方案
  • 我尝试了您给定的代码,但它在 log cat 中显示错误,例如解析 data.java.lang.class 异常时出现的错误:android.widget.image in logcat 错误请告诉我如何解决此错误
  • 所以你成功了吗? :) 现在是时候用接受和支持来回报了,拜托:)
猜你喜欢
  • 1970-01-01
  • 2014-02-19
  • 2015-07-06
  • 2013-01-08
  • 2013-03-17
  • 2016-07-16
  • 2021-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多