user1647329
user1647329

Reputation: 1

Executing http in android not working

try {
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet("http://www.google.com");
            client.execute(request);//it fails at this line
            Log.e("yo", "yo");
        } catch (Exception e) {}

Have anyone figured out the problem please as I am experiencing the same issue. My device is connected to the same network, pasting the URL in browser works however using HTTP doesn't.

Upvotes: 0

Views: 251

Answers (3)

mukesh
mukesh

Reputation: 4140

try HttpPost method

Declare INTERNET PERMISSION IN manfiest file

httpclient=new DefaultHttpClient();
     HttpPost httppost=new HttpPost(URL);

     HttpResponse res = null;
        try {
            res = httpclient.execute(httppost);
            System.out.println("asa "+res);

        } catch (ClientProtocolException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } 

Upvotes: 1

Infinity
Infinity

Reputation: 3875

Are you running it on the same thread as your UI? You have to use separate thread for any network connection to prevent UI Blocking. You need to use AsyncTask in this case; and obviously, android Internet permission is needed.

Upvotes: 0

Blake Beaupain
Blake Beaupain

Reputation: 638

Did you forget to add

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

to your AndroidManifest.xml?

Upvotes: 0

Related Questions