0

I am using okhttp3 to send an image to the server, this way:

final OkHttpClient cliente=new OkHttpClient();

    HttpUrl.Builder urlBuilder = HttpUrl.parse(getString(R.string.sendimageurl)).newBuilder();

    String url = urlBuilder.build().toString();
    Request request=new Request.Builder()
            .addHeader("imagebase64", toBASE64(bitmap))
            .addHeader("token", almacen.getToken())
            .url(url)
            .build();

(Yes, params are sent in the headers, don't blame me, its the way the service is built).

It is always entering in onFailure() method, launching ConnectionShutdownException:

okhttp3.internal.http2.ConnectionShutdownException
at okhttp3.internal.http2.Http2Connection.newStream(Http2Connection.java:248)
at okhttp3.internal.http2.Http2Connection.newStream(Http2Connection.java:231)
at okhttp3.internal.http2.Http2Codec.writeRequestHeaders(Http2Codec.java:117)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:50)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:254)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:200)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)

I am not setting any interceptor in my call.

What can I do to send my image?

Thank you.

EDIT:

After Christopher's suggestion, I do:

final OkHttpClient cliente=new OkHttpClient();

    OkHttpClient client1 = cliente.newBuilder()
            .readTimeout(10000, TimeUnit.MILLISECONDS)
            .build();

    HttpUrl.Builder urlBuilder = HttpUrl.parse(getString(R.string.sendimageurl)).newBuilder();

    String url = urlBuilder.build().toString();
    Request request=new Request.Builder()
            .addHeader("imagebase64", toBASE64(bitmap))
            .addHeader("token", almacen.getToken())
            .url(url)
            .build();
    client1.newCall(request).enqueue(new Callback() {
       //stuff
     }

But no result, same exception.

EDIT 2:

After sea cat answer, I realized it should be a POST request. Changed to:

final OkHttpClient cliente=new OkHttpClient();

    HttpUrl.Builder urlBuilder = HttpUrl.parse(getString(R.string.sendimageurl)).newBuilder();

    String url = urlBuilder.build().toString();
    RequestBody requestBody = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("imagebase64", toBASE64(bitmap))
            .addFormDataPart("token", almacen.getToken())
            .build();

    Request request=new Request.Builder()
            .addHeader("imagebase64", toBASE64(bitmap))
            .addHeader("token", almacen.getToken())
            .url(url)
            .post(requestBody)
            .build();

(The data has to be sent in headers, but the post needs addFormDataPart, so I added them there too) but the exception is the same.

Fustigador
  • 6,339
  • 12
  • 59
  • 115

1 Answers1

3

Your request is of type get(). Is that what you want?

(I lack the reputation to comment)

sea cat
  • 251
  • 5
  • 8
  • Thanks for pointing that out, actually it shold be a POST request. But it didn't solved the issue. But thanks, since that should be addressed too. – Fustigador Jan 29 '19 at 11:32
  • Tried to send the image without the data in headers...and despite that they told me that the data must be sent in headers, now it returns a 200. So I guess someone lied me, unadvertedly. I am upvoting you, since you put me in the right track. Thanks again! – Fustigador Jan 29 '19 at 11:43
  • Cheers, that put me in the 50s and I can comment now. I would suggest using Fiddler to see the traffic, you can see exactly what you send to the server, headers and all (android under 7 I think), and what the server comes back with. And play around with sending no payload to the server, I'm suspecting it'll just answer with a 200, but there might be a message with it – sea cat Jan 30 '19 at 11:38