I'm trying to connect to google endpoint in android app but when I run the app on emulator or real device both are not work and I get error 404 not found. so I create button that implement the asynctask of connection :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void tellJoke(View view) {
EndpointsAsyncTask asyncTask = new EndpointsAsyncTask(this);
asyncTask.execute();
}
}
and this is the class of asynctask:
public class EndpointsAsyncTask extends AsyncTask<Void, Void, String> {
private static MyApi myApiService = null;
private Context mContext;
public EndpointsAsyncTask (Context context){
mContext = context;
}
@Override
protected String doInBackground(Void... 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();
}
try {
return myApiService.jokes().execute().getData();
} catch (IOException e) {
return e.getMessage();
}
}
@Override
protected void onPostExecute(String result) {
Intent intent = new Intent(mContext, AndroidLibraryActivity.class);
intent.putExtra("jokes", result);
mContext.startActivity(intent);
}
}
and in android library I received the result like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_library);
TextView jokes;
jokes = findViewById(R.id.jokes);
Intent intent = getIntent();
String joke = intent.getStringExtra("jokes");
jokes.setText(joke);
}
and this is my endpoint:
@Api(
name = "myApi",
version = "v1",
namespace = @ApiNamespace(
ownerDomain = "backend.builditbigger.gradle.udacity.com",
ownerName = "backend.builditbigger.gradle.udacity.com",
packagePath = ""
)
)
public class MyEndpoint {
/** A simple endpoint method that takes a name and says Hi back */
@ApiMethod(name = "jokes")
public MyBean jokes(){
MyBean response = new MyBean();
Joker jokes = new Joker();
String jokesResult = jokes.getJoke();
response.setData(jokesResult);
return response;
}
also I read many solutions but nothing work with me and by the way when I run it on real device I change the rooturl to this link:
but it doesn't work so how make it work at least on the simulator devices!