更新 Android Studio 后无法导入 Google 客户端
Cannot import Google client after updating Android Studio
更新到 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();
}
}
导入 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;
但它突出显示 'client',无法解析符号客户端。 gradle 文件有依赖编译项目(路径: ':backend', 配置: 'android-endpoints') 我已经同步了。
与其尝试回滚版本,不如我们现在需要使用不同的导入或进行不同的配置? Google 云页面上的代码现在过时了吗?
我能找到的最佳解决方案是
HttpClient is not supported any more in sdk 23. You have to use URLConnection or downgrade to sdk 22
还有
Google officially suggests to developers to use Volley library for networking related stuff here,
我在端点 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'
我不知道为什么突然需要它们,但我通过将这些依赖项添加到缺少的包中解决了这个问题。
更新到 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();
}
}
导入 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;
但它突出显示 'client',无法解析符号客户端。 gradle 文件有依赖编译项目(路径: ':backend', 配置: 'android-endpoints') 我已经同步了。
与其尝试回滚版本,不如我们现在需要使用不同的导入或进行不同的配置? Google 云页面上的代码现在过时了吗?
我能找到的最佳解决方案是
HttpClient is not supported any more in sdk 23. You have to use URLConnection or downgrade to sdk 22
还有
Google officially suggests to developers to use Volley library for networking related stuff here,
我在端点 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'
我不知道为什么突然需要它们,但我通过将这些依赖项添加到缺少的包中解决了这个问题。