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

  • Home
  • SEARCH
  • 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 7886669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:22:38+00:00 2026-06-03T05:22:38+00:00

I have problem with Android using the class HttpUrlConnection my method is this: public

  • 0

I have problem with Android using the class HttpUrlConnection my method is this:

public InputStream test() {
        URL url = null;
        try {
            url = new URL("http://www.android.com/");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       HttpURLConnection urlConnection = null;
    try {
        urlConnection = (HttpURLConnection) url.openConnection();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    InputStream in= null;

       try {
         in = new BufferedInputStream(urlConnection.getInputStream());
       }catch(IOException e){
           e.printStackTrace();
       }
        finally {
         urlConnection.disconnect();
       }
return in;
}

When i execute this method android generates this exception:

05-03 15:15:19.116: W/System.err(1085): android.os.NetworkOnMainThreadException
05-03 15:15:19.116: W/System.err(1085): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
05-03 15:15:19.116: W/System.err(1085): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
05-03 15:15:19.116: W/System.err(1085): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
05-03 15:15:19.116: W/System.err(1085): at java.net.InetAddress.getAllByName(InetAddress.java:220)
05-03 15:15:19.126: W/System.err(1085): at libcore.net.http.HttpConnection.(HttpConnection.java:71)
05-03 15:15:19.126: W/System.err(1085): at libcore.net.http.HttpConnection.(HttpConnection.java:50)
05-03 15:15:19.126: W/System.err(1085): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
05-03 15:15:19.126: W/System.err(1085): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
05-03 15:15:19.126: W/System.err(1085): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-03 15:15:19.126: W/System.err(1085): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
05-03 15:15:19.126: W/System.err(1085): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
05-03 15:15:19.126: W/System.err(1085): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
05-03 15:15:19.126: W/System.err(1085): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
05-03 15:15:19.126: W/System.err(1085): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
05-03 15:15:19.136: W/System.err(1085): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
05-03 15:15:19.136: W/System.err(1085): at com.BertacchiMazzoni.HTTPClient.HttpClientTutorial.test(HttpClientTutorial.java:35)
05-03 15:15:19.136: W/System.err(1085): at com.BertacchiMazzoni.HTTPClient.HTTPClientActivity.onCreate(HTTPClientActivity.java:18)
05-03 15:15:19.136: W/System.err(1085): at android.app.Activity.performCreate(Activity.java:4465)
05-03 15:15:19.136: W/System.err(1085): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-03 15:15:19.136: W/System.err(1085): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-03 15:15:19.136: W/System.err(1085): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-03 15:15:19.136: W/System.err(1085): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-03 15:15:19.136: W/System.err(1085): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-03 15:15:19.146: W/System.err(1085): at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 15:15:19.146: W/System.err(1085): at android.os.Looper.loop(Looper.java:137)
05-03 15:15:19.146: W/System.err(1085): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-03 15:15:19.146: W/System.err(1085): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 15:15:19.146: W/System.err(1085): at java.lang.reflect.Method.invoke(Method.java:511)
05-03 15:15:19.146: W/System.err(1085): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-03 15:15:19.146: W/System.err(1085): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-03 15:15:19.146: W/System.err(1085): at dalvik.system.NativeStart.main(Native Method)

Can i resolve this problem? Where is the error?

Thanks a lot!
Regards!

  • 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-03T05:22:39+00:00Added an answer on June 3, 2026 at 5:22 am

    A quick google for the exception leads you here: http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

    Which states:

    The exception that is thrown when an application attempts to perform a
    networking operation on its main thread.

    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. See the document Designing for Responsiveness.

    So basically I’m guessing you’re performing your network operation on the main application thread; and for Honeycomb and up it looks like you can’t do this. It’s a bad idea anyway, as it’s likely to make your application hang and appear unresponsive. You’d need to use another thread or handler or asynctask or Loader to perform the operation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with android 4.0.3. I'm using the method below to get local
I have this problem when trying to run hello world program using android SDK.
I have a problem with my code in Android. I am using this to
I have a problem trying to stream audio using the AsyncPlayer class in android.
I am using this Android class to implement a countdown: http://developer.android.com/reference/android/os/CountDownTimer.html I have created
I have run into a strange problem on Android , using jQuery mobile and
I'm using ormlite for my android-app. But now i have a problem. I have
I have a small problem with android maps. I added an ItemizedOverlay class to
I'm new to this android. i'm using a service to do some background work.
I have the following code in android - I'm using asmack API. public XMPPManager(String

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.