美文网首页
如何使用retrofit去请求网络

如何使用retrofit去请求网络

作者: nickgao | 来源:发表于2019-01-18 18:53 被阅读0次

    引入:

    <pre class="brush:xml;toolbar:false" style="margin: 0.5em 0px; padding: 0.4em 0.6em; line-height: 1.5; border-radius: 8px; background: rgb(248, 248, 248); color: rgb(28, 31, 33); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">compile 'com.google.code.gson:gson:2.8.0'
    compile 'com.squareup.retrofit2:retrofit:2.4.0'
    compile 'com.squareup.retrofit2:converter-gson:2.4.0'
    </pre>

    因为retrofit已经包含了okhttp的库,所以就不要额外引入okhttp了

    我们使用retrofit来请求:http://139.199.89.89/api/v1/books这样一个API

    <pre class="brush:java;toolbar:false" style="margin: 0.5em 0px; padding: 0.4em 0.6em; line-height: 1.5; border-radius: 8px; background: rgb(248, 248, 248); color: rgb(28, 31, 33); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">private void useRetrofit() {
    Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("http://139.199.89.89") //设置网络请求的Url地址
    .addConverterFactory(GsonConverterFactory.create()) //设置数据解析器
    .build();
    BookService service = retrofit.create(BookService.class);
    Call<BookResponse> call = service.getResult();

    //3.发送请求
    call.enqueue(new Callback<BookResponse>() {
        @Override
        public void onResponse(Call<BookResponse> call, Response<BookResponse> response) {
            Log.d(TAG,"<<<<<response="+response);
        }
    
        @Override
        public void onFailure(Call<BookResponse> call, Throwable t) {
    
        }
    });
    

    }
    </pre>

    我们要写一个service,叫BookService:

    <pre class="brush:java;toolbar:false" style="margin: 0.5em 0px; padding: 0.4em 0.6em; line-height: 1.5; border-radius: 8px; background: rgb(248, 248, 248); color: rgb(28, 31, 33); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">package com.pic.optimize.http;

    import retrofit2.Call;
    import retrofit2.http.GET;

    public interface BookService {
    @GET("/api/v1/books")
    Call<BookResponse> getResult();
    }
    </pre>

    然后要写json的解析实体BookResponse:

    <pre class="brush:java;toolbar:false" style="margin: 0.5em 0px; padding: 0.4em 0.6em; line-height: 1.5; border-radius: 8px; background: rgb(248, 248, 248); color: rgb(28, 31, 33); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import java.util.ArrayList;

    public class BookResponse {
    public ArrayList<Book> data;
    public int status;
    public String message;
    }
    </pre>

    然后就在onResponse中就可以得到回调了

    我们再写一个添加一本书的post的请求,url是http://139.199.89.89,服务端接收参数是

    bookName和bookDescription:

    <pre class="brush:java;toolbar:false" style="margin: 0.5em 0px; padding: 0.4em 0.6em; line-height: 1.5; border-radius: 8px; background: rgb(248, 248, 248); color: rgb(28, 31, 33); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import retrofit2.Call;
    import retrofit2.http.Field;
    import retrofit2.http.FormUrlEncoded;
    import retrofit2.http.POST;

    public interface BookPostService {
    @POST("/api/v1/books")
    @FormUrlEncoded
    Call<BookResponse> getResult(@Field("bookName") String name, @Field("bookDescription") String description);
    }
    </pre>

    <pre class="brush:java;toolbar:false" style="margin: 0.5em 0px; padding: 0.4em 0.6em; line-height: 1.5; border-radius: 8px; background: rgb(248, 248, 248); color: rgb(28, 31, 33); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">private void useRetrofit() {
    Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("http://139.199.89.89") //设置网络请求的Url地址
    .addConverterFactory(GsonConverterFactory.create()) //设置数据解析器
    .build();
    BookPostService service = retrofit.create(BookPostService.class);
    Call<BookResponse> call = service.getResult(mBookNameEdit.getText().toString(),mDescriptionEdit.getText().toString());

    //3.发送请求
    call.enqueue(new Callback<BookResponse>() {
        @Override
        public void onResponse(Call<BookResponse> call, Response<BookResponse> response) {
            Log.d(TAG,"<<<<<response="+response);
        }
    
        @Override
        public void onFailure(Call<BookResponse> call, Throwable t) {
    
        }
    });
    

    }
    </pre>

    相关文章

      网友评论

          本文标题:如何使用retrofit去请求网络

          本文链接:https://www.haomeiwen.com/subject/vdcodqtx.html