导入
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2' compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
======主方法======
package com.example.admin.retrofittest2; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import retrofit.Call; import retrofit.Callback; import retrofit.GsonConverterFactory; import retrofit.Response; import retrofit.Retrofit; public class MainActivity extends AppCompatActivity { private String path="http://api.expoon.com/AppNews/"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Retrofit retrofit=new Retrofit.Builder() .baseUrl(path) .addConverterFactory(GsonConverterFactory.create()) .build(); ApiService apiService=retrofit.create(ApiService.class); Call<Interfacetest> call= apiService.getbean(); call.enqueue(new Callback<Interfacetest>() { @Override public void onResponse(Response<Interfacetest> response, Retrofit retrofit) { Interfacetest body = response.body(); Log.d("返回回来的数据:", body.getInfo()); } @Override public void onFailure(Throwable t) { Log.d("返回回来的数据:", "onFailure"); } }); } }
===========定义一个接口=============
============写一个实体类==================package com.example.admin.retrofittest2; import retrofit.Call; import retrofit.http.GET; /** * Created by admin on 2016/8/24. */ public interface ApiService { @GET("getNewsList/type/1/p/1") Call<Interfacetest> getbean(); }