From dfc79a96fde05dd7eae3c7aa69de7b15eb9c560d Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 21 Jul 2010 19:49:03 -0700 Subject: [PATCH] slurp and parse JSON in preupload --- .../src/com/danga/camli/UploadThread.java | 9 +++++++++ .../uploader/src/com/danga/camli/Util.java | 15 +++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 clients/android/uploader/src/com/danga/camli/Util.java diff --git a/clients/android/uploader/src/com/danga/camli/UploadThread.java b/clients/android/uploader/src/com/danga/camli/UploadThread.java index d5edd8829..80a4e0c07 100644 --- a/clients/android/uploader/src/com/danga/camli/UploadThread.java +++ b/clients/android/uploader/src/com/danga/camli/UploadThread.java @@ -18,6 +18,8 @@ import org.apache.http.impl.DefaultHttpRequestFactory; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; +import org.json.JSONException; +import org.json.JSONObject; import android.util.Log; @@ -70,12 +72,19 @@ public class UploadThread extends Thread { Log.d(TAG, "response: " + res); Log.d(TAG, "response code: " + res.getStatusLine()); Log.d(TAG, "entity: " + res.getEntity()); + + String jsonSlurp = Util.slurp(res.getEntity().getContent()); + Log.d(TAG, "JSON content: " + jsonSlurp); + JSONObject json = new JSONObject(jsonSlurp); + Log.d(TAG, "JSON response: " + json); } catch (ClientProtocolException e) { Log.e(TAG, "preupload error", e); return; } catch (IOException e) { Log.e(TAG, "preupload error", e); return; + } catch (JSONException e) { + Log.e(TAG, "JSON parse error", e); } } } diff --git a/clients/android/uploader/src/com/danga/camli/Util.java b/clients/android/uploader/src/com/danga/camli/Util.java new file mode 100644 index 000000000..f4695eb73 --- /dev/null +++ b/clients/android/uploader/src/com/danga/camli/Util.java @@ -0,0 +1,15 @@ +package com.danga.camli; + +import java.io.IOException; +import java.io.InputStream; + +public class Util { + public static String slurp(InputStream in) throws IOException { + StringBuilder sb = new StringBuilder(); + byte[] b = new byte[4096]; + for (int n; (n = in.read(b)) != -1;) { + sb.append(new String(b, 0, n)); + } + return sb.toString(); + } +}