-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.picasaProfileImageViaJson
328 lines (202 loc) · 8 KB
/
.picasaProfileImageViaJson
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package com.gracetech.glimps;
import java.io.IOException;
import android.os.AsyncTask;
import com.gracetech.glimps.callbacks.WDPicasaCallBacks;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
public class PicasaImagesHelper implements WDPicasaCallBacks {
//https://picasaweb.google.com/data/feed/api/user/103316116977988458724/?alt=jsonc//
String userID = "103316116977988458724";
String jsonFun = "?alt=jsonc";
ArrayList<String> urls = new ArrayList<>();
protected final String defaultUrl = "https://picasaweb.google.com/data/feed/api/user/";
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
boolean profileAlbumFound = false;
WDPicasaCallBacks picasaCallBack = null;
// Picasa JSON response node keys
private static final
String
TAG_DATA = "data",
TAG_ITEMS = "items",
TAG_TITLE = "title",
TAG_PROFILE = "profile",
TAG_MEDIA = "media",
TAG_URL = "url",
TAG_ALBUMID = "albumid",
TAG_IMAGE = "image";
private String url = null;
PicasaImagesHelper() {
// default params just for testing //
url = defaultUrl + userID + jsonFun;
getJsonStringFromUrlAsync(url, this);
}
PicasaImagesHelper(String userID , WDPicasaCallBacks picasaCallBack) {
this.picasaCallBack = picasaCallBack;
this.userID = userID;
this.url = defaultUrl + userID + jsonFun;
getJsonStringFromUrlAsync(url, this);
}
public void getJsonStringFromUrlAsync(final String jsonUrl, final PicasaImagesHelper callback) {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String results = null;
return results = (makeServiceCall(url, GET, null));// getJSONFromUrl();
}
@Override
protected void onPostExecute(String jsonObject) {
callback.resultFromHtmlToJson(jsonObject);
}
}.execute();
}
public void parserJsonAsyncTogetAlbumId(final JSONObject jObj) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
try {
parserJsonTogetProfileAlbumID();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}.execute();
}
public void parserJsonAsyncTogetAlbumImagesUrls(final JSONObject jObj) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
try {
parserJsonTogetProfileImagesUrls();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}.execute();
}
public String makeServiceCall(String url, int method,
List<NameValuePair> params) {
try {
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
private void resultFromHtmlToJson(String jsonString) {
if (jsonString != null && !profileAlbumFound) {
try {
jObj = new JSONObject(jsonString);
parserJsonAsyncTogetAlbumId(jObj);
} catch (JSONException e) {
e.printStackTrace();
}
} else if (jsonString != null && profileAlbumFound) {
// Here we get the Json profile Album String //
try {
jObj = new JSONObject(jsonString);
parserJsonTogetProfileImagesUrls();
} catch (JSONException e) {
e.printStackTrace();
}
} else
Mesg.SimpleMsg("Can't get Json String");
}
private void albumsResults(ArrayList<String> urls) {
int x = 0;
}
private void parserJsonTogetProfileAlbumID() throws JSONException {
JSONArray items = jObj.getJSONObject(TAG_DATA).getJSONArray(TAG_ITEMS);
for (int index = 0; index < items.length(); ) {
String title = items.getJSONObject(index).getString(TAG_TITLE);
if (title.toLowerCase().contains(TAG_PROFILE)) {
Mesg.L("we fine the profile album now grab the urls ");
profileAlbumFound = true;
String albumID = items.getJSONObject(index).getString("id");
url = defaultUrl + userID + "/" + TAG_ALBUMID + "/" + albumID + jsonFun;
jObj = null;
getJsonStringFromUrlAsync(url, this);
}
Mesg.L(" title : " + title.toString() + "/n");
if (profileAlbumFound != true)
index++;
else break;
}
}
private void parserJsonTogetProfileImagesUrls() throws JSONException {
JSONArray media = jObj.getJSONObject(TAG_DATA).getJSONArray(TAG_ITEMS);
for (int index = 0; index < media.length(); ) {
JSONObject jsonMedia = media.getJSONObject(index).getJSONObject(TAG_MEDIA);
JSONObject jsonImage = jsonMedia.getJSONObject(TAG_IMAGE);
String jsonUrl = jsonImage.getString(TAG_URL);
urls.add(jsonUrl);
Mesg.L(" url : " + jsonUrl.toString() + "/n");
index++;
}
finishGrabbingAlltheProfilePublicPicasaUserUrls();
}
private void finishGrabbingAlltheProfilePublicPicasaUserUrls() {
// send back the urls Array //
picasaCallBack.getPicasaImagesUrls ( urls);
}
@Override
public void getPicasaImagesUrls(ArrayList<String> urls) {
}
}
**// The Interface to connect with the PicasaClass **//
package com.gracetech.glimps.callbacks;
import java.util.ArrayList;
/**
* Created by Administrator on 21-Mar-15.
*/
public interface WDPicasaCallBacks {
void getPicasaImagesUrls(ArrayList<String> urls);
}