PecaWolf
PecaWolf

Reputation: 77

Android - sending image by post request using okHttp

This is my first post here so I apologize in advance for any imperfection.

I'm making app for Android which takes a bitmap from camera and sends it to server with couple string parameters (like email etc.). I'm using OkHttp library.

Here's method that should do that:

private void sendPhoto(String docMail, String patMail) {
    String serverUrl = "http://bueatifulurl/awesomescript.php";
    final MediaType MEDIA_TYPE_JPG = MediaType.parse("image/jpg");

    if(isNetworkAvailable()){
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        photo.compress(Bitmap.CompressFormat.JPEG, 30, stream);
        byte[] byteArray = stream.toByteArray();

        OkHttpClient client = new OkHttpClient();

        RequestBody innerBody = new FormEncodingBuilder()
                .add("stuffy", "stuff")
                .build();

        RequestBody requestBody = new MultipartBuilder()
                .type(MultipartBuilder.FORM)
                .addPart(
                        Headers.of("stuffity", "stuff"),innerBody
                ).addPart(
                        Headers.of("pict", "pict"),
                        RequestBody.create(MEDIA_TYPE_JPG,byteArray)
                ).build();

        Request request = new Request.Builder()
                .url(serverUrl)
                .post(requestBody)
                .build();

        Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                Toast.makeText(getApplicationContext(), "Aaaaand you failed", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onResponse(Response response) throws IOException {
                final Response resp = response;
                //Log.v(TAG, resp);
                if (response.isSuccessful()) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            String s = resp.code() + " (" + resp.message() + ")";
                            Toast.makeText(getApplicationContext(), "Success!!!", Toast.LENGTH_LONG).show();
                        }
                    });
                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "Failure!!!", Toast.LENGTH_LONG).show();
                        }
                    });
                }
            }
        });
    }else{
        Toast.makeText(this, "Network Unavailable", Toast.LENGTH_LONG).show();
    }
}

When I press the button that executes this method, app crashes and I get following error:

java.lang.NoSuchMethodError: No interface method writeDecimalLong(J)Lokio/BufferedSink; in class Lokio/BufferedSink; or its super classes (declaration of 'okio.BufferedSink' appears in /data/app/package.myapp/base.apk)

What should I do to make it work? If there is an answer to this somewhere here, please point it out, because I failed to find any. Thank you.

Upvotes: 0

Views: 3624

Answers (1)

Jesse Wilson
Jesse Wilson

Reputation: 40623

Whenever you get a NoSuchMethodError you should check that your library versions work together. I suspect here you need to upgrade Okio to 1.3.

Upvotes: 1

Related Questions