Android - 使用okHttp通过邮寄请求发送图像

时间:2015-05-13 07:30:26

标签: android image post bytearray okhttp

这是我在这里发表的第一篇文章,所以我提前为任何不完美而道歉。

我正在制作适用于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)

我该怎么做才能让它发挥作用?如果这里的某个地方有答案,请指出,因为我找不到任何答案。谢谢。

1 个答案:

答案 0 :(得分:1)

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