使用JSON显示ImageView,不返回任何值

时间:2014-04-09 11:08:06

标签: php android json bitmap base64

我的问题是下一个......

我试图显示一个图像,该图像在我的phpmyadmin中定义,并托管在相应的文件夹中,但bitmapfactory返回nothin。

我的JAVA:

if (success == 1) {
                            // successfully received product details
                            JSONArray productObj = json.getJSONArray(TAG_PRODUCT); // JSON Array

                        // get first product object from JSON Array
                        JSONObject product = productObj.getJSONObject(0);


                        // product with this pid found
                        // Edit Text
                        txtName = (EditText) findViewById(R.id.inputName);
                        txtPrice = (EditText) findViewById(R.id.inputPrice);
                        txtDesc = (EditText) findViewById(R.id.inputDesc);


                        // display product data in EditText
                        txtName.setText(product.getString(TAG_NAME));
                        txtPrice.setText(product.getString(TAG_PRICE));
                        txtDesc.setText(product.getString(TAG_DESCRIPTION));

                        //imageview
                        imageView1 = (ImageView)findViewById(R.id.imageView1);
                        imagen_url=product.getString("TAG_IMAGEN");
                        String imagen_url2=product.optString("TAG_IMAGEN");
                        byte[] encodeByte = Base64.decode(imagen_url, Base64.DEFAULT);
                        Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);


                            imageView1.setImageBitmap(bitmap);

和PHP

        $product["imagen"] = base64_encode($result["imagen"]);

它返回错误:

04-10 04:12:32.344: W/System.err(1424): org.json.JSONException: No value for TAG_IMAGEN
04-10 04:12:32.364: W/System.err(1424):     at org.json.JSONObject.get(JSONObject.java:355)
04-10 04:12:32.364: W/System.err(1424):     at org.json.JSONObject.getString(JSONObject.java:515)
04-10 04:12:32.374: W/System.err(1424):     at com.example.androidhive.EditProductActivity$GetProductDetails$1.run(EditProductActivity.java:180)
04-10 04:12:32.374: W/System.err(1424):     at android.os.Handler.handleCallback(Handler.java:733)
04-10 04:12:32.374: W/System.err(1424):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-10 04:12:32.404: W/System.err(1424):     at android.os.Looper.loop(Looper.java:136)
04-10 04:12:32.404: W/System.err(1424):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-10 04:12:32.404: W/System.err(1424):     at java.lang.reflect.Method.invokeNative(Native Method)
04-10 04:12:32.414: W/System.err(1424):     at java.lang.reflect.Method.invoke(Method.java:515)
04-10 04:12:32.414: W/System.err(1424):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-10 04:12:32.414: W/System.err(1424):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-10 04:12:32.434: W/System.err(1424):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

问题是标签,如果我把:

imagen_url=product.getString("imagen");

有效,但没有:

imagen_url=product.getString("TAG_IMAGEN");

我不知道为什么,但是通过这种改变,它运作正常!