我的工作类 :MyNewWork
public class MyNewWork {
//网络获取Json
public static String getUrlString(String urlString){
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
//获取请求码
int responseCode = urlConnection.getResponseCode();
//判断请求吗是否==200
if (responseCode == 200) {
InputStream inputStream = urlConnection.getInputStream();
//按行读取
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//
String qq="";
String ww="";
//
while((qq=bufferedReader.readLine())!=null){
ww+=qq;
}
//返回数据
return ww;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
//判断是否有网
/*public static boolean isConn(Context context){
boolean bisConnFlag=false;
ConnectivityManager conManager = (ConnectivityManager)context.getSystemService(Contextext.CONNECTIVITY_SERVICE);
NetworkInfo network = conManager.getActiveNetworkInfo();
if(network!=null){
bisConnFlag = network.isAvailable();
}
return bisConnFlag;
}
*/
public static boolean isConn(Context context){
Boolean bisconnFiag = false;
ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetworkInfo != null) {
bisconnFiag = activeNetworkInfo.isConnected();
}
return bisconnFiag;
}
//网络图片
public static Bitmap getBitMap(String mUrl){
try {
URL url = new URL(mUrl);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
int responseCode = urlConnection.getResponseCode();
if (responseCode == 200) {
InputStream inputStream = urlConnection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}else{
Log.e("gxq", "responseCode---tupian: "+responseCode );
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
//网络获取Json
public static String getNetjson(String urlString) {
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");//若果是get请求可以不用配置; 其他请求必须配置
urlConnection.setConnectTimeout(8000);//设置链接超时间
InputStream inputStream = urlConnection.getInputStream();//获取网络返回的输入流;
//可拼接的字符串
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String temp = "";
while ((temp = bufferedReader.readLine()) != null) {
stringBuilder.append(temp);
temp = "";
}
//这个是网络获取的数据
String data = stringBuilder.toString();
Log.e(tag, "getData: " + data);
return data;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
//网络获取Json
public static String getUrlString(String urlString){
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
//获取请求码
int responseCode = urlConnection.getResponseCode();
//判断请求吗是否==200
if (responseCode == 200) {
InputStream inputStream = urlConnection.getInputStream();
//按长度读取
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//数组
byte[] bytes = new byte[1024];
//初始长度
int length = 0;
while ((length = stream.read(bytes)) != -1) {
byteArrayOutputStream.write(bytes, 0, length);
}
String s = byteArrayOutputStream.toString();
return s;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}