VK SDK for android problems - java

I'm using Android Studio. I've downloaded VK SDK from github and added it in my project file.Also I import it
import com.perm.kate.api.Api;
I authorize, then get my friends with it
Api api = new Api(Account.access_token, Constants.API_ID);
users = api.getFriends(Account.user_id, "photo", null, null, null);
Insert them in DB
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
SharedPreferences.Editor editor = prefs.edit();
for(int i =0; i< users.size();++i){
editor.putString("FriendFirstName" + String.valueOf(i), users.get(i).first_name);
editor.putString("FriendLastName"+ String.valueOf(i), users.get(i).last_name);
editor.putString("FriendPhoto"+ String.valueOf(i), users.get(i).photo);
}
editor.commit();
After, I can get any of my friend's name and it's working fine:
Names.add(prefs.getString("FriendFirstName" + String.valueOf(i), null)
+" "+prefs.getString("FriendLastName" + String.valueOf(i), null) );
BUT whenever I try get the photo or photo_medium_rec or any kind of photo it gives me null
Toast.makeText(getActivity(),String.valueOf(prefs.getString("FriendPhoto0", null))
,Toast.LENGTH_LONG).show();//NULL
Why is it giving null?
Addition
I'm new in programming, so here are some of my dumb questions about VK SDK. What should I use Api or Request methods like this:
VKRequest request = VKApi.uploadWallPhotoRequest(new VKUploadImage(photo, VKImageParameters.jpgImage(0.9f)), 0, 0);
Is there any difference between them? How to use Request? And how get my personal user information? I couldn't fined any methods in Api to get it.

Try to use official VK SDK (link).
And maybe it will be helpfull (link)

Related

How to build a short Dynamic Link with a Desktop Fallback URL on Android?

I have implemented deep link in my Android App to share content. The problem is on Android I can't find a way to set a Fallback URL when the user open the short link on his desktop.
With the Firebase DynamicLink.Builder I can set iOS fallback URL because my app doesn't exist on iOS but I can't find a way to set the dfl parameters in my link.
Which lead the user to an error page like this :
Here how I build my short dynamic link:
//link example : https://app.example.com/details/ebLvAV9fi9S7Pab0qR3a
String link = domainUri + "/details/" + object.getUid();
FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLink(Uri.parse(link))
.setDomainUriPrefix(domainUri)
.setAndroidParameters(new DynamicLink.AndroidParameters.Builder().setMinimumVersion(1).build())
// Fallback Url for iOS
.setIosParameters(new DynamicLink.IosParameters.Builder("").setFallbackUrl(Uri.parse(RMP_WEB_BASE_URL)).build())
.setSocialMetaTagParameters(
new DynamicLink.SocialMetaTagParameters.Builder()
.setTitle(title)
.setDescription(description)
.setImageUrl(Uri.parse(imageUrl))
.build())
.buildShortDynamicLink()
.addOnCompleteListener(new OnCompleteListener<ShortDynamicLink>() {
#Override
public void onComplete(#NonNull Task<ShortDynamicLink> task) {
if (task.isSuccessful() && task.getResult() != null) {
shortLink = task.getResult().getShortLink();
//Create Shareable Intent
//...
}
}
});
I have read that I need to specify a Desktop Fallback URL like the iOS one but DynamicLink.Builder doesn't seems to include one.
I would like to redirect my user to the home page https://example.com when they open the link from a non-android device.
I have tried to use setLongLink(longLink) in the DynamicLink.Builder with the parameters ?dfl=https://example.com but it doesn't seems to work and it even break my dynamic link on android.
This is a Swift solution but may be helpful to others-
Unfortunately, there is currently no built-in method to handle this programmatically through the Firebase url editor. You must manually add an 'ofl' parameter to the link. The easiest way to do this:
// Grab link from Firebase builder
guard var longDynamicLink = shareLink.url else { return }
// Parse URL to string
var urlStr = longDynamicLink.absoluteString
// Append the ofl fallback (ofl param specifies a device other than ios or android)
urlStr = urlStr + "&ofl=https://www.google.com/"
// Convert back to a URL
var urlFinal = URL(string: urlStr)!
// Shorten the url & check for errors
DynamicLinkComponents.shortenURL(urlFinal, options: nil, completion:{ [weak self] url,warnings,error in
if let _ = error{
return
}
if let warnings = warnings{
for warning in warnings{
print("Shorten URL warnings: ", warning)
}
}
guard let shortUrl = url else {return}
// prompt the user with UIActivityViewController
self?.showShareSheet(url: shortUrl)
})
The final URL can then be used to present the shareable panel with another function like:
self.showShareSheet(url: finalUrl) which triggers the UIActivityViewController
Credit to http://ostack.cn/?qa=168161/ for the original idea
More about ofl: https://firebase.google.com/docs/dynamic-links/create-manually?authuser=3#general-params

Cannot get user details from post in restfb using java

Recently I've been struggling with getting user information from post_id with java. I'm new in restfb, but after reasearching, below code should work. All available permissions are granted. Even in Graph API Explorer when writing post_id I cannot retrieve post's author details.
This is how I do it:
FacebookClient facebookClient = new DefaultFacebookClient(token);
String command = "orangepolska/feed";
Connection<Post> pagePosts = facebookClient.fetchConnection(command, Post.class);
ArrayList<String> postList = new ArrayList<String>();
String row;
for( List<Post> posts : pagePosts){
for (Post post : posts) {
if (post.getCreatedTime().after(startDate) && post.getCreatedTime().before(endDate)){
String message = post.getMessage();
CategorizedFacebookType postedBy = post.getFrom();
Post.Comments comments = post.getComments();
row = " owner: "+postedBy.getName()+" owner_id: "+postedBy.getId()+" post: "+message+" + " likes: "+post.getLikesCount() + "\n";
System.out.println(row);
postList.add(row);
}
}
}
return postList;
The problem occurs with various of functions like: getName(), getID(), getLikesCount() etc - these return null.
How can i fix it?
Thanks in advance.
You need to fetch the feed with the fields parameter so Facebook knows which fields you need to be filled. RestFB can only provide access to information that are given by Facebook ;)
Have a look here: http://restfb.com/#selecting-specific-fields
Norbert is correct but the link he posted did not work for me - you need to include "Parameter.with("fields", "from")" to get the user information.
Connection<Post> pagePosts = facebookClient.fetchConnection(command, Post.class, Parameter.with("fields", "from"));

Compare Google Maps Marker id with id in SQLite database

I'm trying to compare the value of Markers with an id in my pre-made SQLite database. The reason being is so that I can have specific details for specific markers.
For example:
If the marker clicked has id = 1 then I wish to search the database for id '1' and then grab the details from that row. I thought it was simple enough to just loop through the database, but this doesn't seem to be working. My current code is:
final Cursor dbId = monDatabase.database.rawQuery("SELECT _id from monuments", null);
int idColumnCount = dbId.getColumnCount();
dbId.moveToFirst();
while(dbId.isAfterLast() == false) {
for(int f = 0; f < idColumnCount; f++) {
mainMarkerId = dbId.getInt(0);
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
if(marker.getId().equals("m" + mainMarkerId)){
Log.v("marker.getId()", "the id is: " + marker.getId());
selectDesc = monDatabase.database.rawQuery("SELECT description from monuments WHERE _id = " + mainMarkerId, null);
selectDesc.moveToFirst();
_description = selectDesc.getString(0);
Intent descriptionIntent = new Intent(MainActivity.this, DisplayData.class);
descriptionIntent.putExtra(EXTRA_MESSAGE, _description);
startActivity(descriptionIntent);
markerId.moveToNext();
}
}
});
dbId.moveToNext();
}
}
I'm unsure if it is possible this way, or maybe I have to use a Hashmap or something.
Any help is much appreciated,
Thanks!
EDIT:
I forgot to mention that I have all the Markers already displayed on the maps, so it's more of a case of being able to get the description for that location and then passing it through to a new Intent.
SOLUTION:
Just thought I'd post the solution on here if anyone else needed it. I didn't need a Hashtable, all I used was:
selectDesc = monDatabase.database.rawQuery("SELECT description from monuments WHERE title = '" + marker.getTitle() + "'", null);
No need for all the for loops or anything. Very very simple!
your best bet is to create a HashMap of the database elements using the database id as the key and the marker as the value so then you can pull from your database and have what marker you need

Android Java Text to Speech Viewing Extra String Information

I've been running through many of the text to speech examples available for Android and I have an issue that I assume is really simple, but I cannot for the life of me work it out!
I simply want to be able to view the output of EXTRA_AVAILABLE_VOICES (for example) which according to this link is returned in an ArrayList. There are many examples of how to deal with such output programmatically, but for the benefit of my learning and understanding, I want to see the actual returned data for myself.
My project is set up exactly as the android developers example from here
// We now return the list of available and unavailable voices
// as well as the return code.
Intent returnData = new Intent();
returnData.putStringArrayListExtra(
TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES, available);
returnData.putStringArrayListExtra(
TextToSpeech.Engine.EXTRA_UNAVAILABLE_VOICES, unavailable);
setResult(result, returnData);
finish();
}
Ideally I'd like to have the output displayed after the 'constant value' in a simple TextView from a string, but I can't achieve that, neither can I get it in a ListView despite my many efforts... Please can someone help me solve this!
Once I know how to view the returned data, I can then go on to follow the examples of how to deal with it correctly.
I've not included any the code I've already tried, as I can't find an example anywhere and it's been pure guess work (which I be embarrassed to show!)
Thanks in advance.
For anyone who is ever stuck with the same thing, I used the code below, edited from the sample found here:
ArrayList<String> available = data
.getStringArrayListExtra("availableVoices");
Log.v("languages count", String.valueOf(available.size()));
Iterator<String> iter = available.iterator();
while (iter.hasNext()) {
String lang = iter.next();
Locale locale = new Locale(lang);
Log.v(TAG, "language: " + lang);
Log.v(TAG, "language locale: " + locale.toString());
TextView LocaleResults = (TextView) getView().findViewById(
R.id.textViewConfig);
LocaleResults.append("\nAvailable Engine Language: " + lang);
}
ArrayList<String> unavailable = data
.getStringArrayListExtra("unavailableVoices");
Log.v("languages count", String.valueOf(unavailable.size()));
Iterator<String> iteru = unavailable.iterator();
while (iteru.hasNext()) {
String ulang = iteru.next();
Locale ulocale = new Locale(ulang);
Log.v(TAG, "ulanguage: " + ulang);
Log.v(TAG, "ulanguage locale: " + ulocale.toString());
TextView LocaleResults = (TextView) getView().findViewById(
R.id.textViewConfig);
LocaleResults.append("\nUnavailable Engine Language: " + ulang);
}

Android shared preference not working

I am developing an Android app where I am trying to save some values using the sharedPreference concept. I am just trying the basic things for saving something into the SharedPreference. But its not working. Here is what am trying to do:
SharedPreferences preferences = getSharedPreferences(SHARED_PREF_NAME,0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SHARED_PREF_USER_NAME, "username");
editor.putString(SHARED_PREF_PASSWORD, "password");
editor.commit();
System.out.println("SHARED_PREF_USER_NAME ::" +
preferences.getString(SHARED_PREF_USER_NAME, ""));
System.out.println("SHARED_PREF_PASSWORD ::" +
preferences.getString(SHARED_PREF_PASSWORD, ""));
Just below the code, I am trying to print the values that were saved into the SharedPreference. But am getting the empty string.
Your code seams to be ok, I ran it on a test project and got the correct output. Maybe it's about your preference strings? Do they contain spaces?
Means if String SHARED_PREF_USER_NAME = "user name" then Change it to "username" and Try.
You can use this code.
SharedPreferences preferences = context.getSharedPreferences(SHARED_PREF_NAME,0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("username",SHARED_PREF_USER_NAME);
editor.putString("password",SHARED_PREF_PASSWORD);
editor.commit();
System.out.println("SHARED_PREF_USER_NAME ::" +
preferences.getString(SHARED_PREF_USER_NAME, ""));
System.out.println("SHARED_PREF_PASSWORD ::" +
preferences.getString(SHARED_PREF_PASSWORD, ""));

Categories