MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// http://api.svipmovie.com/front/videoDetailApi/videoDetail.do?mediaId=d8628ea824ca4ae9af3cfc0d4afed0a9
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://www.zhaoapi.cn")
// .addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.build();
// testPath(retrofit);
GitHubService service = retrofit.create(GitHubService.class);
Call<String> call = service.stringGet();
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
String result = response.body() ;
System.out.println(Thread.currentThread().getName());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
// Call<LoginBean> call = service.url("/user/login?mobile=18588888888&password=111111");
//
// call.enqueue(new Callback<LoginBean>() {
// @Override
// public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
// LoginBean bean = response.body() ;
// System.out.println(Thread.currentThread().getName());
//
// }
//
// @Override
// public void onFailure(Call<LoginBean> call, Throwable t) {
//
// }
// });
// Call<LoginBean> call = service.login("18588888888","111111");
// Map<String,String> map = new HashMap<>();
// map.put("mobile","18588888888");
// map.put("password","111111");
//
//
// Call<ResponseBody> call = service.postMap1(map);
//
// call.enqueue(new Callback<ResponseBody>() {
// @Override
// public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
// try {
// String resu = response.body().string() ;
// System.out.println(Thread.currentThread().getName());
//
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
//
// @Override
// public void onFailure(Call<ResponseBody> call, Throwable t) {
//
// }
// });
//
// Call<LoginBean> call = service.postBody(new Login("18588888888","111111"));
//
// call.enqueue(new Callback<LoginBean>() {
// @Override
// public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
// LoginBean bean = response.body() ;
// System.out.println(Thread.currentThread().getName());
// }
//
// @Override
// public void onFailure(Call<LoginBean> call, Throwable t) {
//
// }
// });
// Call<Bean> call = service.testGetParams("d8628ea824ca4ae9af3cfc0d4afed0a9");
//
// 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());
//
// }
//
// @Override
// public void onFailure(Call<Bean> call, Throwable t) {
//
// }
// });
}
public void testIpPath(Retrofit retrofit){
GitHubService service = retrofit.create(GitHubService.class);
Call<BweiBean> call = service.testBwei("Bwei");
call.enqueue(new Callback<BweiBean>() {
@Override
public void onResponse(Call<BweiBean> call, Response<BweiBean> response) {
BweiBean bean = response.body() ;
System.out.println(Thread.currentThread().getName());
}
@Override
public void onFailure(Call<BweiBean> call, Throwable t) {
System.out.println(Thread.currentThread().getName());
System.out.println(Thread.currentThread().getName());
System.out.println(Thread.currentThread().getName());
System.out.println(Thread.currentThread().getName());
System.out.println(Thread.currentThread().getName());
}
});
}
private void testPath(Retrofit retrofit){
GitHubService gitHubService = retrofit.create(GitHubService.class);
Call<Bean> call = gitHubService.test1509D("front");
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());
}
@Override
public void onFailure(Call<Bean> call, Throwable t) {
}
});
}
}
接口
public interface GitHubService {
//
// Path 路径替换
// GET get 请求
@GET("{user}/videoDetailApi/videoDetail.do?mediaId=d8628ea824ca4ae9af3cfc0d4afed0a9")
Call<Bean> test1509D(@Path("user") String user);
@GET("/{test}/news")
Call<BweiBean> testBwei(@Path("test") String test);
@GET("/Bwei/news")
Call<BweiBean> testGet();
// d8628ea824ca4ae9af3cfc0d4afed0a9
// Query get 请求带参数
@GET("front/videoDetailApi/videoDetail.do")
Call<Bean> testGetParams(@Query("mediaId") String value);
// https://www.zhaoapi.cn/user/login?mobile=18588888888&password=111111
@GET("/user/login")
Call<LoginBean> login(@Query("mobile") String mobile,@Query("password") String password);
@GET("/user/login")
Call<LoginBean> loginMap(@QueryMap Map<String,String> map);
@FormUrlEncoded
@POST("/user/login")
Call<LoginBean> post(@Field("mobile") String mobile,@Field("password") String password);
@FormUrlEncoded
@POST("/user/login")
Call<LoginBean> postMap(@FieldMap Map<String,String> map);
@Headers({
"Cache-Control: max-age=640000",
"Accept: application/vnd.github.v3.full+json"
})
@POST("/user/login")
Call<LoginBean> postBody(@Body Login login);
@FormUrlEncoded
@POST("/user/login")
Call<ResponseBody> postMap1(@FieldMap Map<String,String> map);
@GET
Call<LoginBean> url(@Url String url);
@GET("/user/login?mobile=18588888888&password=111111")
Call<String> stringGet();
//
// @GET
// Call<String> getAll(@Url String url,@QueryMap Map<String,String> map);
//
// @POST
// Call<String> postAll(@Url String url , @FieldMap Map<String,String> map);
//
}