Skip to content

Commit 819f881

Browse files
committed
new sample project
1 parent 02218db commit 819f881

118 files changed

Lines changed: 10186 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

HTTP/Volley/app/build.gradle

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apply plugin: 'com.android.application'
2+
3+
dependencies {
4+
compile 'de.greenrobot:eventbus:2.4.0'
5+
compile project(':volley')
6+
compile 'com.google.code.gson:gson:2.5'
7+
compile 'com.android.support:support-v4:23.1.1'
8+
}
9+
10+
android {
11+
compileSdkVersion 19
12+
buildToolsVersion "21.1.2"
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.commonsware.android.volley"
4+
android:versionCode="1"
5+
android:versionName="1.0">
6+
7+
<uses-sdk
8+
android:minSdkVersion="11"
9+
android:targetSdkVersion="17"/>
10+
<uses-permission android:name="android.permission.INTERNET"/>
11+
12+
<application
13+
android:allowBackup="true"
14+
android:icon="@drawable/ic_launcher"
15+
android:label="@string/app_name"
16+
android:theme="@style/AppTheme">
17+
<activity
18+
android:name=".MainActivity"
19+
android:label="@string/app_name">
20+
<intent-filter>
21+
<action android:name="android.intent.action.MAIN"/>
22+
23+
<category android:name="android.intent.category.LAUNCHER"/>
24+
</intent-filter>
25+
</activity>
26+
</application>
27+
28+
</manifest>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// from https://gist.github.com/ficusk/5474673
2+
3+
package com.commonsware.android.volley;
4+
5+
import com.google.gson.Gson;
6+
import com.google.gson.JsonSyntaxException;
7+
8+
import com.android.volley.AuthFailureError;
9+
import com.android.volley.NetworkResponse;
10+
import com.android.volley.ParseError;
11+
import com.android.volley.Request;
12+
import com.android.volley.Response;
13+
import com.android.volley.Response.ErrorListener;
14+
import com.android.volley.Response.Listener;
15+
import com.android.volley.toolbox.HttpHeaderParser;
16+
17+
import java.io.UnsupportedEncodingException;
18+
import java.util.Map;
19+
20+
/**
21+
* Volley adapter for JSON requests that will be parsed into Java objects by Gson.
22+
*/
23+
public class GsonRequest<T> extends Request<T> {
24+
private final Gson gson = new Gson();
25+
private final Class<T> clazz;
26+
private final Map<String, String> headers;
27+
private final Listener<T> listener;
28+
29+
/**
30+
* Make a GET request and return a parsed object from JSON.
31+
*
32+
* @param url URL of the request to make
33+
* @param clazz Relevant class object, for Gson's reflection
34+
* @param headers Map of request headers
35+
*/
36+
public GsonRequest(String url, Class<T> clazz, Map<String, String> headers,
37+
Listener<T> listener, ErrorListener errorListener) {
38+
super(Method.GET, url, errorListener);
39+
this.clazz = clazz;
40+
this.headers = headers;
41+
this.listener = listener;
42+
}
43+
44+
@Override
45+
public Map<String, String> getHeaders() throws AuthFailureError {
46+
return headers != null ? headers : super.getHeaders();
47+
}
48+
49+
@Override
50+
protected void deliverResponse(T response) {
51+
listener.onResponse(response);
52+
}
53+
54+
@Override
55+
protected Response<T> parseNetworkResponse(NetworkResponse response) {
56+
try {
57+
String json = new String(
58+
response.data, HttpHeaderParser.parseCharset(response.headers));
59+
return Response.success(
60+
gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));
61+
} catch (UnsupportedEncodingException e) {
62+
return Response.error(new ParseError(e));
63+
} catch (JsonSyntaxException e) {
64+
return Response.error(new ParseError(e));
65+
}
66+
}
67+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/***
2+
Copyright (c) 2013 CommonsWare, LLC
3+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
4+
use this file except in compliance with the License. You may obtain a copy
5+
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
6+
by applicable law or agreed to in writing, software distributed under the
7+
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
8+
OF ANY KIND, either express or implied. See the License for the specific
9+
language governing permissions and limitations under the License.
10+
11+
From _The Busy Coder's Guide to Android Development_
12+
https://commonsware.com/Android
13+
*/
14+
15+
package com.commonsware.android.volley;
16+
17+
public class Item {
18+
String title;
19+
Owner owner;
20+
String link;
21+
22+
@Override
23+
public String toString() {
24+
return(title);
25+
}
26+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// from http://developer.android.com/training/volley/request.html
2+
3+
package com.commonsware.android.volley;
4+
5+
import android.content.Context;
6+
import android.graphics.Bitmap;
7+
import android.support.v4.util.LruCache;
8+
import android.util.DisplayMetrics;
9+
import com.android.volley.toolbox.ImageLoader.ImageCache;
10+
11+
public class LruBitmapCache extends LruCache<String, Bitmap>
12+
implements ImageCache {
13+
14+
public LruBitmapCache(int maxSize) {
15+
super(maxSize);
16+
}
17+
18+
public LruBitmapCache(Context ctx) {
19+
this(getCacheSize(ctx));
20+
}
21+
22+
@Override
23+
protected int sizeOf(String key, Bitmap value) {
24+
return value.getRowBytes() * value.getHeight();
25+
}
26+
27+
@Override
28+
public Bitmap getBitmap(String url) {
29+
return get(url);
30+
}
31+
32+
@Override
33+
public void putBitmap(String url, Bitmap bitmap) {
34+
put(url, bitmap);
35+
}
36+
37+
// Returns a cache size equal to approximately three screens worth of images.
38+
public static int getCacheSize(Context ctx) {
39+
final DisplayMetrics displayMetrics = ctx.getResources().
40+
getDisplayMetrics();
41+
final int screenWidth = displayMetrics.widthPixels;
42+
final int screenHeight = displayMetrics.heightPixels;
43+
// 4 bytes per pixel
44+
final int screenBytes = screenWidth * screenHeight * 4;
45+
46+
return screenBytes * 3;
47+
}
48+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/***
2+
Copyright (c) 2013-2014 CommonsWare, LLC
3+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
4+
use this file except in compliance with the License. You may obtain a copy
5+
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
6+
by applicable law or agreed to in writing, software distributed under the
7+
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
8+
OF ANY KIND, either express or implied. See the License for the specific
9+
language governing permissions and limitations under the License.
10+
11+
From _The Busy Coder's Guide to Android Development_
12+
https://commonsware.com/Android
13+
*/
14+
15+
package com.commonsware.android.volley;
16+
17+
import android.app.Activity;
18+
import android.content.Intent;
19+
import android.net.Uri;
20+
import android.os.Bundle;
21+
import de.greenrobot.event.EventBus;
22+
23+
public class MainActivity extends Activity {
24+
@Override
25+
protected void onCreate(Bundle savedInstanceState) {
26+
super.onCreate(savedInstanceState);
27+
28+
if (getFragmentManager().findFragmentById(android.R.id.content) == null) {
29+
getFragmentManager().beginTransaction()
30+
.add(android.R.id.content,
31+
new QuestionsFragment()).commit();
32+
}
33+
}
34+
35+
@Override
36+
public void onResume() {
37+
super.onResume();
38+
EventBus.getDefault().register(this);
39+
}
40+
41+
@Override
42+
public void onPause() {
43+
EventBus.getDefault().unregister(this);
44+
super.onPause();
45+
}
46+
47+
public void onEventMainThread(QuestionClickedEvent event) {
48+
startActivity(new Intent(Intent.ACTION_VIEW,
49+
Uri.parse(event.item.link)));
50+
}
51+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/***
2+
Copyright (c) 2013 CommonsWare, LLC
3+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
4+
use this file except in compliance with the License. You may obtain a copy
5+
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
6+
by applicable law or agreed to in writing, software distributed under the
7+
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
8+
OF ANY KIND, either express or implied. See the License for the specific
9+
language governing permissions and limitations under the License.
10+
11+
From _The Busy Coder's Guide to Android Development_
12+
https://commonsware.com/Android
13+
*/
14+
15+
package com.commonsware.android.volley;
16+
17+
import com.google.gson.annotations.SerializedName;
18+
19+
public class Owner {
20+
@SerializedName("profile_image") String profileImage;
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/***
2+
Copyright (c) 2013-2014 CommonsWare, LLC
3+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
4+
use this file except in compliance with the License. You may obtain a copy
5+
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
6+
by applicable law or agreed to in writing, software distributed under the
7+
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
8+
OF ANY KIND, either express or implied. See the License for the specific
9+
language governing permissions and limitations under the License.
10+
11+
From _The Busy Coder's Guide to Android Development_
12+
https://commonsware.com/Android
13+
*/
14+
15+
package com.commonsware.android.volley;
16+
17+
public class QuestionClickedEvent {
18+
final Item item;
19+
20+
QuestionClickedEvent(Item item) {
21+
this.item=item;
22+
}
23+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/***
2+
Copyright (c) 2013-2016 CommonsWare, LLC
3+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
4+
use this file except in compliance with the License. You may obtain a copy
5+
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
6+
by applicable law or agreed to in writing, software distributed under the
7+
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
8+
OF ANY KIND, either express or implied. See the License for the specific
9+
language governing permissions and limitations under the License.
10+
11+
From _The Busy Coder's Guide to Android Development_
12+
https://commonsware.com/Android
13+
*/
14+
15+
package com.commonsware.android.volley;
16+
17+
import android.app.ListFragment;
18+
import android.os.Bundle;
19+
import android.text.Html;
20+
import android.util.Log;
21+
import android.view.View;
22+
import android.view.ViewGroup;
23+
import android.widget.ArrayAdapter;
24+
import android.widget.ImageView;
25+
import android.widget.ListView;
26+
import android.widget.TextView;
27+
import android.widget.Toast;
28+
import com.android.volley.RequestQueue;
29+
import com.android.volley.Response;
30+
import com.android.volley.VolleyError;
31+
import com.android.volley.toolbox.Volley;
32+
import java.util.List;
33+
import de.greenrobot.event.EventBus;
34+
35+
public class QuestionsFragment extends ListFragment implements
36+
Response.Listener<SOQuestions>, Response.ErrorListener {
37+
38+
@Override
39+
public void onCreate(Bundle savedInstanceState) {
40+
super.onCreate(savedInstanceState);
41+
42+
setRetainInstance(true);
43+
}
44+
45+
@Override
46+
public void onViewCreated(View view,
47+
Bundle savedInstanceState) {
48+
super.onViewCreated(view, savedInstanceState);
49+
50+
GsonRequest<SOQuestions> request=
51+
new GsonRequest<SOQuestions>(getString(R.string.url),
52+
SOQuestions.class, null, this, this);
53+
54+
VolleyManager.get(getActivity()).enqueue(request);
55+
}
56+
57+
@Override
58+
public void onListItemClick(ListView l, View v, int position, long id) {
59+
Item item=((ItemsAdapter)getListAdapter()).getItem(position);
60+
61+
EventBus.getDefault().post(new QuestionClickedEvent(item));
62+
}
63+
64+
@Override
65+
public void onErrorResponse(VolleyError error) {
66+
Toast.makeText(getActivity(), error.getMessage(),
67+
Toast.LENGTH_LONG).show();
68+
Log.e(getClass().getSimpleName(),
69+
"Exception from Volley request to StackOverflow", error);
70+
}
71+
72+
@Override
73+
public void onResponse(SOQuestions questions) {
74+
setListAdapter(new ItemsAdapter(questions.items));
75+
}
76+
77+
class ItemsAdapter extends ArrayAdapter<Item> {
78+
ItemsAdapter(List<Item> items) {
79+
super(getActivity(), R.layout.row, R.id.title, items);
80+
}
81+
82+
@Override
83+
public View getView(int position, View convertView, ViewGroup parent) {
84+
View row=super.getView(position, convertView, parent);
85+
Item item=getItem(position);
86+
ImageView icon=(ImageView)row.findViewById(R.id.icon);
87+
88+
VolleyManager
89+
.get(getActivity())
90+
.loadImage(item.owner.profileImage, icon,
91+
R.drawable.owner_placeholder,
92+
R.drawable.owner_error);
93+
94+
TextView title=(TextView)row.findViewById(R.id.title);
95+
96+
title.setText(Html.fromHtml(getItem(position).title));
97+
98+
return(row);
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)