Cannot import Google client after updating Android Studio
Asked Answered
C

2

7

After updating to Android Studio 2.3 it appears I can not use the code for the Cloud Endpoints

class EndpointsAsyncTask extends AsyncTask<Pair<Context, String>, Void, String> {
    private static MyApi myApiService = null;
    private Context context;

    @Override
    protected String doInBackground(Pair<Context, String>... params) {
        if(myApiService == null) {  // Only do this once
            MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
                    new AndroidJsonFactory(), null)
                // options for running against local devappserver
                // - 10.0.2.2 is localhost's IP address in Android emulator
                // - turn off compression when running against local devappserver
                .setRootUrl("http://10.0.2.2:8080/_ah/api/")
                .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                    @Override
                    public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
                        abstractGoogleClientRequest.setDisableGZipContent(true);
                    }
                });
                // end options for devappserver

            myApiService = builder.build();
        }

        context = params[0].first;
        String name = params[0].second;

        try {
            return myApiService.sayHi(name).execute().getData();
        } catch (IOException e) {
            return e.getMessage();
        }
    }

    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(context, result, Toast.LENGTH_LONG).show();
    }
}

The problem arises with importing AndroidHttp and AndroidJsonFactory. It used to work with the click-alt-enter import, not now. I can copy the import manually

import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.extensions.android.json.AndroidJsonFactory;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;

but it highlights 'client', cannot resolve symbol client. The gradle file has the dependency compile project(path: ':backend', configuration: 'android-endpoints') and I have synched.

Rather than trying to rollback versions, is there a different import we need to use now or configure differently? Is the code on the Google Cloud page now outdated?

Copula answered 28/3, 2017 at 15:1 Comment(1)
Do the following answer solve your problem?Hanseatic
B
13

I added these dependencies in the endpoints build.gradle file:

compile group: 'com.google.api-client', name: 'google-api-client', version: '1.22.0'
compile group: 'com.google.api-client', name: 'google-api-client-android', version: '1.22.0'

I don't know why they suddenly are needed, but I solved it by adding these dependencies to the missing packages.

Bantustan answered 26/4, 2017 at 8:12 Comment(1)
This fixes the problem. can anyone elaborate on why we need to do this?Tlaxcala
N
0

Best solution I can find to this is here where is says

HttpClient is not supported any more in sdk 23. You have to use URLConnection or downgrade to sdk 22

also

Google officially suggests to developers to use Volley library for networking related stuff here,

Normi answered 11/4, 2017 at 10:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.