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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:44:07+00:00 2026-06-12T17:44:07+00:00

I’m new to Android programming and could really use some help with a program

  • 0

I’m new to Android programming and could really use some help with a program I’m writing to establish a Http connection and display an image.

I’m using the ‘Beginning Android Application Development’ book by Wei-Meng Lee. The code compiles and there are no errors flagging up but every time I run the program the ‘error connecting’ message appears and no image is displayed.

I’ve looked at various samples of code for GET requests but can’t find anything that works with my code.

Any help that anyone can offer would be greatly appreciated as I am struggling to see any solution so far.

The last line of code about uses-permission was included in the Manifest.

ImageView image;

private InputStream OpenHttpConnection(String urlString)
throws IOException {
    InputStream in = null;
    int response = -1;

    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();

    if(!(conn instanceof HttpURLConnection))
        throw new IOException("Not an HTTP Connection");

    try {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();
        response = httpConn.getResponseCode();
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();
        }
    }
    catch (Exception ex) {
        throw new IOException("Error connecting");
    }
    return in;

}

private Bitmap DownloadImage(String URL) {
    Bitmap bitmap = null;
    InputStream in = null;
    try {
        in = OpenHttpConnection(URL);
        bitmap = BitmapFactory.decodeStream(in);
        in.close();
    }
    catch (IOException e1) {
        Toast.makeText(this, e1.getLocalizedMessage(), Toast.LENGTH_LONG).show();
        e1.printStackTrace();
    }
    return bitmap;
}                                                                                                                                                                       

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

    Bitmap bitmap = DownloadImage("http://www.mayoff.com/5-01cablecarDCP01934.jpg");
    image = (ImageView) findViewById(R.id.image);
    image.setImageBitmap(bitmap);

}

<uses-permission
    android:name="android.permission.INTERNET" />  
  • 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-12T17:44:08+00:00Added an answer on June 12, 2026 at 5:44 pm

    Maybe problem is you having because of api version. you must use AsyncTask class for accessing web functions.

    This may be related to the fact that in apis 11 and above accessing the net in Main thread is not allowed you may have to use ASYNC task.

    example for using ASYNC task;

    class InternetFileCheack extends AsyncTask<Object, Void, Boolean> {
    
      private Button btn;
      private String fileURL;
      Context c;
    
      public InternetFileCheack (Button imv, String url, Context ctx) {
       this.btn = imv;
       this.fileURL = url;
       this.c = ctx;
      }
    
      @Override
      protected Boolean doInBackground(Object... params) {
       Boolean sonuc = null;
       try {
        URL u = new URL(fileURL);
        HttpURLConnection huc = (HttpURLConnection) u.openConnection();
        huc.setRequestMethod("HEAD");
        huc.connect();
        int code = huc.getResponseCode();
    
        if (code == HttpURLConnection.HTTP_OK) {
         sonuc = true;
        } else {
         sonuc = false;
        }
       } catch (Exception e) {
        sonuc = false;
       }
       return sonuc;
      }
    
      @Override
      protected void onPostExecute(Boolean result) {
       btn.setEnabled(result);
    
       if (result) {
        Toast.makeText(c, "", Toast.LENGTH_LONG).show();
        btn.setVisibility(View.VISIBLE);
       } else {
        btn.setVisibility(View.GONE);
       }
      }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters

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.