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 8235889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:58:07+00:00 2026-06-07T18:58:07+00:00

Somehow I don’t seem to get it. 8-} I have an activity in which

  • 0

Somehow I don’t seem to get it. 8-}
I have an activity in which I request JSON data from a database, an the return result includes an image URL. My app works fine on Android v2.x, but as I had to find out by accident, there is weird behavior – including the images not loaded – on Android 4 (that’s not what I’d call compatibility?!) Now I have to fix that, hoping the fix will not mess up functionality on Android 2.x systems again!

Basically, the activity works as described in all the numerous examples on the web (I removed most error checking etc. to trim the code down to the essential parts):

 new GetProductDetails().execute();
 (..)


 class GetProductDetails extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
            (..)
    }

    protected String doInBackground(String... args) {

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("pid", pid));
        // getting product details by making HTTP request
        JSONObject json = jsonParser.makeHttpRequest(
                getString(R.string.urlProductDetails), "GET",
                params);
        try {
               JSONArray prodObj = json.getJSONArray(Consts.PRODS);
                pinfo = prodObj.getJSONObject(0);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }


    protected void onPostExecute(String file_url) {
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                // Storing each json item in variable
                try {

                    String s = "";
                    txtvname = (TextView) findViewById(R.id.txtpname);
                    txtshortinfo = (TextView) findViewById(R.id.txtshortinfo);
(… populating activity window with json data …)

            }
        });


        // update image:
        String imgURL;
        try {
            imgURL = pinfo.getString("pic_url");
            if (imgURL.length() > 1) {
                imgURL = getString(R.string.urlPicsFull) + imgURL;
                ImageView imgProd = (ImageView) findViewById(R.id.imgProduct);
                Drawable drawable = LoadImageFromWebOperations(imgURL);
                imgProd.setImageDrawable(drawable);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        pDialog.dismiss();
    }

    private Drawable LoadImageFromWebOperations(String url) {
        try {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        } catch (Exception e) {
            System.out.println("Could not load " + url + ", Exc=" + e);
            return null;
        }
    }

Now, there is an android.os.NetworkOnMainThreadException when the image is loaded.

  • 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-07T18:58:10+00:00Added an answer on June 7, 2026 at 6:58 pm

    I suppose your targetSDKVersion is too high. Here is some documentation on what compatibility behaviour is disabled on which targetSDKVersion. NetworkOnMainThreadException is actually missing from that list. It is documented here as follows: “This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it’s heavily discouraged.”

    So if you lower your targetSDKVersion to a value below Honeycomb you can do networking on the main thread. Setting targetSDKVersion to a certain value is like saying: “I tested my application on that level.”

    1. onPostExceute always runs on the main thread. No need for using runOnUiThread() there.
    2. LoadImageFromWebOperations() needs to be called in doInBackground() to not run on the main thread. This is causing the exception.
    3. onPostExecute gets as the parameter what doInBackground returns. So if you do not use the parameter in onPostExcecute and return always null in doInBackground you can use Void as the result type (instead of String)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Somehow my master and my origin/master branch have diverged. I actually don't want them
somehow I get never any results when I call: select * from table_1 t1
I don't need to sort listView I only need to get highest value from
I don't code in PHP, but I have this form which I pulled off
I somehow must have overlooked the tell, don't ask OOP principle all these years
I read about HTTP persistent connection but somehow I don't seem to understand what
Somehow, i don't get it how i can set the height of jScrollPane. http://tinyw.in/BLrg
This question has been asked more then once, but somehow I don't get things
I don't believe that I am using any 3.0 specific APIs, but somehow whenever
So, i recently found this term related somehow to neural networks, but I don't

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.