我从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数据获取所有图像?
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
不一定会逐个显示图像。