uk.co.senab.bitmapcache.samples.ViewPagerActivity Maven / Gradle / Ivy
/*******************************************************************************
* Copyright 2011, 2013 Chris Banes.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package uk.co.senab.bitmapcache.samples;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
public class ViewPagerActivity extends Activity {
static final int PUG_COUNT = 20;
/**
* This task simply gets a list of URLs of Photos from PugMe
*/
private class PugListAsyncTask extends AsyncTask> {
static final String PUG_ME_URL = "http://pugme.herokuapp.com/bomb?count=" + PUG_COUNT;
@Override
protected ArrayList doInBackground(Void... params) {
try {
HttpURLConnection conn = (HttpURLConnection) new URL(PUG_ME_URL).openConnection();
conn.setRequestProperty("Accept", "application/json");
InputStream is = conn.getInputStream();
StringBuilder sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(is), 1024);
for (String line = r.readLine(); line != null; line = r.readLine()) {
sb.append(line);
}
try {
is.close();
} catch (IOException e) {
}
String response = sb.toString();
JSONObject document = new JSONObject(response);
JSONArray pugsJsonArray = document.getJSONArray("pugs");
HashSet pugUrls = new HashSet(pugsJsonArray.length());
for (int i = 0, z = pugsJsonArray.length(); i < z; i++) {
pugUrls.add(pugsJsonArray.getString(i));
}
return new ArrayList(pugUrls);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(ArrayList result) {
super.onPostExecute(result);
PugPagerAdapter adapter = new PugPagerAdapter(ViewPagerActivity.this, result);
mViewPager.setAdapter(adapter);
}
}
private ViewPager mViewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
setContentView(mViewPager);
// Start Pug List Download
new PugListAsyncTask().execute();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy