0

I am get the JSON data from PHP. And pass to Android.

Here is my PHP code:

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

And here is how I read the image:

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

Here is how I parse the response from the PHP code:

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) { }

In that way I can view only the last image on my Android mobile... I want display all images from the database into the Android mobile. How can I get all images using JSON data?

7
  • This is insufficient information. we need to know more about the services the php code provides and what responses you get from them. What is Qrimage? What is e here: Qrimage=e.getString("itemimage") Commented Mar 24, 2012 at 8:00
  • <?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(); ?> the above code my php code Commented Mar 24, 2012 at 8:50
  • Please modify your question with elaborations like this. Also you still have not provided the java side code: the logic of constructing this e of yours. Commented Mar 24, 2012 at 9:11
  • my java 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) { } here connect with server i can provide code please see i don't how to modify i am new for stack over flow please help me in android Commented Mar 24, 2012 at 9:39
  • this is my response to the server 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") Commented Mar 24, 2012 at 9:40

1 Answer 1

2

Low you've got quite a complex code there. However, I think your problem is very simple: you actually get all the images, but in those two lines:

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

You overwrite the prevoius picture with the next one to come. Thus only the last one is displayed eventually. You can easily check that by changing the for cycle to:

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

Then the second to last image should be shown. If this really is the case you will need to make out a way to display all the images simultanously.

I just couldn't find any other problem in your code, but please if I got it wrong, write back so I can try to help more.

EDIT As for how you can display the images together: you use image gallery. The image gallery is just another type of android widget (like the image view).

In your activity do the following few lines (initialize your gallery):

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

Here I use image gallery adapter which I define as follows:

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];
    }
}

And you need to declare this image gallery in the layout of your activity:

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

Hopefully this all will help you, however, I have written it in text editor, without trying, so it might be that I made some error. If so please write back.

EDIT2 If you want to stack the images vertically just use ListView instead of the gallery. You will still need to set the adapter, but you can use the same one I already provided you with. However, keep in mind that the ListView will not necessarily show the images one by one.

Sign up to request clarification or add additional context in comments.

11 Comments

ya sir what u told exactly correct i get previous image in my emulator for using your for loop. i want display all images in my emulator .. please help m
I was afraid of this... Ok I will try this give me few moments
sir i want to display all images in my emulator please say the possible solution for me
i try your given code but it show error in log cat like this error in parsing data.java.lang.class exception:android.widget.image in logcat error please tell me how to solve this error
So you got it working? :) Now comes the time to pay back with an accept and upvote, please :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.