网络请求工具类_苏苏的小弟子
来源:互联网 发布:淘宝情趣用品 知乎 编辑:程序博客网 时间:2024/04/28 09:35
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* author:Ankey
* E-mail:none
* Date:2017-11-08
*/
public class NetUtil {
private BufferedReader br;
// 获取当前网络状态
public int getNetState(Context context) {
int stateType = -1;
if (context != null) {
// 获取系统服务管理类
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
// 获取网络活动信息对象
NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
if (activeNetworkInfo != null) {
/**
* stateType:
* -1 无网络
* TYPE_WIFI = 1;
* TYPE_MOBILE = 0;
*/
stateType = activeNetworkInfo.getType();
} else {
// nothing
}
}
return stateType;
}
// 通过
public Bitmap getBitmapByNet(String urlStr) {
HttpURLConnection urlConnection = null;
InputStream inputStream =null;
// 判断参数是否为空
if (urlStr == null || urlStr.trim() == "") {
Log.e("NetUtil", "getJsonByClient()参数为空 54行");
return null;
}
try {
URL url = new URL(urlStr);
urlConnection = (HttpURLConnection) url.openConnection();
// 打开网络连接
urlConnection = (HttpURLConnection) url.openConnection();
// 设置读取超时时间
urlConnection.setReadTimeout(8000);
// 设置连接超时时间
urlConnection.setConnectTimeout(8000);
// 获取响应码
int responseCode = urlConnection.getResponseCode();
if (responseCode == 200) {// 请求成功
// 获取字节流
inputStream = urlConnection.getInputStream();
// 通过Bitam工厂解析字节流转为Bitmap
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
Log.i("TAG","inputStream.close() 已经关闭了");
if(inputStream!=null)inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public String getJsonByUrlConnection(String urlStr) {
HttpURLConnection urlConnection = null;
// 判断参数是否为空
if (urlStr == null || urlStr.trim() == "") {
Log.e("NetUtil", "getJsonByClient()参数为空");
return null;
}
// 声明一个容器接收读取的数据
StringBuffer sb = new StringBuffer();
try {
// 初始化URL
URL url = new URL(urlStr);
// 打开网络连接
urlConnection = (HttpURLConnection) url.openConnection();
// 设置读取超时时间
urlConnection.setReadTimeout(8000);
// 设置连接超时时间
urlConnection.setConnectTimeout(8000);
// 获取响应码
int responseCode = urlConnection.getResponseCode();
if (responseCode == 200) {// 请求成功
// 获取字节流
InputStream inputStream = urlConnection.getInputStream();
// 将字节输入流转成高效流
br = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
return sb.toString().trim();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放资源
try {
if (br != null) br.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (urlConnection != null) urlConnection.connect();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public String getJsonByClient(String urlStr){
/*
* http://api.expoon.com/AppNews/getNewsList/type/1/p/1
*/
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(urlStr);
try {
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
if(entity!=null) {
return EntityUtils.toString(entity);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* author:Ankey
* E-mail:none
* Date:2017-11-08
*/
public class NetUtil {
private BufferedReader br;
// 获取当前网络状态
public int getNetState(Context context) {
int stateType = -1;
if (context != null) {
// 获取系统服务管理类
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
// 获取网络活动信息对象
NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
if (activeNetworkInfo != null) {
/**
* stateType:
* -1 无网络
* TYPE_WIFI = 1;
* TYPE_MOBILE = 0;
*/
stateType = activeNetworkInfo.getType();
} else {
// nothing
}
}
return stateType;
}
// 通过
public Bitmap getBitmapByNet(String urlStr) {
HttpURLConnection urlConnection = null;
InputStream inputStream =null;
// 判断参数是否为空
if (urlStr == null || urlStr.trim() == "") {
Log.e("NetUtil", "getJsonByClient()参数为空 54行");
return null;
}
try {
URL url = new URL(urlStr);
urlConnection = (HttpURLConnection) url.openConnection();
// 打开网络连接
urlConnection = (HttpURLConnection) url.openConnection();
// 设置读取超时时间
urlConnection.setReadTimeout(8000);
// 设置连接超时时间
urlConnection.setConnectTimeout(8000);
// 获取响应码
int responseCode = urlConnection.getResponseCode();
if (responseCode == 200) {// 请求成功
// 获取字节流
inputStream = urlConnection.getInputStream();
// 通过Bitam工厂解析字节流转为Bitmap
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
Log.i("TAG","inputStream.close() 已经关闭了");
if(inputStream!=null)inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public String getJsonByUrlConnection(String urlStr) {
HttpURLConnection urlConnection = null;
// 判断参数是否为空
if (urlStr == null || urlStr.trim() == "") {
Log.e("NetUtil", "getJsonByClient()参数为空");
return null;
}
// 声明一个容器接收读取的数据
StringBuffer sb = new StringBuffer();
try {
// 初始化URL
URL url = new URL(urlStr);
// 打开网络连接
urlConnection = (HttpURLConnection) url.openConnection();
// 设置读取超时时间
urlConnection.setReadTimeout(8000);
// 设置连接超时时间
urlConnection.setConnectTimeout(8000);
// 获取响应码
int responseCode = urlConnection.getResponseCode();
if (responseCode == 200) {// 请求成功
// 获取字节流
InputStream inputStream = urlConnection.getInputStream();
// 将字节输入流转成高效流
br = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
return sb.toString().trim();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放资源
try {
if (br != null) br.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (urlConnection != null) urlConnection.connect();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public String getJsonByClient(String urlStr){
/*
* http://api.expoon.com/AppNews/getNewsList/type/1/p/1
*/
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(urlStr);
try {
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
if(entity!=null) {
return EntityUtils.toString(entity);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
阅读全文
0 0
- 网络请求工具类_苏苏的小弟子
- android开发 获取网络状态---苏苏的小弟子
- ImageLoader----苏苏的小弟子
- AsycnTask的使用----苏苏的小弟子
- 互联网、因特网、万维网----苏苏的小弟子
- 网络请求的工具类
- 请求网络的工具类
- 使用事务通过show/hide切换Fragment(代码片段)----苏苏的小弟子
- 获取蓝牙开关状态以及开关蓝牙----苏苏的小弟子
- 网络请求工具类
- 网络请求工具类
- 网络请求工具类
- 网络请求工具类
- 网络请求工具类
- 04网络请求工具类的编写
- 工具类:处理网络请求的回应
- Swift的JFNetworkTool网络请求工具类
- 请求网络接口的工具类
- 将html页改成jsp的两种方式
- 全能电子地图下载器 离线地图 介绍(支持几十种地图下载) 最新版 3.6 (2015.6.13)
- 如果看了此文你还不懂傅里叶变换,那就过来掐死我吧【完整版】
- 亡命逃窜 搜索
- 制作百度地图离线JavaScript API加载本地瓦片地图
- 网络请求工具类_苏苏的小弟子
- HDU2516 取石子游戏(斐波那契博弈)
- 桥接模式
- 方向导数和梯度
- exp 导出加where 条件
- Android 开发权限注册
- 坦克大战 搜索
- 【java基础:递归】递归recursion!
- theano在windows下安装的若干问题