Created
November 7, 2017 17:12
android insert image database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.putuguna.insertimageandroidtophp; | |
import android.util.Log; | |
import java.io.IOException; | |
import okhttp3.Interceptor; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
import okhttp3.ResponseBody; | |
import okio.Buffer; | |
/** | |
* Created by putuguna on 8/5/2017. | |
*/ | |
public class LoggingInterceptor implements Interceptor { | |
@Override | |
public Response intercept(Chain chain) throws IOException { | |
Request request = chain.request(); | |
long t1 = System.nanoTime(); | |
String requestLog = String.format("Sending request %s on %s%n%s", | |
request.url(), chain.connection(), request.headers()); | |
//YLog.d(String.format("Sending request %s on %s%n%s", | |
// request.url(), chain.connection(), request.headers())); | |
if (request.method().compareToIgnoreCase("post") == 0) { | |
requestLog = "\n" + requestLog + "\n" + bodyToString(request); | |
} | |
Log.d("TAG", "request" + "\n" + requestLog); | |
Response response = chain.proceed(request); | |
long t2 = System.nanoTime(); | |
String responseLog = String.format("Received response for %s in %.1fms%n%s", | |
response.request().url(), (t2 - t1) / 1e6d, response.headers()); | |
String bodyString = response.body().string(); | |
Log.d("TAG", "response" + "\n" + responseLog + "\n" + bodyString); | |
return response.newBuilder() | |
.body(ResponseBody.create(response.body().contentType(), bodyString)) | |
.build(); | |
} | |
public static String bodyToString(final Request request) { | |
try { | |
final Request copy = request.newBuilder().build(); | |
final Buffer buffer = new Buffer(); | |
copy.body().writeTo(buffer); | |
return buffer.readUtf8(); | |
} catch (final IOException e) { | |
return "did not work"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment