I'm trying to reach an API with AndroidNetworking lib.
Here is how I initalized it:
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);
}
}
I've added this to the manifest too:
<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>
This is my own interceptor, because the one in the doc's example is simply not part of the lib.
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;
}
}
How I'm trying to use it:
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());
}
});
The problem is:
I don't see ANY logs about what is happening. Why?