【问题标题】:Android - sending image by post request using okHttpAndroid - 使用 okHttp 通过 post 请求发送图像
【发布时间】:2015-07-24 08:40:12
【问题描述】:

这是我在这里的第一篇文章,所以对于任何不完美之处,我提前道歉。

我正在为 Android 制作应用程序,该应用程序从相机获取位图并使用几个字符串参数(如电子邮件等)将其发送到服务器。我正在使用 OkHttp 库。

这是应该做到这一点的方法:

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();
    }
}

当我按下执行此方法的按钮时,应用程序崩溃并出现以下错误:

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)

我应该怎么做才能让它工作?如果这里的某个地方有这个问题的答案,请指出,因为我没有找到任何答案。谢谢。

【问题讨论】:

    标签: android image post bytearray okhttp


    【解决方案1】:

    每当您收到NoSuchMethodError 时,您都应该检查您的库版本是否可以协同工作。我怀疑您需要将 Okio 升级到 1.3。

    【讨论】:

    • 谢谢帮忙,这解决了一部分问题,剩下的我自己解决了
    • @PecaWolf 如果它解决了您的问题,您能否将答案标记为正确?欢迎在 cmets 中分享其他方面。
    猜你喜欢
    • 2018-04-20
    • 2020-05-12
    • 2017-03-24
    • 2021-08-02
    • 2019-11-24
    • 2023-03-03
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多