package com.bwie.utils.xujiahui20180921;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import com.google.common.io.CharStreams;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
-
@author:HttpUtils 2018.9.21
-
网络请求工具类
*/
public class HttpUtils {
private HttpRequestListener httpRequestListener;public HttpUtils(HttpRequestListener httpRequestListener) {
this.httpRequestListener = httpRequestListener;
}
private void doHttp(String url, String type, String conn) {
MyAsyncTask myAsyncTask = new MyAsyncTask(url, type, conn);
myAsyncTask.execute(url);
}public void doGet(String url) {
doHttp(url, “GET”, “”);
}public void doPost(String url, String conn) {
doHttp(url, “POST”, conn);
}//MyasyncTask
private class MyAsyncTask extends AsyncTask<String, Integer, String> {
private String url;
private String type;
private String conn;public MyAsyncTask(String url, String type, String conn) { this.url = url; this.type = type; this.conn = conn; } @Override protected String doInBackground(String... strings) { String data = ""; try { URL murl = new URL(url); try { HttpURLConnection connection = (HttpURLConnection) murl.openConnection(); connection.setRequestMethod(type); connection.setConnectTimeout(1 * 5000); if ("POST".equals(type)) { connection.setDoOutput(true); connection.getOutputStream().write(conn.getBytes()); } int code = connection.getResponseCode(); if (code == HttpURLConnection.HTTP_OK) { InputStream inputStream = connection.getInputStream(); data = CharStreams.toString(new InputStreamReader(inputStream, "UTF-8")); Message message = new Message(); message.obj = data; message.what = 1000; handler.sendMessage(message); } else { data = "0"; } } catch (IOException e) { e.printStackTrace(); } } catch (MalformedURLException e) { e.printStackTrace(); } return data; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); }
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int what = msg.what;
if (what == 1000) {
String data = (String) msg.obj;
if (“0”.equals(data)) {
httpRequestListener.Filed(“请求失败”);
} else {
httpRequestListener.SuccessRequest(data);
}
}
}
};
/* //doHttp
private void doHttp(String url, String type, String con) {
MyAsyncTask myAsyncTask = new MyAsyncTask(url, type, con);
myAsyncTask.execute(url);
}//get
public void doGet(String url) {
doHttp(url, “GET”, “”);
}//post
public void doPost(String url, String con) {
doHttp(url, “POST”, con);
}//MyAsyncTask
private class MyAsyncTask extends AsyncTask<String, Integer, String> {
private String url;
private String type;
private String con;public MyAsyncTask(String url, String type, String con) { this.url = url; this.type = type; this.con = con; } @Override protected String doInBackground(String... strings) { String data = ""; try { URL murl = new URL(url); try { HttpURLConnection connection = (HttpURLConnection) murl.openConnection(); connection.setRequestMethod(type); connection.setConnectTimeout(2000); if ("POST".equals(type)) { connection.setDoOutput(true); connection.getOutputStream().write(con.getBytes()); } int code = connection.getResponseCode(); if (code == HttpURLConnection.HTTP_OK) { InputStream inputStream = connection.getInputStream(); data = CharStreams.toString(new InputStreamReader(inputStream, "UTF-8")); Message message = new Message(); message.obj = data; message.what = 1000; handler.sendMessage(message); } else { data = "0"; } } catch (IOException e) { e.printStackTrace(); } } catch (MalformedURLException e) { e.printStackTrace(); } return data; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); }
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int what = msg.what;
if (what == 1000) {
String data = (String) msg.obj;
if (data.equals(0)) {
httpRequestListener.SuccessRequest(data);
} else {
httpRequestListener.Filed(“请求失败”);
}} }
};*/
}