4

I am using android 2.3.3, i had made an rss reader which was working great, then i integrated the code of that simple rss reader into another activity, just copy pasted it carefully, no errors there.

The problem is when i run the app on my emulator it gives me error connecting exception. Then i figured out by putting toasts after everyline in try block that the problem is at httpconnection.connect(); line.

I have added the permission in the android manifest while my logcat gives a warning of javaio exception:error connecting.

    try {
        HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
        //Toast.makeText(this, "urlconnection passed", Toast.LENGTH_SHORT).show();
        httpURLConnection.setAllowUserInteraction(false);
        httpURLConnection.setInstanceFollowRedirects(true);
        //Toast.makeText(this, "setting response method", Toast.LENGTH_SHORT).show();
        httpURLConnection.setRequestMethod("GET");
        //Toast.makeText(this, "connecting", Toast.LENGTH_SHORT).show();
        httpURLConnection.connect();
        Toast.makeText(this, "on response code", Toast.LENGTH_SHORT).show();
        response = httpURLConnection.getResponseCode();
        //Toast.makeText(this, "response code passed", Toast.LENGTH_SHORT).show();
        if (response == HttpURLConnection.HTTP_OK) {
            //Toast.makeText(this, "OK", Toast.LENGTH_SHORT).show();
            inputStream = httpURLConnection.getInputStream();
        }

    } catch (Exception e) {
        // TODO: handle exception
        throw new IOException("Error connecting");
    }

Here the code which creates the object:

URL url = new URL(UrlString);
URLConnection connection = url.openConnection();
if (!(connection instanceof HttpURLConnection)) 
  throw new IOException("Not a HTTP connection"); 
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
Salik
  • 508
  • 6
  • 17
  • 35
  • make sure you have in manifast – ρяσѕρєя K Jul 01 '12 at 14:08
  • I have already included that in my manifest file – Salik Jul 01 '12 at 14:13
  • show code where you have created connection object – Vipul Jul 01 '12 at 14:18
  • could you tell me if there is a way i can figure out why exactly isnt the connect() line not working?because Log Cat is not fgiving me any useful hints. – Salik Jul 01 '12 at 14:18
  • here you go vipul: URL url = new URL(UrlString); URLConnection connection = url.openConnection(); if (!(connection instanceof HttpURLConnection)) throw new IOException("Not a HTTP connection"); – Salik Jul 01 '12 at 14:20
  • Don't simply catch all exceptions and re-throw them without at least printing the original exception or including it as a cause in the new thrown exception. Add `e.printStackTrace()` and post it here. – Lukas Knuth Jul 01 '12 at 14:36
  • @lukas: It gives networkonmainthread exception. – Salik Jul 01 '12 at 15:02
  • thank you for the edition lukas knuth :) – Salik Jul 01 '12 at 15:02
  • @user1451836 that does not help at all. Add the full stack-trace to your initial question so we can see what causes the exception to be thrown. – Lukas Knuth Jul 01 '12 at 15:04
  • @Lukas here :[LINK](http://stackoverflow.com/questions/8427045/how-to-solve-networkonmainthread-exception) someone suggested to change the min sdk version to 8, i had it 10. and it worked!but why is that? – Salik Jul 01 '12 at 15:08
  • @user1451836 I can't explain anything without YOUR stack-trace. Copy it from logcat and edit it to your question. – Lukas Knuth Jul 01 '12 at 15:12
  • As you just simply catch all kind of exception and throw an IOException, we cannot actually know what's happened there. Please put "e.printStackTrace();" inside ur catch block in order to know what is actually the exception thown out. – hungr Jul 26 '12 at 03:08
  • 1
    `It gives networkonmainthread exception` This means that you are doing network operations on the main thread, and that's not allowed in the later version... You should use AsyncTask or other methods to do it in the background... – Henry Hu Aug 15 '12 at 00:58
  • @HenryHu if you put that in as an answer it wouldn't appear as an unanswered question. – Warpzit Aug 15 '12 at 09:25

1 Answers1

1

It gives networkonmainthread exception

This means that you are doing network operations on the main thread, and that's not allowed in the Strict Mode.

"In the main thread" usually means that you are doing it in the event handlers, like onResume, onPause, or in the onClick method of your OnClickListener. You may look at this post (Painless threading) to understand what's the problem and why this is not allowed. Generally, if you perform network operations in the main thread, it would block the processing of GUI events, and if the network operation was slow, your application would be not responsive.

Strict Mode appeared in API level 9, so this explains that why you have problem if min SDK version is 10, and why it works with min SDK version 8.

You should use AsyncTask or other methods to do it in the background. You may refer to 1 for this.

Henry Hu
  • 524
  • 4
  • 12