I have a problem with google map api. I used Api Key in my project and also i linked release SHA1 Key. It is working correctly in debug apk, but in release apk place autocompleteview is close automatically. I checked the Logcat, in logcat the api key is different from the key i used in meta-data in android manifest file.
private void manualLocation() {
try {
Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN).build((Activity) context);
startActivityForResult(intent, 1);
} catch (GooglePlayServicesRepairableException e) {
// TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
Place place = PlaceAutocomplete.getPlace(this, data);
Log.e("Tag", "Place: " + place.getAddress() + place.getPhoneNumber());
Log.e("Tag", "Place: " + place.getLatLng() + place.getPlaceTypes());
if (signup.matches("1")) {
latitude = String.valueOf(place.getLatLng().latitude);
longitude = String.valueOf(place.getLatLng().longitude);
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
Status status = PlaceAutocomplete.getStatus(this, data);
// TODO: Handle the error.
Log.e("Tag", status.getStatusMessage());
locationStatus = "0";
AppPreferences.savePreferences(Location.this, "locationStatus", locationStatus);
} else if (resultCode == RESULT_CANCELED) {
locationStatus = "0";
AppPreferences.savePreferences(Location.this, "locationStatus", locationStatus);
// The user canceled the operation.
}
}
}
}
I used the key in meta-data "AIzaSyBH9saN7RRHev1QNKWqtIjejojvqJuCswU" but in Logcat the key is different. i show you the Logcat. Can anyone help me?
BasicNetwork.performRequest: Unexpected response code 403 for https://www.googleapis.com/placesandroid/v1/autocompleteWidget?key=AIzaSyBb2MNLJEmWt-FLJqN28D_-WuxQiMwzUhU
this is the Logcat response
Related
I want to get nonce from server. i wrote backend in PHP that will giving token. after receiving token PaymentMethodNonce returning null.
I want to get nonce from server.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
DropInResult result = data.getParcelableExtra(DropInResult.EXTRA_DROP_IN_RESULT);
PaymentMethodNonce nonce = result.getPaymentMethodNonce();
String stringNonce = nonce.getNonce();
Log.d("mylog", "Result: " + stringNonce);
// Send payment price with the nonce
// use the result to update your UI and send the payment method nonce to your server
paramHash = new HashMap<>();
paramHash.put("couponPrice", couponPrice);
paramHash.put("nonce", stringNonce);
sendPaymentDetails();
} else if (resultCode == Activity.RESULT_CANCELED) {
// the user canceled
Log.d("mylog", "user canceled");
} else {
// handle errors here, an exception may be available in
Exception error = (Exception) data.getSerializableExtra(DropInActivity.EXTRA_ERROR);
Log.d("mylog", "Error : " + error.toString());
}
}
}
DropInResultobject is result. That is returning null but that should be some valid information
Here's the answer:
val result = intent.getParcelableExtra<DropInResult>(DropInResult.EXTRA_DROP_IN_RESULT)
return null
We are using Google PlaceAutocomplete for city picker. We need to get country code for picked city. I am trying to use place.getLocale() but its null. Is there a way I can get Country ISO code from PlaceAutocomplete returned data.
in gradle:
compile 'com.google.android.gms:play-services-places:10.0.1'
code:
private void openCityPicker() {
try {
AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
.setTypeFilter(AutocompleteFilter.TYPE_FILTER_CITIES)
.build();
Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
.setFilter(typeFilter)
.build(this);
startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
} catch (GooglePlayServicesRepairableException e) {
// TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
com.google.android.gms.location.places.Place googleApiPlace = PlaceAutocomplete.getPlace(this, data);
Log.d(TAG, "onActivityResult: " + googleApiPlace.getAddress());
Log.d(TAG, " googleApiPlace.getLocale().getCountry(): " + googleApiPlace.getLocale().getCountry());
Log.d(TAG, " googleApiPlace.getLocale().getDisplayCountry(): " + googleApiPlace.getLocale().getDisplayCountry());
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
Status status = PlaceAutocomplete.getStatus(this, data);
// TODO: Handle the error.
Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
}
}
late but right answer
you can get country code using this
add Place.Field.ADDRESS_COMPONENTS field into your fields list
List<Place.Field> fields = Arrays.asList(Place.Field.ADDRESS_COMPONENTS);
Intent intent = new Autocomplete.IntentBuilder(
AutocompleteActivityMode.FULLSCREEN, fields)
.setTypeFilter(TypeFilter.CITIES)
.build(mActivity);
startActivityForResult(intent, requestCode);
when you get the result into onActivityResult() you'll get full details of location into that you will find country
if (place.getAddressComponents() != null) {
List<AddressComponent> addressComponents = place.getAddressComponents().asList();
for (int i = 0; i < addressComponents.size(); i++) {
if (addressComponents.get(i).getTypes().get(0).equals("country")) {
countryCode = addressComponents.get(i).getShortName();
break;
}
}
}
I'm designing an app that uses maps and requires users to input destinations.i added the PlaceAutoCompleteFragment in the xml
fragment
android:id="#+id/place_autocomplete_fragment"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="top" android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
And this is what is in my java
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
#Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
Log.i(TAG, "Place: " + place.getName());
}
#Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i(TAG, "An error occurred: " + status);
}
});
When I try searching it says:"Can't load search results".What should I do after this?
The autocomplete widget is a search dialog with built-in autocomplete functionality.
Use PlaceAutocomplete.IntentBuilder to create an intent to launch the autocomplete widget as an intent. After setting the optional parameters, call build(Activity) and pass the intent to startActivityForResult(android.content.Intent, int).
int PLACE_AUTOCOMPLETE_REQUEST_CODE = 1;
...
try {
Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN).build(this);
startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
} catch (GooglePlayServicesRepairableException e) {
// TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}
To receive notification when a user has selected a place, your app should override the activity's onActivityResult(), checking for the request code you have passed for your intent, as shown in the following example.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = PlaceAutocomplete.getPlace(this, data);
Log.i(TAG, "Place: " + place.getName());
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
Status status = PlaceAutocomplete.getStatus(this, data);
// TODO: Handle the error.
Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
}
}
Maybe I overlooked something, but through Play Games Services documentation https://developers.google.com/games/services/android/quickstart,I did not yet get any idea about how to implement backend server authentication like how to get a token from Google's server and pass it to my own backend server to verify a login. I wish someone can give me a clue. Thanks !
Step 1:
Create a button with standard google Login button
Step2 : Add a buttonclick listener
Step 3 : In listner check for google play services availablity
public void googleLoginClicked(View v){
if (checkPlayServices()) {
pickUserAccount();
}else{
showToast("Google Play Services is not installed or updated in your deivce", Toast.LENGTH_LONG);
}
}
protected boolean checkPlayServices() {
int resultCode =
GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
// Log.i("GCM", "This device is not supported.");
finish();
}
return false;
}
return true;
}
Step 4:
search for google accounts in phone:
private void pickUserAccount() {
String[] accountTypes = new String[]{"com.google"};
Intent intent = AccountPicker.newChooseAccountIntent(null, null,
accountTypes, false, null, null, null, null);
startActivityForResult(intent, REQUEST_CODE_PICK_ACCOUNT);
}
Step5:
onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_PICK_ACCOUNT) {
if (resultCode == RESULT_OK) {
mEmail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
getUsername(null);
} else if (resultCode == RESULT_CANCELED) {
showToast("You must pick an account",
Toast.LENGTH_SHORT);
}
} else if (requestCode == REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR && resultCode == RESULT_OK) {
Bundle extra = data.getExtras();
String oneTimeToken = extra.getString("authtoken");
getUsername(oneTimeToken);
}
}
Step6: ON success get username from google using a background task
AsyncTask:
doinbackground(){
...
fetchToken
...
}
protected String fetchToken() throws IOException {
try {
return GoogleAuthUtil.getToken(mActivity, mEmail, mScope);
} catch (UserRecoverableAuthException userRecoverableException) {
// GooglePlayServices.apk is either old, disabled, or not present, which is
// recoverable, so we need to show the user some UI through the activity.
mActivity.handleException(userRecoverableException);
} catch (GoogleAuthException fatalException) {
onError("Unrecoverable error " + fatalException.getMessage(), fatalException);
}
return null;
}
Here is my code taken from [https://developers.google.com/google-apps/calendar/quickstart/android][1]
to get google calendar events its working for my email which is used to get permission from google developer console but unable to get other emails data
private List<String> getDataFromApi() {
// List the next 10 events from the primary calendar.
DateTime now = new DateTime(System.currentTimeMillis());
List<String> eventStrings = new ArrayList<String>();
Events events = null;
try {
events = mService.events().list("primary")
.setMaxResults(30)
.setTimeMin(now)
.setOrderBy("startTime")
.setSingleEvents(true)
.execute();
} catch (IOException e) {
e.printStackTrace();
Log.d("Meeting Planner :", "in get data from api " + e.getMessage());
Log.d("Meeting Planner :", "in get data from api " + e.getLocalizedMessage());
Log.d("Meeting Planner :", "in get data from api " + e.toString());
//return null;
}
List<Event> items = events.getItems();
eventStrings.add(
String.format("%s (%s)", event.getSummary(), start));
}
return eventStrings;
}
As according to your refernce. Looks like user havn't been assigned a token
then you should use this code in your onCanceled ()
if (mLastError instanceof UserRecoverableAuthIOException) {
startActivityForResult(
((UserRecoverableAuthIOException) mLastError).getIntent(),
MainActivity.REQUEST_AUTHORIZATION);
} else {
mOutputText.setText("The following error occurred:\n"
+ mLastError.getMessage());
And should include the below method for activity result in your activity
protected void onActivityResult(
int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case REQUEST_AUTHORIZATION:
if (resultCode != RESULT_OK) {
chooseAccount();
}
else {
//your code to call make request again
new MakeRequestTask(mCredential).execute();
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}