Skip to content

Instantly share code, notes, and snippets.

@zaki50
Created May 23, 2013 18:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaki50/5638433 to your computer and use it in GitHub Desktop.
Save zaki50/5638433 to your computer and use it in GitHub Desktop.
adamrocker さんの volley サンプル https://github.com/adamrocker/volley を元に OkHttp を組み込んで SPDY でアクセスするための差分。 変更後のものは https://github.com/zaki50/volley/tree/with-OkHttp にあります。
diff --git a/VolleySample/libs/okhttp-1.0.2.jar b/VolleySample/libs/okhttp-1.0.2.jar
new file mode 100644
index 0000000..7592cdf
Binary files /dev/null and b/VolleySample/libs/okhttp-1.0.2.jar differ
diff --git a/VolleySample/libs/volley.jar b/VolleySample/libs/volley.jar
index 31c143c..762e993 100644
Binary files a/VolleySample/libs/volley.jar and b/VolleySample/libs/volley.jar differ
diff --git a/VolleySample/src/com/adamrocker/volleysample/MainActivity.java b/VolleySample/src/com/adamrocker/volleysample/MainActivity.java
index b2c1dd5..c2118c5 100644
--- a/VolleySample/src/com/adamrocker/volleysample/MainActivity.java
+++ b/VolleySample/src/com/adamrocker/volleysample/MainActivity.java
@@ -3,12 +3,18 @@ package com.adamrocker.volleysample;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
+import com.android.volley.toolbox.HurlStack;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
@@ -16,8 +22,9 @@ import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.adamrocker.volleysample.R;
+import com.squareup.okhttp.OkHttpClient;
+
import android.os.Bundle;
-import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
@@ -36,7 +43,6 @@ import android.widget.Toast;
public class MainActivity extends SherlockActivity {
- private static final String INSTAGRAM_CLIENT_ID = "2954ec574e53454abefc82073af058c2";
private static final Object TAG = new Object();
private static final String LOG = "VOLLEY-SAMPLE";
private RequestQueue mVolley;
@@ -69,6 +75,15 @@ public class MainActivity extends SherlockActivity {
}
}
+ static class OkHttpStack extends HurlStack {
+ private final OkHttpClient mClient = new OkHttpClient();
+
+ @Override
+ protected HttpURLConnection createConnection(URL url) throws IOException {
+ return mClient.open(url);
+ }
+ }
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -76,15 +91,14 @@ public class MainActivity extends SherlockActivity {
setContentView(R.layout.photo_stream);
mBase = (LinearLayout) findViewById(R.id.base);
- mVolley = Volley.newRequestQueue(getApplicationContext());// thread
- // pool(4)
+ mVolley = Volley.newRequestQueue(getApplicationContext(), new OkHttpStack()); // thread
+ // pool(4)
startLoadingAnim();
refreshDatas();
}
private void refreshDatas() {
- String url = "https://api.instagram.com/v1/media/popular?client_id="
- + INSTAGRAM_CLIENT_ID;
+ String url = "https://picasaweb.google.com/data/feed/api/user/117758246455073005073/albumid/5881235953426455569?alt=json";
JsonObjectRequest jsonRequet = new JsonObjectRequest(Method.GET, url,
null, new Listener<JSONObject>() {
public void onResponse(JSONObject result) {
@@ -108,8 +122,7 @@ public class MainActivity extends SherlockActivity {
}
private int parseJson(JSONObject root) throws JSONException {
- int code = root.getJSONObject("meta").getInt("code");
- if (code == 200) {
+ if (true) {
int[] resTexts = { R.id.photo_text1, R.id.photo_text2,
R.id.photo_text3, R.id.photo_text4, R.id.photo_text5,
R.id.photo_text6 };
@@ -118,7 +131,7 @@ public class MainActivity extends SherlockActivity {
int resIndex = 0;
LayoutInflater inf = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
- JSONArray arr = root.getJSONArray("data");
+ JSONArray arr = root.getJSONObject("feed").getJSONArray("entry");
final int len = arr.length();
RelativeLayout rl = (RelativeLayout) inf.inflate(
R.layout.photo_item, null);
@@ -127,9 +140,9 @@ public class MainActivity extends SherlockActivity {
JSONObject json = arr.getJSONObject(i);
String text = null;
try {
- JSONObject caption = json.getJSONObject("caption");
+ JSONObject caption = json.getJSONObject("title");
if (caption != null) {
- text = caption.getString("text");
+ text = caption.getString("$t");
} else {
text = "...";
}
@@ -138,8 +151,7 @@ public class MainActivity extends SherlockActivity {
Log.e(LOG, json.toString());
text = "...";
}
- String imgUrl = json.getJSONObject("images")
- .getJSONObject("low_resolution").getString("url");
+ String imgUrl = json.getJSONObject("content").getString("src");
ImageView iv = (ImageView) rl.findViewById(resImgs[resIndex]);
if (iv == null) {
@@ -179,7 +191,7 @@ public class MainActivity extends SherlockActivity {
mVolley.add(request);
}
}
- return code;
+ return 200;
}
public void onStop() {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment