权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
依赖
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
implementation project(':xlistviewlibrary')
工具类
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;
import java.net.URLConnection;
/**
* Created by liqiang on 2017/11/2.
*/
public class Utils {
/**
* 获取手机网络状态
* -1 :无网络
* 0 : 蜂窝网络;
* 1 : wifi网络
*
* @param context 上下文对象
* @return
*/
public static int getNetType(Context context) {
int mState = -1;// 代表无网络
//获取android系统提供的服务, 转换成链接管理类,这个类专门处理链接相关的东西
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//NetworkInfo封装了网络链接的信息
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetworkInfo == null) {
return mState;
}
int type = activeNetworkInfo.getType();
if (type == ConnectivityManager.TYPE_WIFI) {//代表现在是wifi网络
mState = 1;
} else if (type == ConnectivityManager.TYPE_MOBILE) {
mState = 0;
}
return mState;
}
private static String tag = "TAG";
public static String getNetJson(String urlString) {
try {
//url对象封装接口字符串
URL url = new URL(urlString);
//用url打开连接, 返回值我们用HttpURLConnection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(8000);//设置链接超时时间
int responseCode = urlConnection.getResponseCode(); //获取状态码
if (responseCode == 200) {
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//可拼接的字符串
StringBuilder stringBuilder = new StringBuilder();
String temp = "";
while ((temp = bufferedReader.readLine()) != null) {
stringBuilder.append(temp);
}
String jsonString = stringBuilder.toString();
return jsonString;
} else {
}
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e(tag, "getNetJson: " + e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.e(tag, "getNetJson: " + e.toString());
}
return "";
}
/**
* 获取网络图片
*
* @param urlString
* @return
*/
public static Bitmap getNetBitmap(String urlString) {
try {
URL url = new URL(urlString);
HttpURLConnection httpurlConnection = (HttpURLConnection) url.openConnection();
httpurlConnection.setConnectTimeout(8000);//设置链接超时时间
int responseCode = httpurlConnection.getResponseCode();
if (responseCode == 200) {
InputStream inputStream = httpurlConnection.getInputStream();
//BitmapFactory: android提供好的加工图片的工具类,
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
} else {
//do nothing
}
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e(tag, "getNetBitmap: " + e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.e(tag, "getNetBitmap: " + e.toString());
}
return null;
}
public static String getNetClient(String urlString) {
//得到httpclient对象
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
//封装get请求
HttpGet httpGet = new HttpGet(urlString);
try {
//获取返回的response
HttpResponse response = defaultHttpClient.execute(httpGet);
//获取状态码
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
String json = EntityUtils.toString(entity);
Log.e("httpclient", "run:----- "+json );
return json;
}else {
}
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}