连接失败,.getResponseCode() 不等于200,执行的是getErrorStream(),怎么破?
@Override
public void run() {
try {
System.out.println("start download");
URL httpUrll = new URL(url);
HttpURLConnection httpURLConnection = (HttpURLConnection) httpUrll.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
InputStream inputStream;
if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpURLConnection.getInputStream();
} else {
inputStream = httpURLConnection.getErrorStream();
System.out.println("error+++++++++++++++++");
}
FileOutputStream fileOutputStream = null;
File downloadFile;
File sdkFile;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
downloadFile = Environment.getExternalStorageDirectory();
sdkFile = new File(downloadFile, "test.apk");
fileOutputStream = new FileOutputStream(sdkFile);
}
byte[] bytes = new byte[6 * 1024];
int length;
System.out.println("is here");
while ((length = inputStream.read(bytes)) != -1) {
if (fileOutputStream != null) {
fileOutputStream.write(bytes, 0, length);
}
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
System.out.println("download success");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}