mirror of
https://github.com/samuelclay/NewsBlur.git
synced 2025-09-18 21:50:56 +00:00
NetworkUtils changes from removing redirect
This commit is contained in:
parent
57611eddff
commit
caae1fa9e2
1 changed files with 4 additions and 19 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue