java retrofit是什么?让我们一起来了解一下吧!
java retrofit是Java在开发时的一种轻量级http请求部分组件,它的作用是能够像接口调用这种方法来进行http调用。Retrofit在它的前期主要是应用在安卓的网络请求框架,之后也被用于java。
更确定的说,Retrofit 是一个 restful 的 http 网络请求框架的封装。
1. App应用程序通过 Retrofit 请求网络,事实上是通过应用 Retrofit 接口层封装去获得参数、Header、Url 等信息,然后由 OkHttp 完成后面的请求动作。
2. 在服务端返回数据之后,OkHttp 把原先的结果过渡给 Retrofit,Retrofit按照用户的需求对结果进行分析。所以,网络请求的本质事实上依旧是由OkHttp完成的,retrofit只是帮使用者来进行工作简便化的,比如配置网络,处理数据等工作,提高这一系列操作的复用性。
实战演练,具体步骤如下:
package request.http.com.httprequest.activity; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.widget.ImageView; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.Unbinder; import okhttp3.OkHttpClient; import okhttp3.RequestBody; import okhttp3.logging.HttpLoggingInterceptor; import request.http.com.httprequest.R; import request.http.com.httprequest.adapter.RecyclerViewAdapter; import request.http.com.httprequest.api.RequestApi; import request.http.com.httprequest.bean.TokenInfoBean; import request.http.com.httprequest.bean.goodsBean; import request.http.com.httprequest.bean.goodsSuitBean; import request.http.com.httprequest.bean.singleBean; import request.http.com.httprequest.common.CommonFunc; import request.http.com.httprequest.common.Tools; import request.http.com.httprequest.common.retrofitCommon; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import java.util.ArrayList; import java.util.HashMap; public class MainActivity extends Activity { private Unbinder unbinder; @BindView(R.id.lv_showDetail) RecyclerView lv_showDetail; private ArrayList dataBeanList; private RecyclerViewAdapter adapter; private DefaultItemAnimator defaultItemAnimator; private RequestApi requestApi; private RequestBody body; private HashMap hashMap; private String authorizationCode; private String appKey; private singleBean singleBean; private TokenInfoBean tokenInfoBean; private ArrayList singleList; private goodsSuitBean goodsSuitBean; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); unbinder = ButterKnife.bind(this); initView(); } public void initRecyclerView(){ LinearLayoutManager manager = new LinearLayoutManager(this); lv_showDetail.setLayoutManager(manager); DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL); dividerItemDecoration.setDrawable(getApplicationContext().getDrawable(R.drawable.rv_item_devider)); lv_showDetail.addItemDecoration(dividerItemDecoration); adapter = new RecyclerViewAdapter(MainActivity.this, dataBeanList); lv_showDetail.setAdapter(adapter); adapter.notifyDataSetChanged(); } public void initView() { retrofitCommon retrofitCommon = new retrofitCommon(); authorizationCode = CommonFunc.getBasicAuthorizationCode("user","asdasdsd"); appKey = CommonFunc.getFinplexAppKey(); requestApi= retrofitCommon.retrofitRequestPost(“访问接口的地址”); body = retrofitCommon.getRequestBody("code","1232131312"); hashMap = retrofitCommon.getRequestParam(authorizationCode,appKey); requestApi.getGroupGoods(body,hashMap).enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { callSuccessResponse(response); } @Override public void onFailure(Call call, Throwable t) { callErrorResponse(t); } }); } private void callSuccessResponse( Response response){ dataBeanList = new ArrayList<>(); goodsBean goods = response.body(); goodsSuitBean = goods.getGoodsSuit(); singleList = goods.getDataList(); tokenInfoBean = goods.getTokenInfo(); for (int i=0;i< singleList.size();i++){ //处理需要显示的数据 } initRecyclerView(); } private void callErrorResponse(Throwable t){ Tools.i("error","error"+t.fillInStackTrace()); } //去掉base64前缀 public String removeBase64(String baseStr){ String base64Str = baseStr.substring(baseStr.indexOf(",")+1, baseStr.length()); return base64Str; } }
以上就是小编今天的分享了,希望可以帮助到大家。