I am learning to work with google cloud endpoint. I am trying to connect from my device however, I keep getting an error.
failed to connect to /192.168.1.100 (port 8080) after 20000ms: isConnected failed: ECONNREFUSED (Connection refused)
How do I overcome this issue, I would like to run on a real device?
Get Jokes.class
public class GetJokes 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();
Log.i("Failed to con", result);
}
}
MainActivityFragment.class
public class MainActivityFragment extends Fragment {
public static final String JOKE = "JOKE";
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_main, container, false);
Button tellJokeButton = (Button)root.findViewById(R.id.tellJoke_button);
tellJokeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new GetJokes().execute(new Pair<Context, String>(getActivity(), "Manfred"));
}
});
AdView mAdView = (AdView) root.findViewById(R.id.adView);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device. e.g.
// "Use AdRequest.Builder.addTestDevice("ABCDEF012345") to get test ads on this device."
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
mAdView.loadAd(adRequest);
return root;
}
public void tellJoke(){
Intent intent = new Intent(getActivity(), JokeTellerActivity.class);
//TODO: get jokes from Google Cloud endpoint
intent.putExtra(JOKE,
getActivity().getString(R.string.joke)
);
startActivity(intent);
}
}
MyEndPoint.class
public class MyEndpoint {
public static List<Joke> mJokes = new ArrayList<>();
/** A simple endpoint method that takes a name and says Hi back */
@ApiMethod(name = "sayHi")
public MyBean sayHi(@Named("name") String name) {
MyBean response = new MyBean();
response.setData("Hi, " + name);
return response;
}
}
I've tried running it on several different devices and none of them would connect.