Reputation: 402
I try to build my own version of this EndpointsAsyncClass on Github. Here's my implementation:
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();
}
}
However, Android Studio keep telling me I have to move my endpoint class to my app module. Beside the fact that it sounds outrageous, I'm currently following a tutorial and it doesn't mention anything about it. When I try to do it through the method provided in the tip, it claims the package doesn't even exist. And yes, it does exist and the name on the import really is it's name. What am I missing?
Upvotes: 4
Views: 1096
Reputation: 402
There was another way which helped after trying out a few things; add these dependencies for 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.+')
The tip was provided by Morad, and was originally about something else but solved this problem as well.
Upvotes: 4
Reputation: 687
Make sure in your app gradle you have the following dependency:
dependencies {
compile project(path: ':mymodule', configuration: 'android-endpoints')
}
Hope that will work for you.
Upvotes: 0