OSDN Git Service

Fixes for koush's Superuser on marshmallow marshmallow-x86 android-x86-6.0-r1 android-x86-6.0-r2 android-x86-6.0-r3
authorPaulo Sergio Travaglia <pstglia@gmail.com>
Wed, 6 Jan 2016 00:11:34 +0000 (22:11 -0200)
committerPaulo Sergio Travaglia <pstglia@gmail.com>
Wed, 6 Jan 2016 00:11:34 +0000 (22:11 -0200)
Koush Superuser uses Apache http client imports, which are
deprecated on API 23 (marshmallow).

As google recommends, it was replaced by HttpURLConnection

References:
http://developer.android.com/intl/pt-br/about/versions/marshmallow/android-6.0-changes.html
http://stackoverflow.com/questions/9856195/how-to-read-an-http-input-stream
https://developer.android.com/intl/pt-br/reference/java/net/HttpURLConnection.html

Superuser/src/com/koushikdutta/superuser/util/StreamUtility.java

index 63c23ba..7c0da62 100644 (file)
@@ -14,13 +14,13 @@ import java.nio.channels.Channels;
 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();
@@ -52,27 +52,27 @@ public class StreamUtility {
     }
 
     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));
     }