gpt4 book ai didi

AndroidNetworking lib 没有记录任何东西

转载 作者:行者123 更新时间:2023-12-04 04:11:03 25 4
gpt4 key购买 nike

我正在尝试使用 AndroidNetworking 访问 API图书馆。

这是我初始化它的方式:

public class TestLabApp extends Application {

@Override
public void onCreate() {
super.onCreate();


//For logging
RealInterceptor realInterceptor = new RealInterceptor();
realInterceptor.enableLoggingForBody(true);
realInterceptor.enableLoggingForUrl(true);
realInterceptor.enableLoggingForHeaders(true);
realInterceptor.enableLoggingForHttpStatusCodes(true);
realInterceptor.enableLoggingForExecutionTime(false);

//Add logging to okHttpClient
OkHttpClient okHttpClient = new OkHttpClient()
.newBuilder()
.addNetworkInterceptor(realInterceptor)
.build();

//Init AndroidNetworking lib with the okHttpClient (with aloggint interceptor)
AndroidNetworking.initialize(getApplicationContext(), okHttpClient);

}
}

我也将此添加到 list 中:

 <application
android:name=".TestLabApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

这是我自己的拦截器,因为文档示例中的拦截器根本不是库的一部分。

public class RealInterceptor implements Interceptor {

private boolean logUrl = true;
private boolean logBody = true;
private boolean logHeaders = true;
private boolean logHttpStatusCodes = true;
private boolean logExecutionTime = true;


public void enableLoggingForUrl(boolean logUrl) {
this.logUrl = logUrl;
}

public void enableLoggingForHeaders(boolean logHeaders) {
this.logHeaders = logHeaders;
}

public void enableLoggingForBody(boolean logBody) {
this.logBody = logBody;
}

public void enableLoggingForHttpStatusCodes(boolean logHttpStatusCodes) {
this.logHttpStatusCodes = logHttpStatusCodes;
}

public void enableLoggingForExecutionTime(boolean logExecutionTime) {
this.logExecutionTime = logExecutionTime;
}


private void logInfo(Object o) {
Log.i(getClass().getSimpleName(), o.toString());
}

private void logError(Object o) {
Log.e(getClass().getSimpleName(), o.toString());
}

@Override
public Response intercept(Chain chain) throws IOException {

StringBuilder sb = new StringBuilder();

Request request = chain.request();
RequestBody requestBody = request.body();
boolean hasRequestBody = requestBody != null;


if (logUrl) {
sb.append("\nURL: " + request.url());
}

if (logBody) {
if (hasRequestBody) {
Buffer buffer = new Buffer();
requestBody.writeTo(buffer);
String bodyParams = buffer.readString(Charset.forName("UTF-8"));
bodyParams = bodyParams.replace("&", "\nParam: ");

sb.append("\nParam: " + bodyParams);
} else {
sb.append("\nParam: <No params>");
}
}


if (logHeaders) {
Headers headers = request.headers();
String headersStr = "";
for (int i = 0, count = headers.size(); i < count; i++) {
headersStr += "\nHeader: " + headers.name(i) + ": " + headers.value(i);
}
sb.append(headersStr);
}

long startNs = System.nanoTime();
Response response;
try {
response = chain.proceed(request);

} catch (Exception e) {
throw e;
}

if (logHttpStatusCodes) {
sb.append("\nHTTP Status code: " + response.code());
}

long tookSec = TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startNs);
long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);

if (logExecutionTime) {
if (tookSec > 1) {
sb.append("\nExecution time: " + tookSec + " sec");
} else {
sb.append("\nExecution time: " + tookMs + " ms");
}
}

if (response.code() != 200) {
logError(sb.toString());
} else {
logInfo(sb.toString());
}


return response;
}

我如何尝试使用它:

AndroidNetworking.post("http://myurlishere.hu/api/test-result/save")
.addBodyParameter("param_1", "12345")
.addBodyParameter("param_2", "abcdef")
.build()
.getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
// do anything with response

Log.i("RESP___", response.toString());

codeTv.setText(getString(R.string.please_read_the_qr_code));

enableControls(true);
progressBar.setVisibility(View.INVISIBLE);
zBarScannerView.resumeCameraPreview(MainActivity.this);

Toast.makeText(MainActivity.this, "API done", Toast.LENGTH_SHORT).show();

}
@Override
public void onError(ANError error) {
// handle error

codeTv.setText(getString(R.string.please_read_the_qr_code));
enableControls(true);
progressBar.setVisibility(View.INVISIBLE);
zBarScannerView.resumeCameraPreview(MainActivity.this);


DialogHelper.showInfo(MainActivity.this, "Error: Body: "+error.getErrorBody()+", Response: "+error.getResponse()+", Detail: "+error.getErrorDetail()+", Code: "+error.getErrorCode());
}
});

问题是:

我没有看到任何关于正在发生的事情的日志。为什么?

最佳答案

您的代码看起来不错,但我可以建议日志记录的替代解决方案

首先,没有必要创建您自己的日志记录,package okhttp3.logging 已经提供了 HttpLoggingInterceptor,它会记录您在 中期望的所有值真正的拦截器

这是一个替代解决方案:

   val logging = HttpLoggingInterceptor()
logging.level = HttpLoggingInterceptor.Level.BODY
okHttpClientBuilder!!.addInterceptor(logging)

在你的情况下:

  //Add logging to okHttpClient
var okHttpClient = OkHttpClient()
.newBuilder()
.addNetworkInterceptor(logging)
.build()

希望这会有所帮助,如果应用程序处于 Release模式,您可以通过添加条件来禁用日志记录:

    var okHttpClientBuilder = OkHttpClient()
.newBuilder()

if (BuildConfig.DEBUG) {
okHttpClientBuilder.addNetworkInterceptor(logging)
}

var okHttpClient = okHttpClientBuilder.build()

关于AndroidNetworking lib 没有记录任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61728606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com