You are just beginning to become familiar with retrofit and are swapping the network framework in your project for retrofit.
Add Dependencies:
Compile ' com.squareup.retrofit2:retrofit:2.1.0 '//retrofit2 required packages
compile ' com.squareup.retrofit2: converter-gson:2.1.0 '//converterfactory's Gson dependency pack
Retrofit wrapper class for network interface services
public class Retrofitwrapper {
private static retrofitwrapper retrofitwrapper;
Private retrofit retrofit;
Private Retrofitwrapper () {
retrofit = new Retrofit.builder (). BaseURL (Appconfig.retrofit_url)
. Addconverterfactory (Gsonconverterfactory.create ())
. Build ();
/**
* Single-row mode
/public static Retrofitwrapper Getwrapperinstance () {
if (Retrofitwrapper = = null) {
synchronized (retrofitwrapper.class) {
if (Retrofitwrapper = null) {
retrofitwrapper = new Retrofitwrapper ();
}} return retrofitwrapper;
}
Public <T> T Create (final class<t> service) {return
retrofit.create (service);
}
}
To define an interface:
Public interface Retrofitapi {
/**
* Login
/@POST ("Servroot")
call<loginbean> Dologinresult (@QueryMap map<string, string> Map);
Through BaseURL (Appconfig.retrofit_url) + @POST ("Servroot")
Stitching into full request URL: "Http://www.xxxx.cn/servRoot"
The portal to initiate the request:
public class Retrofitmodel {
private retrofitapi retrofitapi;
private static Retrofitmodel Retrofitmodel;
/**
* Single-row mode
/public static Retrofitmodel Getmodelinstance () {
if (Retrofitmodel = = null) {
Retrofitmodel = new Retrofitmodel ();
}
return retrofitmodel;
}
Private Retrofitmodel () {
Retrofitapi = retrofitwrapper.getwrapperinstance (). Create (Retrofitapi.class);
/**
* Login
/public call<loginbean> login (map<string,string> Map) {
call< loginbean> Logincall = retrofitapi.dologinresult (map);
Return Logincall
}
}
To initiate a logon request:
Private Retrofitmodel Retrofitmodel;
Retrofitmodel = Retrofitmodel.getmodelinstance (); /** * Use retrofit login/public void Retrofitlogin () {map<string, string> Map = new hashmap<&
gt; ();
Map.put ("user_name", username);
Map.put ("User_pwd", Userpass);
Map.put ("ClassName", "Com.ycya.service.UserInfoService");
Map.put ("methodname", "Login"); The generics here I already know the structure of the returned data//If you do not know, you can define generics as:call<responsebody>//And then Responsebody.body () after the request succeeds. Strin
g () View structure to parse call<loginbean> call = Retrofitmodel.login (map); Call.enqueue (New callback<loginbean> () {@Override public void Onresponse (call<loginbean& Gt
Call, response<loginbean> Response) {if (response.issuccessful ()) {//get returned generic data
Loginbean Loginbean = Response.body ();
}} @Override public void OnFailure (call<loginbean> call, Throwable t) {}}); }
Upload a single picture
Refer to a lot of online methods can not be transmitted to the background of the empty back to find the official document copy instance code directly OK
To define an interface:
In Retrofitapi:
/**
* Accident upload scene Picture
*
* ID pathfile These two backstage need
* Last that need to pass the picture * * * *
@Multipart
@POST (" Imgupload ")
call<responsebody> uploadimg (@Part (" id ") Requestbody ID,
@Part (" Pathfile ") requestbody Pathfile,
@Part multipartbody.part img);
In Retrofitmodel:
/**
* Accident upload in the scene picture
/Public call<responsebody> uploadimg (requestbody ID, requestbody pathfile, Multipartbody.part img) {
call<responsebody> call=retrofitapi.uploadimg (id,pathfile,img);
return call;
Upload:
/** * Upload Scene picture */public void UploadImg1 (String path) {//id requestbody ID =requestbody.c
Reate (Mediatype.parse ("Multipart/form-data"), "0"); Pathfile requestbody pathfile =requestbody.create (Mediatype.parse ("Multipart/form-data"), "filestore/uploadimg")
;
1, according to the address to get file File = new file (path); 2. Create Requestbody, where ' multipart/form-data ' is encoded type requestbody RequestFile = requestbody.create (Mediatype.parse ("Multi
Part/form-data "), file); 3, create ' Multipartbody.part ', which need to note that the first parameter ' filename ' needs to correspond to the server, that is, ' key ' multipartbody.part part = MultipartBody.Part.crea
Teformdata ("filename", File.getname (), requestfile);
Call<responsebody> call = Retrofitmodel.uploadimg (Id,pathfile, part); Call.enqueue (New callback<responsebody> () {@Override public void Onresponse (call<response Body> call, response<responsebody> Response) {if (responSe.issuccessful ()) {try {klog.e (Response.body (). String ());
catch (IOException e) {e.printstacktrace (); @Override public void OnFailure (call<responsebody> call, T
Hrowable t) {klog.e ("onfailure");
}
}); }
There are other methods have not been tried, the back if you try, OK will come up.
A document (a lot of examples): Https://futurestud.io/tutorials/retrofit-2-basics-of-api-description
Reference: http://blog.csdn.net/u011974987/article/details/50895633