Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: HTTPClient

I was trying http-cleint tutorials from svn.apache.org. While running the application I am getting the following error in console.

[2010-04-30 09:26:36 - HalloAndroid] ActivityManager: java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.org.example/.HalloAndroid } from null (pid=-1, uid=-1) requires android.permission.INTERNET

I have added android.permission.INTERNET in AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.org.example"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".HalloAndroid"
                  android:label="@string/app_name" android:permission="android.permission.INTERNET">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

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

</manifest>

The java code in HalloAndroid.java is as follows

 HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget2 = new HttpGet("http://google.com/");
        HttpResponse response2 = null;
        try {
            response2 = httpclient.execute(httpget2);
        } catch (ClientProtocolException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    HttpEntity entity = response2.getEntity();
    if (entity != null) {
        long len = entity.getContentLength();
        if (len != -1 && len < 2048) {
            try {
                    Log.d(TAG, EntityUtils.toString(entity));
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        } else {
            // Stream content out
        }

Any help is much appreciated.

like image 330
Primal Pappachan Avatar asked Apr 30 '10 04:04

Primal Pappachan


People also ask

What is HttpClient Android?

HttpClient is used when you want to receive and send data from the server over the internet. So for this you need to create a http client using HttpClient class. First, you will create the object of Http client and the URL to the constructor of HttpPost class that post the data.

What is Android async HTTP?

Overview. A popular third-party library called android-async-http helps handle the entire process of sending and parsing network requests for us in a more robust and easy-to-use way.

What is HttpClient used for?

An HttpClient can be used to send requests and retrieve their responses. An HttpClient is created through a builder . The builder can be used to configure per-client state, like: the preferred protocol version ( HTTP/1.1 or HTTP/2 ), whether to follow redirects, a proxy, an authenticator, etc.


1 Answers

Problem solved. This line in the AndroidManifest.xml file was causing the trouble.

android:permission="android.permission.INTERNET"
like image 143
Primal Pappachan Avatar answered Oct 01 '22 05:10

Primal Pappachan