NetworkUtils changes from removing redirect

This commit is contained in:
Mark Anderson 2015-03-25 22:22:51 +00:00
parent 57611eddff
commit caae1fa9e2

View file

@ -11,7 +11,6 @@ import com.squareup.okhttp.Response;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;
@ -31,38 +30,24 @@ public class NetworkUtils {
return (netInfo != null && netInfo.isConnected());
}
public static UrlLoadResult loadURL(URL url, OutputStream outputStream) throws IOException {
UrlLoadResult result = new UrlLoadResult();
public static int loadURL(URL url, OutputStream outputStream) throws IOException {
int bytesRead = 0;
try {
Request.Builder requestBuilder = new Request.Builder().url(url);
Response response = httpClient.newCall(requestBuilder.build()).execute();
if (response.isSuccessful()) {
int code = response.code();
// we explicitly requested redirects, so if we still get one, it is because of a protocol
// change. inform the caller by returning the new URL
if ((code == HttpURLConnection.HTTP_MOVED_TEMP) || (code == HttpURLConnection.HTTP_MOVED_PERM)) {
String loc = response.header("Location");
result.redirUrl = loc;
return result;
}
InputStream inputStream = response.body().byteStream();
byte[] b = new byte[1024];
int read;
while ((read = inputStream.read(b)) != -1) {
outputStream.write(b, 0, read);
result.bytesRead += read;
bytesRead += read;
}
}
} catch (Throwable t) {
// a huge number of things could go wrong fetching and storing an image. don't spam logs with them
}
return result;
return bytesRead;
}
public static class UrlLoadResult extends Object {
public int bytesRead;
public String redirUrl;
}
}