android開發http請求POST&GET封裝工具
阿新 • • 發佈:2019-01-03
最近一直想著封裝個網路請求框架,但是看到有很多部落格有封裝而且還不錯,於是就想著封裝一個安卓帶的,以便自己以後使用.
一,請求成功和失敗回撥介面
public interface OnResponseListner {
void onSucess(String response);
void onError(String error);
}
二,post&get請求方法工具類
public class HttpUtlis {
/**
*get請求封裝
*/
public static void getRequest(String url, Map<String,String> params, String encode,OnResponseListner listner) {
StringBuffer sb = new StringBuffer(url);
sb.append("?");
if (params!=null && !params.isEmpty()){
for (Map.Entry<String,String> entry:params.entrySet()) { //增強for遍歷迴圈新增拼接請求內容
sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
sb.deleteCharAt(sb.length()-1 );
if (listner!=null) {
try {
URL path = new URL(sb.toString());
if (path!=null) {
HttpURLConnection con = (HttpURLConnection) path.openConnection();
con.setRequestMethod("GET"); //設定請求方式
con.setConnectTimeout(3000); //連結超時3秒
con.setDoOutput(true);
con.setDoInput(true);
OutputStream os = con.getOutputStream();
os.write(sb.toString().getBytes(encode));
os.close();
if (con.getResponseCode() == 200) { //應答碼200表示請求成功
onSucessResopond(encode, listner, con);
}
}
} catch (Exception error) {
error.printStackTrace();
onError(listner, error);
}
}
}
}
/**
* POST請求
*/
public static void postRequest(String url,Map<String,String> params,String encode,OnResponseListner listner){
StringBuffer sb = new StringBuffer();
if (params!=null && !params.isEmpty()){
for (Map.Entry<String,String> entry: params.entrySet()) {
sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
sb.deleteCharAt(sb.length()-1);
}
if (listner!=null) {
try {
URL path = new URL(url);
if (path!=null){
HttpURLConnection con = (HttpURLConnection) path.openConnection();
con.setRequestMethod("POST"); //設定請求方法POST
con.setConnectTimeout(3000);
con.setDoOutput(true);
con.setDoInput(true);
byte[] bytes = sb.toString().getBytes();
OutputStream outputStream = con.getOutputStream();
outputStream.write(bytes);
outputStream.close();
if (con.getResponseCode()==200){
onSucessResopond(encode, listner, con);
}
}
} catch (Exception e) {
e.printStackTrace();
onError(listner, e);
}
}
}
private static void onError(OnResponseListner listner,Exception onError) {
listner.onError(onError.toString());
}
private static void onSucessResopond(String encode, OnResponseListner listner, HttpURLConnection con) throws IOException {
InputStream inputStream = con.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();//建立記憶體輸出流
int len = 0;
byte[] bytes = new byte[1024];
if (inputStream != null) {
while ((len = inputStream.read(bytes)) != -1) {
baos.write(bytes, 0, len);
}
String str = new String(baos.toByteArray(), encode);
listner.onSucess(str);
}
}
}
三,activity中使用直接類名靜態方法呼叫
String url="";
String encode="utf-8";
public void GET(View view){
Map<String,String> map=new HashMap<>();
map.put("userName","");
map.put("pwd","");
HttpUtlis.getRequest(url, map,encode, new OnResponseListner() {
@Override
public void onSucess(String response) {
}
@Override
public void onError(String error) {
}
});
}