接口
package com.example.retrofit_gson; /** * date:2018/6/12 * author:殷成龙 * function:黑猫警长 */ public class Contsant { public static final String REG_URL="http://m2.qiushibaike.com/";
}
主页面
package com.example.retrofit_gson; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import com.example.retrofit_gson.Bean; import com.example.retrofit_gson.Contsant; import com.example.retrofit_gson.MyServer; import java.io.IOException; import java.util.ArrayList; import java.util.List; import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; public class MainActivity extends AppCompatActivity { private Retrofit retrofit; private TextView tv_title; private MyServer myServer; private List<Bean.ItemsBean> beanList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_title = findViewById(R.id.tv_title); retrofit = new Retrofit.Builder() .baseUrl(Contsant.REG_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); myServer = retrofit.create(MyServer.class); /*myServer.getLatestJsonString().enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { try { tv_title.setText(response.body().string()); } catch (IOException e) { e.printStackTrace(); } } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { } });*/ int a = 1; myServer.getInfoList("latest",a).enqueue(new Callback<Bean>() { @Override public void onResponse(Call<Bean> call, Response<Bean> response) { List<Bean.ItemsBean> items = response.body().getItems(); beanList.addAll(items); tv_title.setText(beanList.get(1).getContent()); } @Override public void onFailure(Call<Bean> call, Throwable t) { } }); } }
接口类
package com.example.retrofit_gson; import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Path; import retrofit2.http.Query; /** * date:2018/6/12 * author:殷成龙 * function:黑猫警长 */ public interface MyServer { @GET("article/list/latest?page=1") Call<ResponseBody> getLatestJsonString(); @GET("article/list/{type}?") Call<Bean> getInfoList(@Path("type") String type, @Query("page") int page); }
接口类
package com.example.retrofit_gson; import java.util.Map; import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Path; import retrofit2.http.Query; import retrofit2.http.QueryMap; import retrofit2.http.Url; /** * date:2018/6/12 * author:殷成龙 * function: */ public interface MyServerInterface { //GET网络请求 /** * 基本的Get请求 * http://m2.qiushibaike.com/article/list/latest?page=6 * RxJava和Retrofit的封装,其中有一点就是解决这个问题 */ @GET("article/list/latest?page=1") Call<ResponseBody> getLatestJsonString(); /** * http://m2.qiushibaike.com/article/list/latest?page=6 * 指定Path参数和Query参数 * 注解中的变量{ 变量名称自定义 } 在方法参数中 * 格式:@Path("上面定义的变量名") 类型 定义的变量名 作用:替换URL地址中{}所包含的部分 * 格式:@Query("参数名") 类型 定义到的参数名 作用:会拼接在URL的最后部分,类似追加了"page = 1"的字符串,形成后提供给服务器的网址 */ @GET("article/list/{type}?") Call<Bean.ItemsBean.UserBean> getInfoList(@Path("type") String type, @Query("page") int page); /** * 请求网络,动态的使用网址 * 在注解中不指定URL地址,代码中指定网址,会忽略默认网址,使用指定的网址 */ @GET Call<ResponseBody> getNetWorkDate(@Url String urlString); /** * Get请求,方法中没有参数,但是我在注解里面定义了完整URL路径,这种情况下BaseURL会被忽略掉 */ @GET("http://img.61gequ.com/allimg/2013-02/2167-130201101H7.jpg") Call<ResponseBody> getNetWorkDate(); /** * Get请求提交表单的数据 * @QueryMap 参数将在Url地址中添加 " type =text & count = 30"的字符串 */ @GET("web/LoginServlet") Call<ResponseBody> getRegInfo(@QueryMap Map<String, String> map); }
布局
<?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" tools:context="com.example.retrofit_gson.MainActivity"> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
//Retrofit2的依赖 compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'不要忘记加网络权限