Simple Package for OKHTTP3
public class Okhttputil {
public static final String tag= "okhttputil";
private static Okhttpclient client;
private static Okhttputil httputil;
Private Okhttputil () {
Client=new Okhttpclient.builder ()
. ConnectTimeout (timeunit.seconds)
. ReadTimeout (10,timeunit.seconds)
. Build ();
}
Using singleton mode, only one instance of Okhttputil and Okhttpclient is Guaranteed.
public static Okhttputil getinstance () {
Synchronized (okhttputil.class) {
If (httputil==null) {
httputil= New Okhttputil ();
Return httputil;
}
}
Return httputil;
}
/**
* Get Asynchronous request
* @param URL
*/
public static void GetData (String Url) {
Request request=new request.builder (). URL (url)
. Build ();
Client.newcall (request) enqueue (new Callback () {
@Override
public void OnFailure (call call, ioexception E) {
}
@Override
public void Onresponse (call call, Response Response) throws IOException {
String type
LOG.I (TAG, "getData:" + response.body (). string ());
byte array type
LOG.I (TAG, "getData:" + response.body (). bytes ());
byte stream type
LOG.I (TAG, "getData:" + response.body (). bytestream ());
Character Stream type
LOG.I (TAG, "getData:" + response.body (). charstream ());
}
});
}
/**
* Get requests with multiple parameters
* @param URL
* @param map
*/
public void Getparams (String url,map<string,string> Map) {
String url1=url;
If (map!=null&&map.size () >0) {
For (String Key:map.keySet ()) {
url1+= "&" +key+ "=" +map.get (key);
}
}
GetData (url1);
}
/**
* Submit a single key value pair
* @param URL
* @param key
* @param value
*/
public void Postkeyvaluepaire (String url,string key,string Value) {
Formbody formbody=new Formbody.builder ()
. Add (key,value)
. Build ();
Request request=new Request.builder ()
. URL (url)
. Post (formbody)
. Build ();
Client.newcall (request) enqueue (new Callback () {
@Override
public void OnFailure (call call, ioexception E) {
}
@Override
public void Onresponse (call call, Response Response) throws IOException {
}
});
}
/**
* Submit multiple Key-value Pairs
*
* @param URL Submission Path
* @param map is used to place key value pairs, map key corresponding keys, value corresponding values
*/
public void Postmap (String url,map<string,string> Map) {
Formbody.builder formbody=new Formbody.builder ();
If (map!=null) {
For (String Key:map.keySet ()) {
Formbody.add (key,map.get (key));
}
}
Request request=new request.builder (). URL (url)
. Post (formbody.build ())
. Build ();
Client.newcall (request) enqueue (new Callback () {
@Override
public void OnFailure (call call, ioexception E) {
}
@Override
public void Onresponse (call call, Response Response) throws IOException {
}
});
}
/**
* Post submits JSON data
* @param URL
* @param content
*/
public void poststring (String url,string content) {
MediaType Mediatype=mediatype.parse ("application/json; Charset=utf-8 ");
Requestbody requestbody=requestbody.create (mediatype,content);
Request request=new Request.builder ()
. URL (url)
. Post (requestbody)
. Build ();
Client.newcall (request)
. enqueue (new Callback () {
@Override
public void OnFailure (call call, ioexception E) {
}
@Override
public void Onresponse (call call, Response Response) throws IOException {
}
});
}
/**
* Upload a single file
*/
public void UploadFile (String url, file File) {
MediaType Type=mediatype.parse (getmediatype (file.getname ()));//set The type of upload file
Requestbody requestbody=requestbody.create (type,file);//create Request body
Request request=new request.builder (). URL (url)//create request
. Post (requestbody)
. Build ();
Client.newcall (request) enqueue (new Callback () {
@Override
public void OnFailure (call call, ioexception E) {
}
@Override
public void Onresponse (call call, Response Response) throws IOException {
}
});
}
/**
* Upload Multiple files
*/
public void Uploadfiles (String url,file[] files) {
Multipartbody.builder builder=new multipartbody.builder (). setType (multipartbody.form);
For (int I=0;i<files.length;i++) {
Requestbody filebody=requestbody.create (mediatype.parse (getmediatype (files[i].getname ())), files[i]);
Builder.addformdatapart ("file", files[i].getname (), filebody);
}
Request request=new Request.builder ()
. URL (url)
. Post (builder.build ())
. Build ();
Client.newcall (request) enqueue (new Callback () {
@Override
public void OnFailure (call call, ioexception E) {
}
@Override
public void Onresponse (call call, Response Response) throws IOException {
}
});
}
/**
* Upload multiple files and parameters
*/
public void Uplaodmultifiles (String url,file[] files,map<string,string> Map) {
Multipartbody.builder builder=new multipartbody.builder (). setType (multipartbody.form);
Add File
If (files!=null&&files.length>0) {
For (int I=0;i<files.length;i++) {
Builder.addformdatapart ("uploadfile", files[i].getname (), requestbody.create (mediatype.parse (files[i].getName ()) , files[i]));
}
}
Adding parameters
If (map!=null&&map.size () >0) {
For (String Key:map.keySet ()) {
Builder.addformdatapart (key,map.get (key));
}
}
Request request=new request.builder (). URL (url)
. Post (builder.build ())
. Build ();
Client.newcall (request) enqueue (new Callback () {
@Override
public void OnFailure (call call, ioexception E) {
}
@Override
public void Onresponse (call call, Response Response) throws IOException {
}
});
}
/**
* Determine the mediatype of the file according to the file name
* @param fileName
* @return
*/
private string Getmediatype (string FileName) {
fileNameMap map= Urlconnection.getfilenamemap ();
String contenttypefor=map.getcontenttypefor (fileName);
If (contenttypefor==null) {
Contenttypefor= "applicationn/octet-stream";
}
Return contenttypefor;
}
/**
* Set the request header
* @param headersparams
* @return
*/
Private Headers setheaders (map<string, string> Headersparams) {
Headers headers=null;
okhttp3. Headers.builder headersbuilder=new okhttp3. Headers.builder ();
If (headersparams! = Null)
{
iterator<string> Iterator = Headersparams.keyset (). Iterator ();
String key = "";
While (iterator.hasnext ()) {
Key = Iterator.next (). toString ();
Headersbuilder.add (key, Headersparams.get (key));
LOG.D ("get http", "get_headers===" +key+ "= = =" +headersparams.get (key));
}
}
Headers=headersbuilder.build ();
Return headers;
}
/**
* Get method joins the spelling parameter
* @param mapparams
* @return
*/
Private String seturlparams (map<string, string> Mapparams) {
String Strparams = "";
If (mapparams! = Null) {
iterator<string> Iterator = Mapparams.keyset (). Iterator ();
String key = "";
While (iterator.hasnext ()) {
Key = Iterator.next (). toString ();
Strparams + = "&" + key + "=" + mapparams.get (key);
}
}
Return strparams;
}
}
Simple use of OKHTTP3 (ii)