package com.example.viewpager_homework_tools;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
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 android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class HttpUtils {
/**
* 判断获取网络状态是否联通
* @param context
* @return
*/
public static boolean isNetWorkConn(Context context){
boolean bl=false;
ConnectivityManager manager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info=manager.getActiveNetworkInfo();
if(info!=null){
return info.isConnected();
}
return bl;
}
public static String getJsonContent(String url){
String result = "";
URL url2;
InputStream is;
InputStreamReader isr;
BufferedReader br;
try {
url2 = new URL(url);
HttpURLConnection connection =
(HttpURLConnection)url2.openConnection();
is = connection.getInputStream();
isr = new InputStreamReader(is,"utf-8");
String line = "";
br = new BufferedReader(isr);
while ((line=br.readLine())!=null) {
result+=line;
}
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static String getJsonContent2(String url){
String result = "";
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = null;
try {
response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode()==200) {
result = EntityUtils.toString(response.getEntity(), "utf-8");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static byte[] getByteContent2(String url){
byte []data = null;
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = null;
try {
response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode()==200) {
data = EntityUtils.toByteArray(response.getEntity());
}
return data;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
public static byte[] getByteContent(String url){
try {
URL url2 = new URL(url);
HttpURLConnection httpConn = (HttpURLConnection) url2
.openConnection();
httpConn.setDoInput(true); //允许输入内容
httpConn.setDoOutput(false);
httpConn.connect();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (httpConn.getResponseCode()==200) {
BufferedInputStream bis = new BufferedInputStream(httpConn.getInputStream());
byte[] buffer = new byte[8*1024];
int c = 0;
while ((c=bis.read(buffer))!=-1) {
baos.write(buffer, 0, c);
baos.flush();
}
byte[] data = baos.toByteArray();
return data;
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
Android 联网获取普通数据 和 JSON 数据工具类
最新推荐文章于 2018-12-24 23:46:04 发布