It's so hard, summer is here, and so does my cold, and I feel like I'm completely out of shape these two days.
Retrofit and okhttp come from Square, and retrofit is a layer of encapsulation of okhttp. All network requests are handed over to Okhttp, we only need to use retrofit to make network requests through simple configuration
Don't talk too much nonsense, go directly to the code
The first step is to add dependencies and permissions (the sparrow is small but has all the internal organs, don’t underestimate these two steps, sometimes it will take away your job)
rely:
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'//Retrofit2所需要的包
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'//ConverterFactory的Gson依赖包
compile 'com.squareup.retrofit2:converter-scalars:2.0.0-beta4'//ConverterFactory的String依赖包
加个XUtils3.0的依赖吧,后期加载图片使用
compile 'org.xutils:xutils:3.3.36'
Permissions are network permissions, I will not write them out!
Layout file, in activity_main, there is a ListView ListView entry layout on the left, an ImageView on the right, and two TextViews on the right
Really into the code ah, keep your eyes open
Create MyApp to inherit Application
public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); x.Ext.init(this); } }Don't forget to register in the manifest file, don't say I didn't tell you
Create Retrofit interface RetrofitUtil
public interface RetrofitUtil { @GET("impressApi/news/mergeList?sign=C7548DE604BCB8A17592EFB9006F9265&pageSize=10&gender=2&ts=1871746850&page=1h") Call<JavaBean> getString(); }
Create the MainModel interface in the model layer
public interface MainModel { void loadData(); }Then create the implementation class IMainModel of MainModel
public class IMainModel implements MainModel { ListView listView; Context context; public IMainModel(ListView listView, Context context) { this.listView = listView; this.context = context; } @Override public void loadData() { Retrofit retrofit = new Retrofit.Builder().baseUrl("http://www.yulin520.com/a2a/") .addConverterFactory(ScalarsConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); RetrofitUtil retrofitUtil = retrofit.create(RetrofitUtil.class); Call<JavaBean> call = retrofitUtil.getString(); call.enqueue(new Callback<JavaBean>() { @Override public void onResponse(Call<JavaBean> call, Response<JavaBean> response) { JavaBean jaon = response.body(); MyBaseAdapter adapter = new MyBaseAdapter(context,jaon.data); listView.setAdapter(adapter); } @Override public void onFailure(Call<JavaBean> call, Throwable t) { } }); } }Create the adapter MyBaseAdapter
public class MyBaseAdapter extends BaseAdapter { private Context context; private ArrayList<JavaBean.DataBean> list; public MyBaseAdapter(Context context, ArrayList<JavaBean.DataBean> list) { this.context = context; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { convertView = View.inflate(context, R.layout.item_layout,null); ImageView imageView = (ImageView) convertView.findViewById(R.id.item_image); TextView item_name = (TextView) convertView.findViewById(R.id.item_name); TextView item_des = (TextView) convertView.findViewById(R.id.item_describe); x.image().bind(imageView,list.get(position).userImg); item_name.setText(list.get(position).title); item_des.setText(list.get(position).introduction); return convertView; } }Create the interface MainPresenter in the presenter layer
public interface MainPresenter { void loadData(); void loadActivity(); }Next is the implementation class IMainPresenter
public class IMainPresenter implements MainPresenter { MainModel mainModel; MainView mianView; public IMainPresenter(MainModel mainModel, MainView mianView) { this.mainModel = mainModel; this.mianView = mianView; } @Override public void loadData() { mainModel.loadData(); } @Override public void loadActivity() { mainModel.loadData(); } }Create the MainView interface in the View layer
public interface MainView { void loadData(); }Let MainActivity implement the MainView interface
public class MainActivity extends AppCompatActivity implements MainView { private ListView listView; MainModel mainModel; private MainPresenter presenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView); mainModel = new IMainModel(listView,this); presenter = new IMainPresenter(mainModel,this); presenter.loadActivity(); } @Override public void loadData() { presenter.loadData(); } }