compile 'com.squareup.retrofit2:retrofit:2.3.0' compile'com.squareup.retrofit2:converter-gson:2.3.0' implementation 'com.google.code.gson:gson:2.8.2'
<uses-permission android:name="android.permission.INTERNET" />
activity_main
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView tools:listitem="@layout/item" android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </android.support.constraint.ConstraintLayout>
item
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <ImageView android:id="@+id/img" android:layout_width="60dp" android:layout_height="60dp" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/te" android:layout_width="match_parent" android:layout_height="60dp" android:text="1111111" /> </LinearLayout> </android.support.constraint.ConstraintLayout>
Bean
根据接口内容封装
GitHubService
import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Path; /** * Created by lenovo on 2017/11/29. */ public interface GitHubService { //videoDetailApi @GET("front/{user}/videoDetail.do?mediaId=d8628ea824ca4ae9af3cfc0d4afed0a9") //把参数user 替换到 {user} Call<Bean> ListRepos(@Path("user")String user); }
MainActivity
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import com.google.gson.Gson; import java.util.ArrayList; import java.util.List; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; public class MainActivity extends AppCompatActivity { private List<Bean.RetBean.ListBean.ChildListBean> list = new ArrayList<>(); private ListView listView; private Gson gson; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = findViewById(R.id.listview); // http://api.svipmovie.com/front/videoDetailApi/videoDetail.do?mediaId=d8628ea824ca4ae9af3cfc0d4afed0a9 Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://api.svipmovie.com/") // .addConverterFactory(ScalarsConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); GitHubService gitHubService = retrofit.create(GitHubService.class); Call<Bean> call = gitHubService.ListRepos("videoDetailApi"); call.enqueue(new Callback<Bean>() { @Override public void onResponse(Call<Bean> call, Response<Bean> response) { Bean bean = response.body(); System.out.println(Thread.currentThread().getName()); System.out.println(list.toString()); list = bean.getRet().getList().get(0).getChildList(); MyAdapter adapter = new MyAdapter(); listView.setAdapter(adapter); } @Override public void onFailure(Call<Bean> call, Throwable t) { System.out.println("失败"); } }); } class MyAdapter extends BaseAdapter { @Override public int getCount() { return list == null ? 0 : 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) { Hand hand; if (convertView == null) { convertView = View.inflate(MainActivity.this, R.layout.item, null); hand = new Hand(); hand.te = convertView.findViewById(R.id.te); // hand.img = convertView.findViewById(R.id.img); convertView.setTag(hand); } else { hand = (Hand) convertView.getTag(); } hand.te.setText(list.get(position).getTitle()); // hand.te.setText("111111111"); return convertView; } } class Hand { ImageView img; TextView te; } }