0

I'm trying to make a simple http request with Android, but the application crashes at run time. The error occurs at:

HttpResponse rp= hc.execute(post);

I thought it may be because of a missing internet permission but adding:

 <uses-permission android:name="android.permission.INTERNET" />

did not work.

This is the piece of code which I have used:

public class Http_requestActivity extends Activity {
    /** Called when the activity is first created. */

    private String getPage() {
        String str = "***";

        try
        {
            HttpClient hc = new DefaultHttpClient();
            HttpPost post = new HttpPost("http://www.yahoo.com");

            HttpResponse rp = hc.execute(post);

            if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
            {
                str = EntityUtils.toString(rp.getEntity());
            }
        } catch(IOException e) {
            e.printStackTrace();
        }  

        return str;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView txt = (TextView)findViewById(R.id.textView1);
        txt.setText(this.getPage());

    }
}

log file:

06-19 09:04:47.277: E/AndroidRuntime(2213): FATAL EXCEPTION: main
06-19 09:04:47.277: E/AndroidRuntime(2213): android.os.NetworkOnMainThreadException
06-19 09:04:47.277: E/AndroidRuntime(2213):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at com.exatech.httprequest.Http_requestActivity.getPage(Http_requestActivity.java:99)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at com.exatech.httprequest.Http_requestActivity.access$0(Http_requestActivity.java:91)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at com.exatech.httprequest.Http_requestActivity$1.onClick(Http_requestActivity.java:123)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at android.view.View.performClick(View.java:3511)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at android.view.View$PerformClick.run(View.java:14105)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at android.os.Handler.handleCallback(Handler.java:605)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at android.os.Looper.loop(Looper.java:137)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at android.app.ActivityThread.main(ActivityThread.java:4424)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at java.lang.reflect.Method.invokeNative(Native Method)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at java.lang.reflect.Method.invoke(Method.java:511)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at dalvik.system.NativeStart.main(Native Method)
Chilledrat
  • 2,593
  • 3
  • 28
  • 38
Srht
  • 473
  • 3
  • 7
  • 17
  • 2
    You should not do the networking work in UI thread.......Please use other thread or Async task for it.... it may cause android.os.NetworkOnMainThreadException – Dheeresh Singh Jun 19 '12 at 09:33
  • Just copy & paste your code into mine. It works fine. Try clean & build your project. Also make sure textView1 does exists in main.xml – RobGThai Jun 19 '12 at 09:42
  • @al0neevenings I have added logcat. – Srht Jun 19 '12 at 10:06
  • @RobGThai What should I do for cleaning? (textView1 exist in main.xml) – Srht Jun 19 '12 at 10:08
  • Forget that, Logcat stated the error. Check @srikanth answer. Kinda strange that mine worked tho. – RobGThai Jun 20 '12 at 01:27
  • I swear, this question is asked everyday here on StackOverflow. :) Read my blog post on the subject: ## [Why Ice Cream Sandwich Crashes Your App](http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html) – Alex Lockwood Jun 22 '12 at 17:02

0 Answers0