package com.example.administrator.downloadimage;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.ParseException;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by Administrator on 2017/5/4.
*/
public class HttpUtils {
public static boolean isNetWorkConn(Context context) {
ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if (info !=
null) {
return info.isConnected();
}
else {
return false;
}
}
public static byte[]
httpClientLoadData(String path) {
byte[] result =
null;
try {
HttpClient httpClient =
new DefaultHttpClient();
HttpGet httpget =
new HttpGet(path);
HttpResponse response = httpClient.execute(httpget);
if (response.getStatusLine().getStatusCode() ==
200) {
HttpEntity entity = response.getEntity();
result = EntityUtils.toByteArray(entity);
}
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (ParseException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static byte[]
httpURLConnLoadData(String path) {
BufferedInputStream bis =
null;
ByteArrayOutputStream baos =
new ByteArrayOutputStream();
try {
HttpURLConnection huc = (HttpURLConnection)
new URL(path).openConnection();
huc.setRequestMethod(
"GET");
huc.connect();
if (huc.getResponseCode() ==
200) {
bis =
new BufferedInputStream(huc.getInputStream());
int i = -
1;
byte[] bb =
new byte[
100];
while ((i = bis.read(bb)) != -
1) {
baos.write(bb,
0, i);
baos.flush();
}
bis.close();
return baos.toByteArray();
}
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void httpURLConnectionLoadData(String path) {
HttpURLConnection con =
null;
FileOutputStream fos =
null;
BufferedInputStream bis =
null;
try {
con = (HttpURLConnection)
new URL(path).openConnection();
con.setRequestMethod(
"GET");
con.setUseCaches(
false);
con.setConnectTimeout(
5000);
con.setReadTimeout(
5000);
con.setRequestProperty(
"Content-type",
"application/json");
con.setRequestProperty(
"Connection",
"Keep-Alive");
con.connect();
if (con.getResponseCode() ==
200) {
bis =
new BufferedInputStream(con.getInputStream());
fos =
new FileOutputStream(
new File(
"my-file.txt"));
int len = -
1;
byte[] bytes =
new byte[
1024];
while ((len = bis.read(bytes)) != -
1) {
fos.write(bytes,
0, len);
fos.flush();
}
}
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (bis !=
null) {
bis.close();
}
if (fos !=
null) {
fos.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
con.disconnect();
}
}