0

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) {
    sb.append(line);
}
    reader.close();
    return sb.toString();
}

Thank You ! :)

Edited by Stefan_1

4
Contributors
18
Replies
32
Views
3 Years
Discussion Span
Last Post by JamesCherrill
Featured Replies
  • You probably should be useing the `URLEncoder` class. See [stackoverflow: Java URL encoding](http://stackoverflow.com/questions/10786042/java-url-encoding) Read More

  • (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 … Read More

  • 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 … Read More

0

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.

0

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

1

(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 by JamesCherrill

0

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) {
    e.printStackTrace();
    LabelInfo.setText("No Internet Connection !" + e);
    return;
}
0

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.

1

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:
http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html

Edited by JamesCherrill

0

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 ...

0

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

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.