-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathApiClient.java
More file actions
103 lines (81 loc) · 3.54 KB
/
ApiClient.java
File metadata and controls
103 lines (81 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package github.vatsal.easyweather.retrofit.api;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ApiClient {
private static ApiClient uniqInstance;
private final String URL_LIVE = "http://api.openweathermap.org/data/2.5/";
private WeatherInterface weatherInterface;
public static synchronized ApiClient getInstance() {
if (uniqInstance == null) {
uniqInstance = new ApiClient();
}
return uniqInstance;
}
private void ApiClient(final String appId) {
try {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
// set your desired log level
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
Interceptor headerInterceptor = new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
Request.Builder builder = original.newBuilder();
builder.method(original.method(), original.body());
builder.addHeader(WeatherInterface.FIELDS.APPID, appId);
Request request = builder.build();
return chain.proceed(request);
}
};
/* Interceptor bodyInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
// String stringJson = response.body().toString();
//response.body().bytes();
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(response.body().toString());
jsonObject.put("_3h", jsonObject.get("3h"));
jsonObject.remove("3h");
} catch (JSONException e) {
e.printStackTrace();
}
MediaType contentType = response.body().contentType();
ResponseBody body = ResponseBody.create(contentType, String.valueOf(jsonObject));
return response.newBuilder().body(body).build();
}
};*/
OkHttpClient httpClient = new OkHttpClient.Builder()
.addInterceptor(headerInterceptor)
.addInterceptor(logging)
// .addInterceptor(bodyInterceptor)
.build();
//httpClient.networkInterceptors().add(headerInterceptor);
String API_URL = URL_LIVE;
// <-- this is the important line!
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient)
.build();
weatherInterface = retrofit.create(WeatherInterface.class);
} catch (Exception e) {
e.printStackTrace();
}
}
public WeatherInterface getApi(String appId) {
if (uniqInstance == null) {
getInstance();
}
uniqInstance.ApiClient(appId);
return weatherInterface;
}
}