slurp and parse JSON in preupload

This commit is contained in:
Brad Fitzpatrick 2010-07-21 19:49:03 -07:00
parent 373a336bc6
commit dfc79a96fd
2 changed files with 24 additions and 0 deletions

View File

@ -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);
}
}
}

View File

@ -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();
}
}