我尝试在Github上构建我自己的EndpointsAsyncClass版本。这是我的实施:
package com.example.pontuse.helloendpointsproject;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Pair;
import android.widget.Toast;
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;
import com.google.devrel.samples.mymodule.api.commentEndpoint.CommentEndpoint;
import java.io.IOException;
/**
* Created by pontuse on 2014-09-07.
*/
public class EndpointsAsyncTask extends AsyncTask<Pair<Context, String>, Void, String> {
private static CommentEndpoint 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();
}
}
然而,Android Studio一直告诉我必须将我的端点类移动到我的app模块。除了听起来令人发指的事实,我现在following a tutorial并没有提及它。当我尝试通过提示中提供的方法来实现它时,它声称该包甚至不存在。是的,它确实存在,导入的名称确实是它的名字。我错过了什么?
答案 0 :(得分:4)
在尝试了一些事情之后还有另一种方法有所帮助;为app-engine添加这些依赖项。
// Play Services will validate the application prior to allowing OAuth2 access.
compile(group: 'com.google.android.gms', name: 'play-services', version: '3.2.+')
// The following lines implement maven imports as defined at:
// https://code.google.com/p/google-api-java-client/wiki/Setup
// Add the Google API client library.
compile(group: 'com.google.api-client', name: 'google-api-client', version: '1.17.0-rc') {
// Exclude artifacts that the Android SDK/Runtime provides.
exclude(group: 'xpp3', module: 'xpp3')
exclude(group: 'org.apache.httpcomponents', module: 'httpclient')
exclude(group: 'junit', module: 'junit')
exclude(group: 'com.google.android', module: 'android')
}
// Add the Android extensions for the Google API client library.
// This will automatically include play services as long as you have download that library
// from the Android SDK manager.
// Add the Android extensions for the Google API client library.
compile(group: 'com.google.api-client', name: 'google-api-client-android',
version: '1.17.0-rc')
{
// Exclude play services, since we're not using this yet.
exclude(group: 'com.google.android.google-play-services', module: 'google-play-services')
}
// END Google APIs
// The following client libraries make HTTP/JSON on Android easier.
// Android extensions for Google HTTP Client.
compile(group: 'com.google.http-client', name: 'google-http-client-android',
version: '1.17.0-rc') {
exclude(group: 'com.google.android', module: 'android')
}
// This is used by the Google HTTP client library.
compile(group: 'com.google.guava', name: 'guava', version: '14.0.+')
提示是provided by Morad,最初是关于别的东西,但也解决了这个问题。
答案 1 :(得分:0)
确保在您的应用程序gradle中您有以下依赖项:
dependencies {
compile project(path: ':mymodule', configuration: 'android-endpoints')
}
希望这对你有用。