4

I am using this code to fetch my changelog from the net.

InputStream content = null;
           try {


               URL url = new URL("http://dreamhawk.blinkenshell.org/changelog.txt");
               HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
               urlConnection.setRequestMethod("GET");
               urlConnection.connect();

               content = urlConnection.getInputStream();


               BufferedReader r = new BufferedReader(new InputStreamReader(content));
               StringBuilder total = new StringBuilder();
               String line;
               String NL = System.getProperty("line.separator");
               try {
                   while ((line = r.readLine()) != null) {
                       total.append(line + NL);
                   }
               } catch (IOException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               }

               String page = total.toString();

               Dialog dialog = new Dialog(Main.this);

               dialog.setContentView(R.layout.custom_dialog);
               dialog.setTitle(R.string.changelog);
               dialog.setCanceledOnTouchOutside(true);

               TextView text = (TextView) dialog.findViewById(R.id.text);
               text.setTextSize(13);
               text.setText(page);
               dialog.show();
           } catch (Exception e) {
               //handle the exception !
           }

This works, on Android 2.3 and below... But since the ICS update, i am getting nothing! No dialog, no response, no nothing! Help! :/

DreamHawk
  • 785
  • 2
  • 9
  • 20

2 Answers2

15

I know in android 3.0 network connection on the main thread isnt permitted.

StrictMode is turned on automatically. Im sure it is the same for 4.0

To fix the issue, you must perform the network connection on a separate thread... for example, using an AsyncTask.


Read this blog post on the subject:

Why Ice Cream Sandwich Crashes Your App

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
coder_For_Life22
  • 26,645
  • 20
  • 86
  • 118
  • Absolutely delicious ! Now my changelog comes up as an dialog, as it should... How come Google made it like this? Whats the reason? Again, thanks alot! – DreamHawk Dec 05 '11 at 20:43
  • Yeah. I did some head scratching a while back when i had the same issue. I guess google calls there selves building a more "secure" development process. =) – coder_For_Life22 Dec 05 '11 at 20:49
  • So weird :/. But i... Okay i haven't searched alot about android 3.0 and using NET SErvices there. But it was my next lead, to fire up a 3.0 Emulator and try there... Kind off making you furious, it works on one version, but not on the "latest" :/ – DreamHawk Dec 05 '11 at 20:53
  • 5
    Please don't just disable strict mode. Strict mode is telling you something. Making a network call on the UI-Thread will cause your app and the whole phone to freeze. Do your Network calls in an Async Task – Janusz Dec 13 '11 at 08:11
  • 1
    Answers like these are *very* dangerous to new Android developers. They see that it has a lot of upvotes and assume that it is correct. The original answer (simply turning off strict mode) is the worst possible solution to the problem... I've edited the answer to specify what should be done (as Janusz notes above). – Alex Lockwood Jul 02 '12 at 03:44
0

Well, why won't you use the WebClient? (usage: https://gist.github.com/2906703), you don't really need to mess up with the StrictMode Thread Policy. If google thought it was important to change it, who am I to disagree.

fsschmitt
  • 599
  • 5
  • 10