import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpUriRequest;
import org.json.JSONException;
import org.json.JSONObject;
-import android.net.http.AndroidHttpClient;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
public class StreamUtility {
//private static final String LOGTAG = StreamUtility.class.getSimpleName();
}
public static String downloadUriAsString(String uri) throws IOException {
- HttpGet get = new HttpGet(uri);
+ URL get = new URL(uri);
return downloadUriAsString(get);
}
- public static String downloadUriAsString(final HttpUriRequest req) throws IOException {
- AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
- try {
- HttpResponse res = client.execute(req);
- return readToEnd(res.getEntity().getContent());
- }
- finally {
- client.close();
+ public static String downloadUriAsString(final URL req) throws IOException {
+ InputStream in = req.openStream();
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+ StringBuilder result = new StringBuilder();
+ String line;
+ while((line = reader.readLine()) != null) {
+ result.append(line);
}
+ return result.toString();
}
public static JSONObject downloadUriAsJSONObject(String uri) throws IOException, JSONException {
return new JSONObject(downloadUriAsString(uri));
}
- public static JSONObject downloadUriAsJSONObject(HttpUriRequest req) throws IOException, JSONException {
+ public static JSONObject downloadUriAsJSONObject(URL req) throws IOException, JSONException {
return new JSONObject(downloadUriAsString(req));
}