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

Gregory Shavers Jr
Gregory Shavers Jr
2,268 Points

Error: Cannot find symbol class OkHttpClient

Hello Everyone,

I'm working on creating my weather app. I'm having issues with the build gradle(Module: app) For some reason I think it may not be syncing.

*******This is what's synced for the build gradle(Module:app)*******

apply plugin: 'com.android.application' android { compileSdkVersion 21 buildToolsVersion "21.1.2"

defaultConfig {
    applicationId "com.gbsjr.stromy"
    minSdkVersion 16
    targetSdkVersion 21
    versionCode 1
    versionName "1.0"
}
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.3' compile 'com.squareup.okhttp:okhttp:2.6.0' }

*******This is my code in the MainActivity Java*******

package com.gbsjr.stromy; import android.app.DownloadManager; import android.os.Bundle; import android.support.v7.app.ActionBarActivity;

public class MainActivity extends ActionBarActivity {

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



    String apiKey = "2f2875ae75e8063b0bb0ce8de7295a07";
    double latitude = 37.8267;
    double longitude = -122.423;
    String forcastUrl =  "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);
    //Response response = call.execute();

}

}

Here is my error message. Error:(27, 9) error: cannot find symbol class OkHttpClient

Is there anything I can do resolve this issue? I've already tried the Invalidated/Cache Restart... and even resync the gradle, however this error message persist. Any help would be much appreciated?

1 Answer

I don't think you have imported the library yet Try import com.squareup.okhttp.*; to import the entire okhttp library In my code I have

Gregory Shavers Jr
Gregory Shavers Jr
2,268 Points

I see allot people having this problem. I don't know why, but once the Gradle is sync, you have to remove any code relating to this library. The IDE doesn't fix the object, which I find weird. My suggestion is once the Gradle is synced, run the Invalidated/Cache Restart, after the IDE restart itself, make sure you create a new OkHttpClient object, you will then see the library. I hope this helps someone.