A type-safe HTTP client for Android and Java
<一>简单使用:
首先我们先引入相关的jar
android studio 引用方式如下:
compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1' compile 'io.reactivex:rxandroid:1.1.0' compile 'io.reactivex:rxjava:1.1.0' compile 'com.squareup.retrofit:retrofit:2.0.0-beta2' compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2' compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2' compile 'de.greenrobot:eventbus:2.4.0'
添加完我们要使用的引用jar之后,我们就可以去使用retrifit:来实现网络数据请求了
<二>我们先写一个接口类:
public interface ApiService { @GET("service/getIpInfo.php") Call<GetIpInfoResponse> getIpInfo(@Query("ip") String ip); @GET("service/getIpInfo.php") Observable<GetIpInfoResponse> getIpInfo2(@Query("ip") String ip); }
上面的是根据不用的需求来定义不同的接口,其实他就是我们所说的api,这里使用函数的方式来定义,是不是看上去舒服多了呢。如果要直接获取HTTP返回的对象,使用
Response
对象。然后写一个 我们最为属性的model类
public class IpInfo { public String country; public String country_id; public String area; public String area_id; public String ip; }
之后写一个返回的回调类这里这类返回的是一个对象就是我们刚才写的ipinfo类,这类我们自定义一个返回的code,这里我们直接继承就ok了public class BaseResponse { public int code; }
public class GetIpInfoResponse extends BaseResponse { public IpInfo data; }
然后我们来看看在activity 是如何去使用的。public class MainActivity extends AppCompatActivity { private static final String ENDPOINT = "http://ip.taobao.com"; private TextView mTvContent; private ProgressBar mProgressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); mTvContent = (TextView) findViewById(R.id.tv_content); mProgressBar = (ProgressBar) findViewById(R.id.progress_bar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Retrofit retrofit = new Retrofit.Builder() .baseUrl(ENDPOINT) .addConverterFactory(GsonConverterFactory.create()) .build(); ApiService apiService = retrofit.create(ApiService.class); mProgressBar.setVisibility(View.VISIBLE); Call<GetIpInfoResponse> call = apiService.getIpInfo("223.255.152.98"); call.enqueue(new Callback<GetIpInfoResponse>() { @Override public void onResponse(Response<GetIpInfoResponse> response, Retrofit retrofit) { mProgressBar.setVisibility(View.GONE); GetIpInfoResponse getIpInfoResponse = response.body(); mTvContent.setText(getIpInfoResponse.data.country); } @Override public void onFailure(Throwable t) { mProgressBar.setVisibility(View.GONE); mTvContent.setText(t.getMessage()); } }); /* Retrofit retrofit = new Retrofit.Builder() .baseUrl(ENDPOINT) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); ApiService apiService = retrofit.create(ApiService.class); apiService.getIpInfo("223.255.152.98") .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<GetIpInfoResponse>() { @Override public void onCompleted() { mProgressBar.setVisibility(View.GONE); } @Override public void onError(Throwable e) { mProgressBar.setVisibility(View.GONE); mTvContent.setText(e.getMessage()); } @Override public void onNext(GetIpInfoResponse getIpInfoResponse) { mTvContent.setText(getIpInfoResponse.data.country); } });*/ } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override protected void onDestroy() { super.onDestroy(); /*反注册EventBus */ BaseApplication.getInstance().unRegister(this); } }
这里就已经完成了我们的初步使用了。是不是很简单呢?记得在清单文件加上<uses-permission android:name="android.permission.INTERNET"/>布局就不贴了就一个按钮添加点击事件就ok了。