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 Build a Weather App (2015) Networking Making an HTTP GET Request with OkHttp

How do you find build.gradle(Module: app) and I have another problem

Hi I am Trying to find out how to find build.gradle (Module: app)

I already Looked at others But its a bit confusing.So I would like a Simple answer Or a tip.

I also have another problem with OkHttp My Okhttpclient is Red Which means a problem.

I gone back and watched the videos But Its no use I tryed googling it But For a 10 year old..

Its confusing. so I had to come here to ask you What is the problem.

Thanks Guys

7 Answers

Hi Channon,

In Android Studio, you can order your files by different filters. there's a dropdown at top left where you can select from various options like Project, Android, Packages etc.

If you select Android, your files should look something like this:

https://www.dropbox.com/s/mllu90efrhqqzqy/Screen%20Shot%202016-02-10%20at%2013.53.35.png?dl=0

If you select Proejct, they'll look like this:

https://www.dropbox.com/s/l8m7i35hvapjms0/Screen%20Shot%202016-02-10%20at%2013.53.45.png?dl=0

In Android, you can see the gradle file you're looking for. In the Project view, it is the file inside the app folder that you want to use.

I hope that helps - if not, let me know.

Steve.

P.S. I gave up ... the links should work for you. :wink:

Thank you SOOOOOOO MUCH STEVE You are a hero Thanks..again.

I've been working and trying figure out the problems FOR DAYS! you really saved me alot of time steve :D

No problem, Channon. Glad you got it sorted. :+1:

And steve I want to ask you for one more favor My OkHttpClient is..red.

I done what keli'i done but It did not work.

So can you help? I am waiting for an answer from keli'i but Just asking you IF He doesn't respond.

P.S. Sorry If I am no clear at telling you I am only 10 years old I'm trying my best :)

Just starting Android Studio - will be right with you ...

Hi Channon,

Don't worry about asking questions - that's what Treehouse is all about - keep the questions coming, it helps us all!

Where is your error? Is it where you are using OkHttpClient? In the import list at the top of your code, do you have the line:

import com.squareup.okhttp.OkHttpClient;

That's a starting point!

Steve.

Actually Its at Mainactivity And I am not sure what line but the code looks like this :

OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(forecastUrl)
                    .build();

And OkHttpClient is red

Can you post a screenshot and paste your code too - just MainActivity.

I think this may be a versions issue. Later versions of OkHttp take a different constructor; the newer versions use different callbacks.

Let's see your code and screen, then we can see if we need to roll the version back to the one used in the course.

Steve.

Keli'i Martin
Keli'i Martin
8,227 Points

And while you're at it, could you paste your build.gradle file?

Here : http://i.imgur.com/SNoPnZW.png

Want more screenshots?

BTW My android studio version is v0.8.2

Here is my Build.gradle code keli'i :

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.0.2"

    defaultConfig {
        applicationId "teamtreehouse.com.stormy"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.2'
    compile 'com.squareup.okhttp3:okhttp:3.1.1'
    compile 'com.jakewharton:butterknife:7.0.1'
}

Those errors look like the import statements haven't landed. Try using the Code menu and selecting Optimise Imports ....

You're also running a very old version of Android Studio and your Java seems to be v7, rather than v8. Maybe try some updates?

Hey Steve I updated Android studio And now the version is 1.5. But the error is still there.

So do you know how to fix this.... Do I need to download something? cause I saw on the OkHttp GitHub Page to download something here's a screenshot :

http://i.imgur.com/vRZROjO.png?1

If not, then what's the problem? BTW I did not download anything at present.

Thanks for helping

Channon.

Hi Channon,

Your issues look as though the import statements aren't present. Let's work with that for a minute.

At the top of your MainActivity (whatever this screenshot is showing) add these lines:

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;

There may be more to add but I just want to see if this fixes a couple of points. So add those, then take another screenshot.

Thanks,

Steve.

Hey Steve Sorry It took so long to reply back But I got the screenshot right here : <blockquote class="imgur-embed-pub" lang="en" data-id="vjnYQhq"><a href="//imgur.com/vjnYQhq">Screenshot</a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>

But I saw that okhttp is red. If you want more just tell me :)

In the Gradle sync window at the bottom left - there's some issues in there that mean Gradle hasn't sync'd with the web.

Click the "Try Again" option at the top and paste in the contents of the Gradle Sync window.

One concern is that you're running Java 7, not 8. That may cause conflict; I don't really know. Let's try that next, depending what errors Gradle throws.

Steve.

Hey Steve! The console did not say anything but okhttp is still red. But lots more errors disappear.

OK, so the band across the top of the screen in [this](imgur.com/vjnYQhq) screenshot has gone, and the warning at the bottom that says "failed to resolve: com.android.support ... etc" has also gone?

But OkHttp is still red in the import statements? Is it red when you use it in the code (I'm guessing it probably is).

I'm just going to check whether your version of OkHttp can be used with Java 7 ... I'll be right back.

OK - it is compatible with Java 7. I'm running out of ideas!

How About I give you the project files and you check it.

Yep - I'm up for that. Do you want to email them, or use GitHub?

Email is good I gtg now See you tomorrow.

Cool - give me a shout when you're around tomorrow.

Hey Steve you There?

Yes! Email to email hidden

Hi do you have the email now?

Yes, I do but I cannot download bulk files as I do not have MediaFire Pro. It'll take me forever doing it one at a time.

Looking at this, your source files aren't included. Inside the app folder, I would expect to see another called src then main. Those aren't there. Which folder is your source code in?

Sorry...But what do you mean by "source code"

The Java code you've been working with containing your Activities etc.

At \Stormy2\app\src\main\java\teamtreehouse\com\stormy Then go to Mainactivity.java

I'm getting this:

pic

No folder for src, or, source.

Oh...Sorry I Am new to mediafire but I'm reuploading it now I'll send the link through email

Can you just zip your project folder up and email it to me, please? MediaFire won't let me download multiple files at once. Locate the Stormy2 folder, compress it and send it over.

Oh! - That's how you do it! Right I will do that right away

You should have the email now Steve.

Anyways See you tomorrow! :)

OK - I've got the project onto my Windows machine and opened it. I immediately get an error from Gradle (the same as the one in your screenshot) that says "Install missing platform(s) and sync project". I click that link and it downloads some archives ... not sure what; i t looked like an SDK modification. Next up, I get another Gradle error "Install Build Tools 21.0.2 and sync project", so I'll click that ... I think that version is obsolete so I'll have to fix that later. Gradle tries to sync again and fails with "Fix plugin version and sync project" - this is a Gradle plugin. I am using Gradle 2.8 that came with my download; that needs Android Gradle plugin v 1.2.0. I'll click the upate button again. It attempts to sync ... and completes that. Leaving me with a project that has red lines in the places you did. Time to fix that! Be right back ...

OK - sorted. Three changes needed, I think; all in Gradle.

Change your build tools version, then the okhttp version and the butterknife one too. If we go back a few versions, to the point the course was written, the imports work as expected.

Copy this over your build.gradle(Module: app) file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "teamtreehouse.com.stormy"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.2'
    compile 'com.squareup.okhttp:okhttp:2.3.0'
    compile 'com.jakewharton:butterknife:6.1.0'
}

You will need to let Gradle manage a few downloads and sync attempts, but this is all working fine here now, so that should fix it for you.

Let me know how you get on.

Steve.

Hi Steve I done that But My internet was slow so I tried again but my internet was broken or something But Idk so I

Downloaded it offline And did not work The console said : gradle sync failed: Could not download artifact 'okio.jar (com.squareup.okio:okio:1.3.0)': No cached version available for offline mode

And now that I'm back on the internet It still says that. Is there anyway too change from syncing offline to online?

Thanks Channon.

Oh- Nevermind...I think I fixed that and....THANKS STEVE Oh Boy! That was quite tough...

Thank you sooooooo much It worked Thank you sooo much (That's probably The last "Thank you Sooooo much")

Steve You're the best!

Channon.

Cool - glad you got going. :+1: :smile:

Keli'i Martin
Keli'i Martin
8,227 Points

So the build.gradle file for your Module:app is typically located in the app folder of your project. For example, for my Stormy project, the build.gradle file for the app is in /Stormy/app/build.gradle.

As for OkHttp, once you find your build.gradle file, make sure the dependency for OkHttp is in your build.gradle. It should be something like compile 'com.squareup.okhttp3:okhttp:3.1.1' although your version number may be different. If that is there and your OkHttpClient is still red, try hitting Alt+Enter. You may just need to import the class into your file.

I hope this helps!

Thanks Keli'i this helps too but steve is a bit more simple with the pictures too.

but thank you anyways You still helped Soo everythings fine

Oh and OkHttpClient is still re I tryed hitting Alt+enter..it worked but...What do I do it says :

  1. Create class "OkHttpClient"
  2. Create Enum 'OkHttpClient'
  3. Create inner class 'OkHttpClient'
  4. Creater interface 'OkHttpClient'

And...Find jar on web.

So what do I choose? 1 2 3 or 4?

Keli'i Martin
Keli'i Martin
8,227 Points

Yeah, it won't be any of those options. Basically, what should have come up was an option to import the OkHttpClient class. Steve Hunter lists what you should be importing at the top of your Java file. My only concern is that if there isn't an option to import the OkHttpClient class when you hit Alt-Enter, I wonder if the OkHttpClient line was added to your build.gradle file. Check and make sure the following line is in there:

compile 'com.squareup.okhttp3:okhttp:3.1.1'

Also make sure to re-sync once you've made that change. Hope this helps!

I did but the console says :

UnsupportedMethodException Failed to set up Android modules in project 'Stormy': Unsupported method: BaseArtifact.getJavaCompileTaskName(). The version of Gradle you connect to does not support that method. To resolve the problem you can change/upgrade the target version of Gradle you connect to. Alternatively, you can ignore this exception and read other information from the model.

Ok I will try But I gtg now Have a nice day! :)

It is Solved Now everyone! Thanks to Steve and Keli'i

:+1: :+1: :+1: :+1: :+1: :+1: :+1: :+1: :+1: :+1: