Skip to content

Commit

Permalink
增加logging-interceptor和自定义service示例
Browse files Browse the repository at this point in the history
  • Loading branch information
VeiZhang committed Jul 17, 2018
1 parent 855f40b commit d7cd89c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
6 changes: 4 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ dependencies {
compile 'com.android.support:appcompat-v7:25.0.0'
compile project(':retrofit')
// compile 'com.excellence:retrofit:_latestVersion'
compile 'com.excellence:basetools:1.2.4'
compile 'com.excellence:permission:1.0.0'
compile 'com.excellence:basetools:1.2.5'
compile 'com.excellence:permission:1.0.1'
// 必须与Okhttp版本一致
compile 'com.squareup.okhttp3:logging-interceptor:3.6.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import com.excellence.retrofit.RetrofitClient;

import okhttp3.logging.HttpLoggingInterceptor;

/**
* <pre>
* author : VeiZhang
Expand All @@ -27,7 +29,7 @@ public class BaseActivity extends Activity
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
new RetrofitClient.Builder(this).baseUrl(BASE_URL).addLog(true).cacheEnable(true).build();
new RetrofitClient.Builder(this).baseUrl(BASE_URL).addLog(true).cacheEnable(true).addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC)).build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@
import android.widget.TextView;

import com.excellence.retrofit.HttpRequest;
import com.excellence.retrofit.RetrofitClient;
import com.excellence.retrofit.interfaces.Listener;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class GetActivity extends BaseActivity implements View.OnClickListener
{
private Button mGetBtn = null;
Expand All @@ -29,6 +37,33 @@ protected void onCreate(Bundle savedInstanceState)

mGetBtn.setOnClickListener(this);
mObGetBtn.setOnClickListener(this);

userRequest();
}

/**
* 自定义retrofit service
*/
private void userRequest()
{
Retrofit retrofit = RetrofitClient.getInstance().getRetrofit();
UserService service = retrofit.create(UserService.class);
Call<String> call = service.get("http://www.baidu.com", new HashMap<String, String>(), new HashMap<String, String>());
call.enqueue(new Callback<String>()
{
@Override
public void onResponse(Call<String> call, Response<String> response)
{
System.out.println(response.body());
}

@Override
public void onFailure(Call<String> call, Throwable t)
{
t.printStackTrace();
}
});

}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.excellence.retrofit.sample;

import java.util.Map;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.HeaderMap;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;

/**
* <pre>
* author : VeiZhang
* blog : https://veizhang.github.io/
* time : 2017/4/7
* desc :
* </pre>
*/

public interface UserService
{
/**
* GET请求
*
* @param url
* @param params
* @param headers
* @return
*/
@GET
Call<String> get(@Url String url, @QueryMap Map<String, String> params, @HeaderMap Map<String, String> headers);

}

0 comments on commit d7cd89c

Please sign in to comment.