Okhttp的简单使用 及其封装
导包:
compile 'com.squareup.okhttp3:okhttp:3.8.1'
public class NetUtils {
static final OkHttpClient client = new OkHttpClient();
public static void get(String url, HashMap< String , String > map , final OnNet getter) {
final String urls = getUrl(url, map );
new Thread (new Runnable() {
@Override
public void run() {
Request request = new Request. Builder(). url(urls). build();
Response response = null ;
try {
response = client. newCall(request). execute();
if (response. isSuccessful()){
getter. onSuccessed(response. body(). string ());
}else {
getter. onFailed(response. body(). string ());
}
} catch (IOException e) {
getter. onFailed(e. getCause()+ "" );
}
}
}). start();
}
public static String getUrl(String url,HashMap map ){
if (map == null || map . size()== 0 ){
return url;
}
StringBuilder sb= new StringBuilder();
sb. append(url);
sb. append("?" );
Iterator iterator = map . entrySet(). iterator();
while (iterator. hasNext()){
Map . Entry entry = (Map . Entry) iterator. next();
String key = (String ) entry. getKey();
String value = (String ) entry. getValue();
sb. append(key);
sb. append("=" );
sb. append(value);
sb. append("&" );
}
url = sb. substring(0 , sb. length() - 1 );
return url;
}
public static void post(final String url, final HashMap< String , String > map , final OnNet poster){
if (map == null || map . size()== 0 ){
get(url,null ,poster);
}
new Thread (new Runnable() {
@Override
public void run() {
FormBody. Builder builder = new FormBody. Builder();
for(String key:map . keySet()){
String value = map . get(key);
builder. add(key,value);
}
FormBody body = builder. build();
Request request = new Request. Builder(). url(url). post(body). build();
Response response = null ;
try {
response = client. newCall(request). execute();
if (response. isSuccessful()){
poster. onSuccessed(response. body(). string ());
}else {
poster. onFailed(response. body(). string ());
}
} catch (IOException e) {
poster. onFailed(e. getCause()+ "" );
}
}
}). start();
}
}
原生网络请求的基本使用及其封装
public class NetUtils {
public static String doPost(String urlStr, HashMap< String , String > map ) {
if (map == null ){
map = new HashMap< String ,String > ();
}
try {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url. openConnection();
String cache = parseParams(map );
conn. setDoOutput(true );
conn. getOutputStream(). write(cache . getBytes());
if (conn. getResponseCode()== 200 ){
InputStream is = conn. getInputStream();
BufferedReader bs = new BufferedReader(new InputStreamReader(is));
return bs. readLine();
}
} catch (MalformedURLException e) {
Log . v("meee" , "url格式异常" );
} catch (IOException e) {
Log . v("meee" , "请求中Io异常" );
}
return "" ;
}
public static String doGet(String urlStr, HashMap< String , String > map ) {
if (map == null ){
map = new HashMap< String ,String > ();
}
String cache = parseParams(map );
try {
URL url = new URL(urlStr+ "?" + cache );
HttpURLConnection conn = (HttpURLConnection) url. openConnection();
conn. setRequestMethod("GET" );
if (conn. getResponseCode()== 200 ){
InputStream is = conn. getInputStream();
BufferedReader bs = new BufferedReader(new InputStreamReader(is));
return bs. readLine();
}
} catch (MalformedURLException e) {
Log . v("meee" , "url格式异常" );
} catch (IOException e) {
Log . v("meee" , "请求中Io异常" );
}
return "" ;
}
@NonNull
private static String parseParams(HashMap< String , String > map ) {
String cache = "" ;
if (map . isEmpty()|| map == null ){
return "" ;
}
for (Map . Entry< String , String > kv : map . entrySet()) {
cache += kv. getKey() + "=" + kv. getValue() + "&" ;
}
cache = cache . substring(0 , cache . length() - 1 );
return cache ;
}
}