-1

Hi I'm trying to connect with internet with this code:

  DefaultHttpClient httpclient = new DefaultHttpClient();  
  HttpPost httppost = new HttpPost("http://192.168.0.100/webnet/xyz.php");
  String result = null;
  InputStream is = null;
  StringBuilder sb = null;
  HttpResponse response = httpclient.execute(httppost);

I registered Internet permission on manifest file

I got this error:

System.err android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)

I tests this app with API level 8

  • which os version have you tested? – Nirmal Aug 06 '13 at 12:40
  • 1
    I have to ask, do you not have a search engine accessible from your location?, possible duplicate of [HTTP doesn't work in Android emulator](http://stackoverflow.com/questions/11277734/http-doesnt-work-in-android-emulator) – KevinDTimm Aug 06 '13 at 12:40

4 Answers4

4

You can not run long running task on main thread. please move your network related task in background.

Refer this : AsyncTask Android example

Community
  • 1
  • 1
Juned
  • 6,290
  • 7
  • 45
  • 93
2

Since Android 3.0, you'll see this error when you're trying to make network requests from the main thread. Move this code to a separate thread.

Egor
  • 39,695
  • 10
  • 113
  • 130
2

Try this before executing this block of code. may works in some cases:

try {
                Class strictModeClass=Class.forName("android.os.StrictMode");
                Class strictModeThreadPolicyClass=Class.forName("android.os.StrictMode$ThreadPolicy");
                Object laxPolicy = strictModeThreadPolicyClass.getField("LAX").get(null);
                Method method_setThreadPolicy = strictModeClass.getMethod("setThreadPolicy",strictModeThreadPolicyClass );
                method_setThreadPolicy.invoke(null,laxPolicy);
            }
        catch (Exception e) {

            }

And Try this before asking questions here: "google.com" # agree with kevinDTimm

Anyway you SHOULD NOT call network on UI threads.

GOBINATH.M
  • 663
  • 2
  • 12
  • 24
  • 2
    I'd say this is the worst solution for this problem, nevermind it was accepted. Don't do that! – Egor Aug 06 '13 at 13:07
1

You check first which ip-address is the on (IPV4/IPV6)

Mohsin Shaikh
  • 494
  • 1
  • 4
  • 11