Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Missing internet permission though I gave the internet permission?

I recently completed Build a weather app course and then I thought that I will build the stormy again without seeing the video.But I got an error called Missing internet permission but a gave the permission and my emulator not in a airplane mode My error is

error.java
 E/AndroidRuntime FATAL EXCEPTION: OkHttp Dispatcher
    Process: com.example.niyamat.stoormyy, PID: 2722
    java.lang.SecurityException: Permission denied (missing INTERNET permission?)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:451)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
            at java.net.InetAddress.getAllByName(InetAddress.java:215)
            at com.squareup.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
            at com.squareup.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187)
            at com.squareup.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156)
            at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98)
            at com.squareup.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:344)
            at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:327)
            at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:245)
            at com.squareup.okhttp.Call.getResponse(Call.java:267)
            at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)
            at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)
            at com.squareup.okhttp.Call.access$100(Call.java:34)
            at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:162)
            at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
            at libcore.io.Posix.android_getaddrinfo(Native Method)
            at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:438)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
            at java.net.InetAddress.getAllByName(InetAddress.java:215)
            at com.squareup.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
            at com.squareup.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187)
            at com.squareup.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156)
            at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98)
            at com.squareup.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:344)
            at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:327)
            at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:245)
            at com.squareup.okhttp.Call.getResponse(Call.java:267)
            at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)
            at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)
            at com.squareup.okhttp.Call.access$100(Call.java:34)
            at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:162)
            at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)

My manifest file is

manifest.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.niyamat.stoormyy" >
    <uses-permission android:name="ANDROID.PERMISSION.INTERNET"/>
    <uses-permission android:name="ANDROID.PERMISSION.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

And my main Activity is

MainActivity.java
package com.example.niyamat.stoormyy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        double latitude = 37.8267;
        double longitude = -122.423;
        String apiKey = "4782d6d6e5aee3f63ad0f4191c2e9f15";
        String forecastUrl = "https://api.forecast.io/forecast/" + apiKey + "/" + latitude + "," + longitude;

        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(forecastUrl).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                try {
                    String jsonData = response.body().string();
                    Log.v(TAG, jsonData);
                } catch (Exception e) {
                    Log.e(TAG, "Exception caught", e);
                }
            }
        });

    }


}

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Niyamat;

If memory serves, I think the permissions are:

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

Notice the difference in case. Try that and see if it helps. Post back if you are still stuck.

Happy coding,
Ken

Hi Ken Alger

Thank you Its work.

But one thing I don't understand that when first time I set the permission the android studio auto completed

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

that line but second time when I set the permission the android studio auto completed

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

that line.So I don't understand why that happen.