square / okhttp

Square’s meticulous HTTP client for the JVM, Android, and GraalVM.
https://square.github.io/okhttp/
Apache License 2.0
45.87k stars 9.16k forks source link

The image is not sent by the post method #7779

Closed zibit1985 closed 1 year ago

zibit1985 commented 1 year ago

in the emulator, what is less than 2 kb is sent from the phone does not go away (size 30-50 kb) writes I/System.out: java.io.IOException: unexpected end of stream on http:// what can not be tell me?

`public Boolean SendImage_ms(String serverURL) {
        try {
        Bitmap image = photo_img_ms;
        String id_ms = ID_Item.toString();
        String filename = "image.png";

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.PNG, 10, baos);

      //  String encoded_file;
      //  encoded_file = Base64.getEncoder().encodeToString(baos.toByteArray());

        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("file", filename,
                        RequestBody.create(MEDIA_TYPE_PNG,
                                baos.toByteArray()))

                .addFormDataPart("id", id_ms)
                //.addFormDataPart("file", encoded_file)
                .addFormDataPart("api_key", api_key)
                .build();

        Request request = new Request.Builder()
                .url(serverURL)
                .addHeader("Content-Type", "multipart/form-data; boundary=something")
                .addHeader("Connection", "close")
                .method("POST",body)
                .build();

        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new LoggingInterceptor())
                .writeTimeout(600, TimeUnit.SECONDS)
                .readTimeout(600, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .build();

        client.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                System.out.println(e.toString());
            }

            @Override
            public void onResponse(final Call call, final Response response) throws IOException {
                if (!response.isSuccessful()) {
                    System.out.println("error");
                }
                System.out.println(response.body().string());
            }
        });

        return true;
    } catch (Exception ex) {
        System.out.println(ex.toString());
        // Handle the error
    }
    return false;
}

class LoggingInterceptor implements Interceptor {
    @Override public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();

        long t1 = System.nanoTime();
        Log.d("OkHttp", String.format("Sending request %s on %s%n%s",
                request.url(), chain.connection(), request.headers(), request.body(),request.toString()));

        Response response = chain.proceed(request);

        long t2 = System.nanoTime();
        Log.d("OkHttp", String.format("Received response for %s in %.1fms%n%s",
                response.request().url(), (t2 - t1) / 1e6d, response.headers()));

        return response;
    }
}`
yschimke commented 1 year ago

We can't provide 1:1 user support. Sorry.

I'd suggest using a site like stackoverflow, or make a reproducible bug report, such as a standalone test that fails.

i-infra commented 11 months ago

Try without LoggingInterceptor ref #8138