import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import com.xujun.busticketsystem.RegisterActivity;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class GetPostUtil {
private static String URL = "/";//填写服务器IP以及项目名称
/**
* 向指定的url发送post请求
*
* @param params 请求参数
* @return 远程响应
*/
public static String executeHttpPost(String target, String params) {
HttpURLConnection connection = null;
DataOutputStream out = null;
InputStream in = null;
URL realUrl = null;
try {
realUrl = new URL(URL+target);
//获得连接
connection = (HttpURLConnection) realUrl.openConnection();
//设置输入输出
connection.setDoInput(true);
connection.setDoOutput(true);
//设置客户端请求方式
connection.setRequestMethod("POST");
//设置响应时间,传递数据超过8s为超时
connection.setConnectTimeout(60*1000);
connection.setReadTimeout(60*1000);
//禁用POST请求使用缓存
connection.setUseCaches(false);
// 设置该HttpURLConnection实例是否自动执行重定向
connection.setInstanceFollowRedirects(true);
//设置浏览器原生form表单的提交方式
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Charset","utf-8");
connection.connect();
//获取输出流
out = new DataOutputStream(connection.getOutputStream());
//输出
if(params != null){
out.writeBytes(params);
}
//刷新输出流
out.flush();
out.close();
//获取响应代码
int responseCode = connection.getResponseCode();
if (HttpURLConnection.HTTP_OK == responseCode) {
//获取输入流
in = connection.getInputStream();
return parseInfo(in);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
//意外退出时,连接关闭保护
if (connection != null) {
connection.disconnect();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
//得到字节输入流,将字节输入流转成String类型
public static String parseInfo(InputStream inputStream) {
BufferedReader reader = null;
String line = "";
StringBuilder response = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(inputStream));
while ((line = reader.readLine()) != null) {
response.append(line);
}
return response.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* 向指定的url发送get请求
*
* @param params 请求参数
* @return 远程响应
*/
public static String executeHttpGet(String params, String target) {
HttpURLConnection connection = null;
InputStream in = null;
URL realUrl = null;
try {
//http地址
realUrl = new URL(URL + target + params);
connection = (HttpURLConnection) realUrl.openConnection();
//设置读取方式
connection.setRequestMethod("GET");
connection.setConnectTimeout(10000);//建立连接超时
connection.setReadTimeout(8000);//传递数据超时
//获取输入流
int responseCode = connection.getResponseCode();
if (HttpURLConnection.HTTP_OK == responseCode) {
//获取输入流
in = connection.getInputStream();
return parseInfo(in);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
return "读取数据异常";
} finally {
//意外退出时,关闭连接保护
if (connection != null) {
connection.disconnect();
;
}
if (in != null) {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return "无法连接服务器";
}
}
调用方法为
GetPostUtil.executeHttpGet(params,"LoginServlet");
GetPostUtil.executeHttpPost("register", params);//params为传递的数据
注:在做毕设时本人不会使用JSON所以直接使用字符串拼接的参数传递到服务器,请大佬们自行改成JSON数据