示例代码:
在Android Studio中通过WiFi网络连接到本地服务器,以下为示例代码:
private static final String BASE_URL = "http://192.168.0.101:3000/";
OkHttpClient httpClient = new OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS) .readTimeout(60, TimeUnit.SECONDS) .writeTimeout(60, TimeUnit.SECONDS) .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request originalRequest = chain.request();
Request.Builder builder = originalRequest.newBuilder()
.header("Authorization", "Bearer " + authToken)
.header("Accept", "application/json")
.method(originalRequest.method(), originalRequest.body());
Request newRequest = builder.build();
return chain.proceed(newRequest);
}
})
.build();
Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE