Abh*_*hek 5 android crashlytics rx-java okhttp retrofit2
我正在尝试使用 OkHttp 通过使用拦截登录到 Crashlytics,因为我的一些客户收到 500 错误,我想完成他们发布的内容和在响应中获得的详细信息。就像 Android Studio 的日志中显示的 OkHttp 一样。
当发生 200 错误但没有成功时,我尝试记录详细信息。
@Module
public class DIModule {
MyApplication application;
public DIModule(MyApplication application) {
this.application = application;
}
public DIModule()
{
}
@Singleton
@Provides
OkHttpClient getOkHttpClient() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
return new OkHttpClient.Builder()
.addInterceptor(logging).addInterceptor(new HeaderInterceptor())
.build();
}
@Singleton
@Provides
Retrofit getRetro(OkHttpClient client) {
return new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
@Singleton
@Provides
Context provideApplicationContext()
{
return application;
}
@Singleton
@Provides
MSharedPref getSharedPreferences(Context app)
{
return new MSharedPref(app);
}
class HeaderInterceptor implements Interceptor
{
@Override
public Response intercept(Chain chain) throws IOException
{
Request.Builder builder = chain.request().newBuilder();
String token="No Value";
if(StaticData.loginResponse!=null)
token=StaticData.loginResponse.getAccess_token();
builder.header("Authorization", "Bearer "+ token)
.header("Accept", "application/json")
.header("Content-Type", "application/json");
Response response=chain.proceed(builder.build());
if(response.code()==200)
{
Log.d("test67","ooooooooooooooooooo"+response);
//here I will log
}
return response;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Log.d() 显示 Response{protocol=http/1.1, code=200, message=OK, url= http://........./api/user }
到crashlytics。
我会从响应中获取所有 OkHttp 的日志,还是我走错了路。非致命日志记录Crashlytics.logException(e);也只采用 Throwable。那么我将如何做呢?
请帮忙..
以下是我为处理此案所做的工作。首先,我创建了一个自定义日志记录异常,它接受调用的请求对象,CustomLoggingException并且仅使用请求对象作为字符串调用异常主构造函数
public class CustomLoggingException extends Exception {
public CustomLoggingException(Request builderRequest, String body) {
super(builderRequest.toString() + body);
}}
Run Code Online (Sandbox Code Playgroud)
请注意,我还将主体作为字符串传递,因为您会遇到请求字符串不记录主体内容但仅记录对象类型的问题。
因此,我检查主体是否不为空,然后获取其字符串内容。
if (builderRequest.body() != null)
body = stringifyRequestBody(builderRequest.body());
Run Code Online (Sandbox Code Playgroud)
stringifyRequestBody 函数将为您提供字符串形式的正文
private String stringifyRequestBody(RequestBody body) {
try {
BufferedSink buffer = new Buffer();
body.writeTo(buffer);
return buffer.getBuffer().readUtf8();
} catch (Exception e) {
Logger.d("Failed to stringify request body: " + e.getMessage());
return "";
}
}
Run Code Online (Sandbox Code Playgroud)
之后,您只需使用这两个输入将该异常记录到 Crashlytics
Crashlytics.logException(new CustomLoggingException(builderRequest, body));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
701 次 |
| 最近记录: |