Android Open Source - vk-android-sdk Test Activity






From Project

Back to project page vk-android-sdk.

License

The source code is released under:

MIT License

If you think the Android project vk-android-sdk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.vk.vktestapp;
/*from w  w  w. j av  a 2  s  .c o  m*/
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.vk.sdk.VKUIHelper;
import com.vk.sdk.api.VKApi;
import com.vk.sdk.api.VKApiConst;
import com.vk.sdk.api.VKBatchRequest;
import com.vk.sdk.api.VKBatchRequest.VKBatchRequestListener;
import com.vk.sdk.api.VKError;
import com.vk.sdk.api.VKParameters;
import com.vk.sdk.api.VKRequest;
import com.vk.sdk.api.VKRequest.VKRequestListener;
import com.vk.sdk.api.VKResponse;
import com.vk.sdk.api.methods.VKApiCaptcha;
import com.vk.sdk.api.model.VKApiPhoto;
import com.vk.sdk.api.model.VKAttachments;
import com.vk.sdk.api.model.VKPhotoArray;
import com.vk.sdk.api.model.VKUsersArray;
import com.vk.sdk.api.model.VKWallPostResult;
import com.vk.sdk.api.photo.VKImageParameters;
import com.vk.sdk.api.photo.VKUploadImage;
import com.vk.sdk.dialogs.VKShareDialog;

import java.io.IOException;

public class TestActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        if (getSupportActionBar() != null) getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();

        if (itemId == android.R.id.home) {
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        VKUIHelper.onResume(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        VKUIHelper.onDestroy(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        VKUIHelper.onActivityResult(this, requestCode, resultCode, data);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_test, container, false);
        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            view.findViewById(R.id.users_get).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    VKRequest request = VKApi.users().get(VKParameters.from(VKApiConst.FIELDS,
                            "id,first_name,last_name,sex,bdate,city,country,photo_50,photo_100," +
                            "photo_200_orig,photo_200,photo_400_orig,photo_max,photo_max_orig,online," +
                            "online_mobile,lists,domain,has_mobile,contacts,connections,site,education," +
                            "universities,schools,can_post,can_see_all_posts,can_see_audio,can_write_private_message," +
                            "status,last_seen,common_count,relation,relatives,counters"));
//                    VKRequest request = VKApi.users().get(VKParameters.from(VKApiConst.USER_IDS, "1,2"));
                    request.secure = false;
                    request.useSystemLanguage = false;
                    startApiCall(request);

                    VKRequest get = new VKRequest("users.get", VKParameters.from(VKApiConst.USER_IDS, "1,2,44", VKApiConst.FIELDS, "photo_50,sex"), VKRequest.HttpMethod.GET, VKUsersArray.class);
                    get.executeWithListener(new VKRequestListener() {
                        @Override
                        public void onComplete(VKResponse response) {
                            super.onComplete(response);
                        }

                        @Override
                        public void onError(VKError error) {
                            super.onError(error);
                        }
                    });
                }
            });

            view.findViewById(R.id.friends_get).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    VKRequest request = VKApi.friends().get(VKParameters.from(VKApiConst.FIELDS, "id,first_name,last_name,sex,bdate,city"));
                    startApiCall(request);
                }
            });

            view.findViewById(R.id.captcha_force).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    VKRequest request = new VKApiCaptcha().force();
                    startApiCall(request);
                }
            });

            view.findViewById(R.id.wall_post).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    makePost(null, "Hello, friends!");
                }
            });

            view.findViewById(R.id.upload_photo).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                final Bitmap photo = getPhoto();
                VKRequest request = VKApi.uploadAlbumPhotoRequest(new VKUploadImage(photo, VKImageParameters.pngImage()), 181808365, 60479154);
                request.executeWithListener(new VKRequestListener() {
                    @Override
                    public void onComplete(VKResponse response) {
                        photo.recycle();
                        VKPhotoArray photoArray = (VKPhotoArray) response.parsedModel;
                        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("https://vk.com/photo-60479154_%s", photoArray.get(0).id)));
                        startActivity(i);
                    }

                    @Override
                    public void onError(VKError error) {
                        showError(error);
                    }
                });
                }
            });

            view.findViewById(R.id.test_validation).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final VKRequest test = new VKRequest("account.testValidation");
                    startApiCall(test);
                }
            });
            view.findViewById(R.id.test_share).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final Bitmap b = getPhoto();
                    VKPhotoArray photos = new VKPhotoArray();
                    photos.add(new VKApiPhoto("photo-47200925_314622346"));
                    new VKShareDialog()
                            .setText("I created this post with VK Android SDK\nSee additional information below\n#vksdk")
                            .setUploadedPhotos(photos)
                            .setAttachmentImages(new VKUploadImage[]{
                                    new VKUploadImage(b, VKImageParameters.pngImage())
                            })
                            .setAttachmentLink("VK Android SDK information", "https://vk.com/dev/android_sdk")
                            .setShareDialogListener(new VKShareDialog.VKShareDialogListener() {
                                @Override
                                public void onVkShareComplete(int postId) {
                                    b.recycle();
                                }

                                @Override
                                public void onVkShareCancel() {
                                    b.recycle();
                                }
                            })
                            .show(getFragmentManager(), "VK_SHARE_DIALOG");
                }
            });

            view.findViewById(R.id.upload_photo_to_wall).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final Bitmap photo = getPhoto();
                    VKRequest request = VKApi.uploadWallPhotoRequest(new VKUploadImage(photo, VKImageParameters.jpgImage(0.9f)), 0, 60479154);
                    request.executeWithListener(new VKRequestListener() {
                        @Override
                        public void onComplete(VKResponse response) {
                            photo.recycle();
                            VKApiPhoto photoModel = ((VKPhotoArray) response.parsedModel).get(0);
                            makePost(new VKAttachments(photoModel));
                        }

                        @Override
                        public void onError(VKError error) {
                            showError(error);
                        }
                    });
                }
            });

            view.findViewById(R.id.upload_several_photos_to_wall).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final Bitmap photo = getPhoto();
                    VKRequest request1 = VKApi.uploadWallPhotoRequest(new VKUploadImage(photo, VKImageParameters.jpgImage(0.9f)), 0, 60479154);
                    VKRequest request2 = VKApi.uploadWallPhotoRequest(new VKUploadImage(photo, VKImageParameters.jpgImage(0.5f)), 0, 60479154);
                    VKRequest request3 = VKApi.uploadWallPhotoRequest(new VKUploadImage(photo, VKImageParameters.jpgImage(0.1f)), 0, 60479154);
                    VKRequest request4 = VKApi.uploadWallPhotoRequest(new VKUploadImage(photo, VKImageParameters.pngImage()), 0, 60479154);

                    VKBatchRequest batch = new VKBatchRequest(request1, request2, request3, request4);
                    batch.executeWithListener(new VKBatchRequestListener() {
                        @Override
                        public void onComplete(VKResponse[] responses) {
                            super.onComplete(responses);
                            photo.recycle();
                            VKAttachments attachments = new VKAttachments();
                            for (int i = 0; i < responses.length; i++) {
                                VKApiPhoto photoModel = ((VKPhotoArray) responses[i].parsedModel).get(0);
                                attachments.add(photoModel);
                            }
                            makePost(attachments);
                        }

                        @Override
                        public void onError(VKError error) {
                            showError(error);
                        }
                    });
                }
            });
        }

        private void startApiCall(VKRequest request) {
            Intent i = new Intent(getActivity(), ApiCallActivity.class);
            i.putExtra("request", request.registerObject());
            startActivity(i);
        }

        private void showError(VKError error) {
            new AlertDialog.Builder(getActivity())
                    .setMessage(error.errorMessage)
                    .setPositiveButton("OK", null)
                    .show();

            if (error.httpError != null) {
                Log.w("Test", "Error in request or upload", error.httpError);
            }
        }
        private Bitmap getPhoto() {
            Bitmap b = null;

            try {
                b = BitmapFactory.decodeStream(getActivity().getAssets().open("android.jpg"));
            } catch (IOException e) {
                e.printStackTrace();
            }

            return b;
        }
        private void makePost(VKAttachments attachments) {
            makePost(attachments, null);
        }
        private void makePost(VKAttachments attachments, String message) {
            VKRequest post = VKApi.wall().post(VKParameters.from(VKApiConst.OWNER_ID, "-60479154", VKApiConst.ATTACHMENTS, attachments, VKApiConst.MESSAGE, message));
            post.setModelClass(VKWallPostResult.class);
            post.executeWithListener(new VKRequestListener() {
                @Override
                public void onComplete(VKResponse response) {
                    super.onComplete(response);
                    VKWallPostResult result = (VKWallPostResult)response.parsedModel;
                    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("https://vk.com/wall-60479154_%s", result.post_id) ) );
                    startActivity(i);
                }

                @Override
                public void onError(VKError error) {
                    showError(error.apiError != null ? error.apiError : error);
                }
            });
        }
    }
}




Java Source Code List

com.vk.sdk.VKAccessToken.java
com.vk.sdk.VKObject.java
com.vk.sdk.VKOpenAuthActivity.java
com.vk.sdk.VKScope.java
com.vk.sdk.VKSdkListener.java
com.vk.sdk.VKSdkVersion.java
com.vk.sdk.VKSdk.java
com.vk.sdk.VKUIHelper.java
com.vk.sdk.api.VKApiConst.java
com.vk.sdk.api.VKApi.java
com.vk.sdk.api.VKBatchRequest.java
com.vk.sdk.api.VKDefaultParser.java
com.vk.sdk.api.VKError.java
com.vk.sdk.api.VKParameters.java
com.vk.sdk.api.VKParser.java
com.vk.sdk.api.VKRequest.java
com.vk.sdk.api.VKResponse.java
com.vk.sdk.api.httpClient.VKAbstractOperation.java
com.vk.sdk.api.httpClient.VKHttpClient.java
com.vk.sdk.api.httpClient.VKHttpOperation.java
com.vk.sdk.api.httpClient.VKImageOperation.java
com.vk.sdk.api.httpClient.VKJsonOperation.java
com.vk.sdk.api.httpClient.VKModelOperation.java
com.vk.sdk.api.httpClient.VKMultipartEntity.java
com.vk.sdk.api.methods.VKApiBase.java
com.vk.sdk.api.methods.VKApiCaptcha.java
com.vk.sdk.api.methods.VKApiFriends.java
com.vk.sdk.api.methods.VKApiGroups.java
com.vk.sdk.api.methods.VKApiPhotos.java
com.vk.sdk.api.methods.VKApiUsers.java
com.vk.sdk.api.methods.VKApiWall.java
com.vk.sdk.api.model.Identifiable.java
com.vk.sdk.api.model.ParseUtils.java
com.vk.sdk.api.model.VKApiApplicationContent.java
com.vk.sdk.api.model.VKApiAudio.java
com.vk.sdk.api.model.VKApiChat.java
com.vk.sdk.api.model.VKApiCity.java
com.vk.sdk.api.model.VKApiComment.java
com.vk.sdk.api.model.VKApiCommunityArray.java
com.vk.sdk.api.model.VKApiCommunityFull.java
com.vk.sdk.api.model.VKApiCommunity.java
com.vk.sdk.api.model.VKApiCountry.java
com.vk.sdk.api.model.VKApiDocument.java
com.vk.sdk.api.model.VKApiLink.java
com.vk.sdk.api.model.VKApiMessage.java
com.vk.sdk.api.model.VKApiModel.java
com.vk.sdk.api.model.VKApiNote.java
com.vk.sdk.api.model.VKApiOwner.java
com.vk.sdk.api.model.VKApiPhotoAlbum.java
com.vk.sdk.api.model.VKApiPhotoSize.java
com.vk.sdk.api.model.VKApiPhoto.java
com.vk.sdk.api.model.VKApiPlace.java
com.vk.sdk.api.model.VKApiPoll.java
com.vk.sdk.api.model.VKApiPost.java
com.vk.sdk.api.model.VKApiPostedPhoto.java
com.vk.sdk.api.model.VKApiSchool.java
com.vk.sdk.api.model.VKApiUniversity.java
com.vk.sdk.api.model.VKApiUserFull.java
com.vk.sdk.api.model.VKApiUser.java
com.vk.sdk.api.model.VKApiVideo.java
com.vk.sdk.api.model.VKApiWikiPage.java
com.vk.sdk.api.model.VKAttachments.java
com.vk.sdk.api.model.VKCommentArray.java
com.vk.sdk.api.model.VKList.java
com.vk.sdk.api.model.VKPhotoArray.java
com.vk.sdk.api.model.VKPhotoSizes.java
com.vk.sdk.api.model.VKPostArray.java
com.vk.sdk.api.model.VKPrivacy.java
com.vk.sdk.api.model.VKScopes.java
com.vk.sdk.api.model.VKUsersArray.java
com.vk.sdk.api.model.VKWallPostResult.java
com.vk.sdk.api.model.package-info.java
com.vk.sdk.api.photo.VKImageParameters.java
com.vk.sdk.api.photo.VKUploadAlbumPhotoRequest.java
com.vk.sdk.api.photo.VKUploadImage.java
com.vk.sdk.api.photo.VKUploadPhotoBase.java
com.vk.sdk.api.photo.VKUploadWallPhotoRequest.java
com.vk.sdk.dialogs.VKCaptchaDialog.java
com.vk.sdk.dialogs.VKShareDialog.java
com.vk.sdk.util.VKJsonHelper.java
com.vk.sdk.util.VKStringJoiner.java
com.vk.sdk.util.VKUtil.java
com.vk.vktestapp.ApiCallActivity.java
com.vk.vktestapp.LoginActivity.java
com.vk.vktestapp.TestActivity.java