需要引入的包
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telecom.Connection;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
需要用到的java代码
public class Http {
public String post(String url1, String data) {
try {
URL url = new URL(url1);
HttpURLConnection Connection = (HttpURLConnection) url.openConnection();//创建连接
Connection.setRequestMethod("POST");
Connection.setConnectTimeout(3000);
Connection.setReadTimeout(3000);
Connection.setDoInput(true);
Connection.setDoOutput(true);
Connection.setUseCaches(false);
Connection.connect();
DataOutputStream dos = new DataOutputStream(Connection.getOutputStream());
String title = data;//这里是POST请求需要的参数字符串类型,例如"id=1&data=2"
dos.write(title.getBytes());
dos.flush();
dos.close();//写完记得关闭
int responseCode = Connection.getResponseCode();
if (responseCode == Connection.HTTP_OK) {//判断请求是否成功
InputStream inputStream = Connection.getInputStream();
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int length = 0;
while ((length = inputStream.read(bytes)) != -1) {
arrayOutputStream.write(bytes, 0, length);
arrayOutputStream.flush();
}//读取响应体的内容
String s = arrayOutputStream.toString();
return s;//返回请求到的内容,字符串形式
} else {
return "-1";//如果请求失败返回-1
}
} catch (Exception e) {
return "-1";//出现异常也返回-1
}
}
public String get(String url1) {
try {
URL url = new URL(url1);
HttpURLConnection Connection = (HttpURLConnection) url.openConnection();
Connection.setRequestMethod("GET");
Connection.setConnectTimeout(3000);
Connection.setReadTimeout(3000);
int responseCode = Connection.getResponseCode();
if (responseCode == Connection.HTTP_OK) {
InputStream inputStream = Connection.getInputStream();
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int length = 0;
while ((length = inputStream.read(bytes)) != -1) {
arrayOutputStream.write(bytes, 0, length);
arrayOutputStream.flush();//强制释放缓冲区
}
String s = arrayOutputStream.toString();
return s;
} else {
return "-1";
}
} catch (Exception e) {
return "-1";
}
}
public static boolean 检查网络状态(Context context) {
NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected() && activeNetworkInfo.getState() == NetworkInfo.State.CONNECTED;
}
public static boolean 网络可用性(Context context) {
if (context == null) {
return false;
}
try {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
if (connectivityManager != null) {
return connectivityManager.getActiveNetworkInfo().isAvailable();
}
return false;
} catch (Exception e) {
return false;
}
}
}
//使用时创建Http类
Http _http = new Http();
//get提交
String _return = _http.get(链接);
//post提交
String _return = _http.post(链接,参数);
//另外还有两个方法
//检测网络是否能用
_http.网络可用性();
//检测网络状态
_http.检测网络状态();