Hello i have a little problem with writing the words in the database, after some space between the words the database can't remember it, thats why because the link eg. ( /some_file.php?search=something%20wierd ) that "%20" its the space between the words but from my mobile application when ill type space doesnt write it into the database... what should i do ? :/

Also i want to give message "You are not connected to the Internet" if is true...

public static String sendSearch(String search) throws Exception {
    URL url = new URL("http://secretchatsk.host56.com/some_file.php?search="+search);
    URLConnection conn = url.openConnection();
    InputStream stream = new BufferedInputStream(conn.getInputStream());
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
    String line;
    StringBuilder sb = new StringBuilder();
    while ((line = reader.readLine()) != null) {
    return sb.toString();

Thank You ! :)

Edited 2 Years Ago by Stefan_1

try a ping before you do anything else.
ping for one or more very reliable sources
(www.google.com, www.facebook.com, www.microsoft.com, ... )
chances of those going offline are pretty slim.
ping fails? (especially for all of them?) it's pretty sure you're not connected.

try a ping before you do anything else.
ping for one or more very reliable sources
(www.google.com, www.facebook.com, www.microsoft.com, ... )
chances of those going offline are pretty slim.
ping fails? (especially for all of them?) it's pretty sure you're not connected.

Im sorry i dont get you, but i like the idea... :D
If you can repeat that ? :D

(Ignore previous version of this post - it doesn't work)

Sites may or may not respond to pings, so the only reliable way is to check a public service that definitely works. Eg try to make an connection to google.com on port 80 (google's search web site). eg

   InetAddress a = InetAddress.getByName("google.com");
   SocketAddress sockaddr = new InetSocketAddress(a, 80);
   socket = new Socket();
   socket.connect(sockaddr, 2000);

If the internet connection is unavailable for any reason the connect nethod will throw an IOException

Edited 2 Years Ago by JamesCherrill

Just like this

try {
    InetAddress g = InetAddress.getByName("https://www.google.com/");
    SocketAddress sockaddr = new InetSocketAddress(g, 80);
    Socket socket = new Socket();
    socket.connect(sockaddr, 2000);
} catch (Exception e) {
    LabelInfo.setText("No Internet Connection !" + e);

Your getByName doesn't contain a valid name. It's just "www.google.com" or "google.com" (see my example code).

You'll see from the stack trace that this is the Exception, not an IOException.

Oh, did you say before this android? My answers were valid for Windows/Mac/Linux, I don't use android at all, but the same code should work as long as it compiles!

Edit: Your exception is documented as "The exception that is thrown when an application attempts to perform a networking operation on its main thread."
It seems there's a problem with where you have put the code in your application. There's no such exception for "normal" Java, so that's all I know, but this may help:

Edited 2 Years Ago by JamesCherrill

I have tried on different ways but still showing that "No Internet Connection" but after 1 sec. later it shows that is sended to the database ...

I have solved the problem but now its crashing the app when no connection..

Once again: what was the exact exception stack trace?

Sorry i didn't see it..
Here is it all:

02-04 11:58:56.335: W/System.err(2378): android.os.NetworkOnMainThreadException
02-04 11:58:56.335: W/System.err(2378):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
02-04 11:58:56.335: W/System.err(2378):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
02-04 11:58:56.345: W/System.err(2378):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-04 11:58:56.345: W/System.err(2378):     at java.net.InetAddress.getByName(InetAddress.java:289)
02-04 11:58:56.345: W/System.err(2378):     at com.kafic.rafa.MainActivity$1.onClick(MainActivity.java:77)
02-04 11:58:56.345: W/System.err(2378):     at android.view.View.performClick(View.java:4204)
02-04 11:58:56.345: W/System.err(2378):     at android.view.View$PerformClick.run(View.java:17355)
02-04 11:58:56.355: W/System.err(2378):     at android.os.Handler.handleCallback(Handler.java:725)
02-04 11:58:56.355: W/System.err(2378):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 11:58:56.355: W/System.err(2378):     at android.os.Looper.loop(Looper.java:137)
02-04 11:58:56.355: W/System.err(2378):     at android.app.ActivityThread.main(ActivityThread.java:5041)
02-04 11:58:56.355: W/System.err(2378):     at java.lang.reflect.Method.invokeNative(Native Method)
02-04 11:58:56.355: W/System.err(2378):     at java.lang.reflect.Method.invoke(Method.java:511)
02-04 11:58:56.355: W/System.err(2378):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-04 11:58:56.355: W/System.err(2378):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-04 11:58:56.365: W/System.err(2378):     at dalvik.system.NativeStart.main(Native Method)
This question has already been answered. Start a new discussion instead.