Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8850901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:00:32+00:00 2026-06-14T13:00:32+00:00

I m working on a project which POST a request to the web server

  • 0

I m working on a project which POST a request to the web server and web server responds in XML format. here is the actual story starts.
Actually i m sending the request to web server in the same activity and getting response in the same activity, the response contains the images of 500X800 in resolution and some plain text.

public class Home extends Activity implements AdapterView.OnItemClickListener {
    /** Called when the activity is first created. */

    Context context = Home.this;
    ArrayList<String> BookTitle = new ArrayList<String>();
    ArrayList<String> BookCoverPhotos = new ArrayList<String>();
    ArrayList<String> BookAuther = new ArrayList<String>();
    ArrayList<String> BookPublishDate = new ArrayList<String>();

    URL bookImageURL = null;
    Bitmap bitMapImage = null;

    View homeTabLayout;
    View reviewLayout;
    ImageButton btnBack;

    // All static variables
    static final String URL = "http://www.shiaislamiclibrary.com/requesthandler.ashx";
    // XML node keys
    static final String KEY_ITEM = "Book"; // parent node
    static final String KEY_BOOKAUTHOR = "BookAuthor";
    static final String KEY_BOOKDATEPUBLISHED = "DatePublished";
    static final String KEY_BOOKTITLE = "BookTitle";
    static final String KEY_BOOKCODE = "BookCode";
    static final String KEY_BOOKIMAGE = "BookImage";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_activity);

        // Get XML Data in a Array List

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL, "Imam Ali"); // getting
                                                            // XML
        Log.i("XML FORMAT", xml);
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_ITEM);

        // looping through all item nodes <item>
        for (int i = 0; i < nl.getLength(); i++) {
            Element e = (Element) nl.item(i);

            BookTitle.add(parser.getValue(e, KEY_BOOKTITLE));
            BookCoverPhotos.add("http://shiaislamicbooks.com/books_Snaps/"
                    + parser.getValue(e, KEY_BOOKCODE) + "/1.jpg");
            BookAuther.add(parser.getValue(e, KEY_BOOKAUTHOR));
            BookPublishDate.add(parser.getValue(e, KEY_BOOKDATEPUBLISHED));
            Log.i("URLs", BookPublishDate + "::" + BookAuther + "::"
                    + BookTitle + BookCoverPhotos.toString());

        }

        // OverView Page Views...

        final ImageView imgOverVuBookCoverPhoto = (ImageView) findViewById(R.id.img_OverVuBookCoverPhoto);
        final TextView txtOverVuBookTitle = (TextView) findViewById(R.id.txt_revwBookTitle);
        final TextView txtOverVuBookAuthor = (TextView) findViewById(R.id.txt_revwAutherName);
        final TextView txtBookPublishDate = (TextView) findViewById(R.id.txt_PublishDate);

        homeTabLayout = findViewById(R.id.rel_HomeLayout);
        reviewLayout = findViewById(R.id.scroll_ReviewLayout);
        reviewLayout.setVisibility(View.GONE);

        btnBack = (ImageButton) findViewById(R.id.btnBack);
        btnBack.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                reviewLayout.setVisibility(View.GONE);
                homeTabLayout.setVisibility(View.VISIBLE);

            }
        });

        final GridView gridView = (GridView) findViewById(R.id.gridview);
        gridView.setAdapter(new ImageAdapter(this));
        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                    long arg3) {

                reviewLayout.setVisibility(View.VISIBLE);
                homeTabLayout.setVisibility(View.GONE);

                Toast.makeText(context, BookTitle.get(pos), Toast.LENGTH_SHORT)
                        .show();

                txtOverVuBookTitle.setText(BookTitle.get(pos));
                txtOverVuBookAuthor.setText(BookAuther.get(pos));
                txtBookPublishDate.setText(BookPublishDate.get(pos));

                try {
                    bookImageURL = new URL(BookCoverPhotos.get(pos));
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    Toast.makeText(context, "Network Error", Toast.LENGTH_LONG)
                            .show();
                    Log.i("URL", "ERROR");
                }

                try {
                    bitMapImage = BitmapFactory.decodeStream(bookImageURL
                            .openConnection().getInputStream());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Toast.makeText(context, "Bitmap Error", Toast.LENGTH_LONG)
                            .show();
                    Log.i("BITMAP", "ERROR");
                }

                imgOverVuBookCoverPhoto.setImageBitmap(bitMapImage);
            }
        });

    }

    public class ImageAdapter extends BaseAdapter {
        private Context context;

        public ImageAdapter(Context c) {
            context = c;
        }

        // ---returns the number of images---
        public int getCount() {
            // return imageIDs.length;
            return BookCoverPhotos.size();
            // return 6;
        }

        // ---returns the ID of an item---
        public Object getItem(int position) {
            return position;
        }

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

        // ---returns an ImageView view---
        public View getView(int position, View convertView, ViewGroup parent) {

            // ImageView bmImage;

            ImageView img_BookCoverPhoto;
            img_BookCoverPhoto = new ImageView(context);

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View row = inflater.inflate(R.layout.grid_style, parent, false);

            TextView txt_BooksTitle = (TextView) row
                    .findViewById(R.id.txt_BookTitle);
            txt_BooksTitle.setText(BookTitle.get(position) + "");

            img_BookCoverPhoto = (ImageView) row
                    .findViewById(R.id.imgBookCover);

            try {
                bookImageURL = new URL(BookCoverPhotos.get(position));
            } catch (MalformedURLException e) {
                e.printStackTrace();
                Toast.makeText(context, "Network Error", Toast.LENGTH_LONG)
                        .show();
                Log.i("URL", "ERROR");
            }

            try {
                bitMapImage = BitmapFactory.decodeStream(bookImageURL
                        .openConnection().getInputStream());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(context, "Bitmap Error", Toast.LENGTH_LONG)
                        .show();
                Log.i("BITMAP", "ERROR");
            }
            // img_BookCoverPhoto.setImageResource(imageIDs[position]);
            img_BookCoverPhoto.setImageBitmap(bitMapImage);

            return row;
        }

    }

    public class AsyncTaskForUI extends AsyncTask<String, Integer, String> {
        ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {

            super.onPreExecute();
            progressDialog = ProgressDialog
                    .show(context,
                            "Loading...",
                            "This may take a few minuts\nDepending Upon your Internet Connection Speed",
                            true);

        }

        @Override
        protected String doInBackground(String... params) {

            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {

            super.onPostExecute(result);
            progressDialog.dismiss();
            Toast.makeText(context, "Completed", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

    }
}

i m loading the images and text in the custom GridView which contains a ImageView and a textView. here is the XML of layout

    <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget44"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imgBookCover"
        android:layout_width="150dp"
        android:layout_height="180dp"
        android:scaleType="center"
        android:adjustViewBounds="true"
        android:src="@drawable/book1">
    </ImageView>

    <TextView
        android:id="@+id/txt_BookTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="TextView"
        android:layout_margin="5dp"
        android:textColorHighlight="#656565" 
        android:textColor="@color/White">
    </TextView>

</LinearLayout>

also here is my XMLParser.java class code

    public class XMLParser {

    // constructor
    public XMLParser() {

    }

    /**
     * Getting XML from URL making HTTP request
     * 
     * @param url
     *            string
     * */
    public String getXmlFromUrl(String url, String SearchKeywords) {
        String xml = null;

        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();

            HttpPost httpPost = new HttpPost(url);

            /*ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                    3);
            nameValuePairs.add(new BasicNameValuePair("type", "Free Books"));
            nameValuePairs.add(new BasicNameValuePair("lang", "English"));
            nameValuePairs.add(new BasicNameValuePair("book_title", "hasan"));

            try {
                httpPost.setEntity(new UrlEncodedFormEntity(
                        nameValuePairs));

            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }*/

            String postParameter = "type=Free Books&lang=English&book_title="+SearchKeywords;

            try {
                httpPost.setEntity(new StringEntity(postParameter));
            }catch (Exception e) {
                // TODO: handle exception
            }


            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // return XML
        return xml;
    }

    /**
     * Getting XML DOM element
     * 
     * @param XML
     *            string
     * */
    public Document getDomElement(String xml) {
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is);

        } catch (ParserConfigurationException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (SAXException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }

        return doc;
    }

    /**
     * Getting node value
     * 
     * @param elem
     *            element
     */
    public final String getElementValue(Node elem) {
        Node child;
        if (elem != null) {
            if (elem.hasChildNodes()) {
                for (child = elem.getFirstChild(); child != null; child = child
                        .getNextSibling()) {
                    if (child.getNodeType() == Node.TEXT_NODE) {
                        return child.getNodeValue();
                    }
                }
            }
        }
        return "";
    }

    /**
     * Getting node value
     * 
     * @param Element
     *            node
     * @param key
     *            string
     * */
    public String getValue(Element item, String str) {
        NodeList n = item.getElementsByTagName(str);
        return this.getElementValue(n.item(0));
    }
}

the code is working just fine but, its hangs app until the server sends response and my app shows black screen until it loads all the images and text in the desired views. and when i scroll the the gridView sometimes it gives exception

java.lang.OutOfMemoryError: bitmap size exceeds VM budget
  • but the problem is that now i want to do XML parsing and bitmap decoding in the AsyncTask. how can i achieve the both tasks in a single doInbackground() method of the AsynTask class.
  • also give a code snippet of resizing the high resolution images to low resolution using the AsyncTask class.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:00:33+00:00Added an answer on June 14, 2026 at 1:00 pm

    You should check the size of the image to be downloaded before decoding the image into the memory which can be done by enabling inJustDecodeBounds from BitmapFactory Options. After that you can scale your Bitmap according to the required size to be loaded. I had answered here in detail how we can get the size of the Bitmap before loading it in memory.

    I had created a method that takes the url, requiredHeight, requiredWidth of the Image and returns a Bitmap that is suitable after scaling.

    private static Bitmap decodeBitmapFromStream(String urlString, 
                                                  int reqWidth, int reqHeight) {
    
            URL url = null;
                InputStream is = null;
                try {
                    url = new URL(urlString);
                    is = (InputStream) url.getContent();
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
                // First decode with inJustDecodeBounds=true to check dimensions
                final BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(is, null, options);
    
                // Height and Width of downloaded Image
                int height = options.outHeight;
                int width = options.outWidth;
    
                // Calculate inSampleSize
                options.inSampleSize = Math.min(width/reqWidth, height/reqHeight);
    
           // As InputStream can be used only once we have to regenerate it again.
                try {
                    is = (InputStream) url.getContent();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                // Decode bitmap with inSampleSize set
                options.inJustDecodeBounds = false;
                return  BitmapFactory.decodeStream(is, null, options);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on a project which uses XSL-Transformations to generate HTML from XML.
I'm working on a new project which has some complex javascript. I can't post
I am working on a project in which my colleague implemented an https server,
I am working on a project that will receive HTTP POSTs which contain XML
I am working on project which includes communication of the server (JavaEE app) and
Can anyone help me find an up-to-date, working ATL project which has a main
Iam working on a project which involves writing a Mork File (Mork is a
I'm working on a project which contains several slave nodes and one master node.
I'm working on a project which involves two processes, let them be A and
I started working on a project which uses Net_URL_Mapper from PEAR (php) as the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.