更新Android Studio后无法导入Google客户端

时间:2021-10-05 23:12:41

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

更新到Android Studio 2.3后,似乎我无法使用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

导入AndroidHttp和AndroidJsonFactory会出现问题。它曾经使用click-alt-enter导入,而不是现在。我可以手动复制导入

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.

但它突出'客户端',无法解析符号客户端。 gradle文件有依赖编译项目(路径:':backend',配置:'android-endpoints'),我已经同步。

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?

而不是尝试回滚版本,我们现在需要使用不同的导入或配置不同吗? Google Cloud页面上的代码现在已经过时了吗?

2 个解决方案

#1


12  

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

我在端点build.gradle文件中添加了这些依赖项:

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.

我不知道为什么突然需要它们,但我通过将这些依赖项添加到缺少的包来解决它。

#2


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

在sdk 23中不再支持HttpClient。您必须使用URLConnection或降级到sdk 22

also

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

谷歌正式建议开发人员在这里使用Volley库来处理网络相关的东西,

#1


12  

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

我在端点build.gradle文件中添加了这些依赖项:

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.

我不知道为什么突然需要它们,但我通过将这些依赖项添加到缺少的包来解决它。

#2


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

在sdk 23中不再支持HttpClient。您必须使用URLConnection或降级到sdk 22

also

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

谷歌正式建议开发人员在这里使用Volley库来处理网络相关的东西,