package com kucherovpi mygruops2 import android content Intent import

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.kucherovpi.mygruops2;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.vk.sdk.VKAccessToken;
import com.vk.sdk.VKScope;
import com.vk.sdk.VKSdk;
import com.vk.sdk.VKSdkListener;
import com.vk.sdk.VKUIHelper;
import com.vk.sdk.api.VKApi;
import com.vk.sdk.api.VKApiConst;
import com.vk.sdk.api.VKError;
import com.vk.sdk.api.VKParameters;
import com.vk.sdk.api.VKRequest;
import com.vk.sdk.api.VKResponse;
import com.vk.sdk.api.model.VKApiCommunityArray;
import com.vk.sdk.api.model.VKApiCommunityFull;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class MainActivity extends ActionBarActivity {
static final String TAG_MYGROUPS = "MyGroups_Debug";
private static final String APP_ID = "4799427";
private static final String USER_ID = "83635369";
ImageLoader imageLoader;
private Button btnLogin, btnLogout;
private TextView tvGo, tvRequest, tvRequest2, tvRequest3;
private ImageView imageView, imageView2, imageView3;
private String[] sMyScopes = new String[]{
VKScope.STATUS,
VKScope.NOTIFY,
VKScope.GROUPS,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Инициализация приложения
VKSdk.initialize(sdkListener, APP_ID);
imageLoader = ImageLoader.getInstance(); // Получили экземпляр ImageLoader
imageLoader.init(ImageLoaderConfiguration.createDefault(this)); // Проинициализировали конфигом по умолчанию
VKUIHelper.onCreate(this);
tvGo = (TextView) findViewById(R.id.tvGo);
btnLogin = (Button) findViewById(R.id.btnLogin);
btnLogout = (Button) findViewById(R.id.btnLogout);
tvRequest = (TextView) findViewById(R.id.tvRequest);
tvRequest2 = (TextView) findViewById(R.id.tvRequest2);
tvRequest3 = (TextView) findViewById(R.id.tvRequest3);
imageView = (ImageView) findViewById(R.id.imageView);
imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView3 = (ImageView) findViewById(R.id.imageView3);
checkSession();
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
VKSdk.authorize(sMyScopes);
}
});
btnLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
VKSdk.logout();
btnLogin.setVisibility(View.VISIBLE);
btnLogout.setVisibility(View.GONE);
tvGo.setVisibility(View.GONE);
tvRequest.setText("");
tvRequest2.setText("");
tvRequest3.setText("");
imageView.setVisibility(View.GONE);
imageView2.setVisibility(View.GONE);
imageView3.setVisibility(View.GONE);
}
});
}
private final VKSdkListener sdkListener = new VKSdkListener() {
@Override
public void onCaptchaError(VKError captchaError) {
}
@Override
public void onTokenExpired(VKAccessToken expiredToken) {
}
@Override
public void onAccessDenied(VKError authorizationError) {
}
@Override
public void onReceiveNewToken(VKAccessToken newToken) {
}
};
@Override
protected void onResume() {
super.onResume();
VKUIHelper.onResume(this);
checkSession();
requestTwo();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
VKUIHelper.onActivityResult(this, requestCode, resultCode, data);
}
@Override
protected void onDestroy() {
super.onDestroy();
VKUIHelper.onDestroy(this);
}
//Проверка, восстановление сессии
private void checkSession() {
if (VKSdk.wakeUpSession()) {
if (!(VKSdk.isLoggedIn())) {
tvRequest.setText("");
tvRequest2.setText("");
tvRequest3.setText("");
imageView.setVisibility(View.GONE);
imageView2.setVisibility(View.GONE);
imageView3.setVisibility(View.GONE);
}
btnLogin.setVisibility(View.GONE);
btnLogout.setVisibility(View.VISIBLE);
tvGo.setVisibility(View.VISIBLE);
} else {
btnLogin.setVisibility(View.VISIBLE);
}
}
private void requestTwo() {
//Запрос списка сообществ пользователя
VKRequest request2 = VKApi.groups().get(VKParameters.from(
VKApiConst.USER_ID, USER_ID,
VKApiConst.EXTENDED, 1,
VKApiConst.COUNT, 3));
//Отправляем запрос и обрабатываем его
request2.executeWithListener(new VKRequest.VKRequestListener() {
@Override
public void onComplete(VKResponse response) {
super.onComplete(response);
Log.d(TAG_MYGROUPS, response.responseString);
VKApiCommunityArray groupsArray = (VKApiCommunityArray) response.parsedModel;
VKApiCommunityFull group = groupsArray.get(0);
VKApiCommunityFull group2 = groupsArray.get(1);
VKApiCommunityFull group3 = groupsArray.get(2);
if (VKSdk.isLoggedIn()) {
tvRequest.append("ID группы: " + group.id + "\r\n");
tvRequest.append(group.name);
imageView.setVisibility(View.VISIBLE);
imageLoader.displayImage(group.photo_100, imageView); // Запустили асинхронный показ картинки
tvRequest2.append("ID группы: " + group2.id + "\r\n");
tvRequest2.append(group2.name);
imageView2.setVisibility(View.VISIBLE);
imageLoader.displayImage(group2.photo_100, imageView2); // Запустили асинхронный показ картинки
tvRequest3.append("ID группы: " + group3.id + "\r\n");
tvRequest3.append(group3.name);
imageView3.setVisibility(View.VISIBLE);
imageLoader.displayImage(group3.photo_100, imageView3); // Запустили асинхронный показ картинки
}
}
@Override
public void attemptFailed(VKRequest request, int attemptNumber, int totalAttempts) {
super.attemptFailed(request, attemptNumber, totalAttempts);
Log.d(TAG_MYGROUPS, "request2 attemptFailed: " + request + " " + attemptNumber + " " + totalAttempts);
}
@Override
public void onError(VKError error) {
super.onError(error);
Log.d(TAG_MYGROUPS, "request2 onError: " + error);
}
@Override
public void onProgress(VKRequest.VKProgressType progressType, long bytesLoaded, long bytesTotal) {
super.onProgress(progressType, bytesLoaded, bytesTotal);
Log.d(TAG_MYGROUPS, "request2 onProgress: " + progressType + " " + bytesLoaded + " " + bytesTotal);
}
});
}
}